package accountdb

import (
	ri "github.com/tech/sendico/pkg/db/repository/index"
	"github.com/tech/sendico/pkg/db/template"
	"github.com/tech/sendico/pkg/mlogger"
	"github.com/tech/sendico/pkg/model"
	"github.com/tech/sendico/pkg/mservice"
	"go.mongodb.org/mongo-driver/v2/mongo"
	"go.uber.org/zap"
)

type AccountDB struct {
	template.DBImp[*model.Account]
}

func Create(logger mlogger.Logger, db *mongo.Database) (*AccountDB, error) {
	p := &AccountDB{
		DBImp: *template.Create[*model.Account](logger, mservice.Accounts, db),
	}

	if err := p.Repository.CreateIndex(&ri.Definition{
		Keys:   []ri.Key{{Field: "login", Sort: ri.Asc}},
		Unique: true,
	}); err != nil {
		p.Logger.Error("Failed to create account database", zap.Error(err))
		return nil, err
	}
	return p, nil
}
