package recipientdb

import (
	"context"

	"github.com/tech/sendico/pkg/auth"
	"github.com/tech/sendico/pkg/db/policy"
	"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"
)

type RecipientDB struct {
	auth.ProtectedDBImp[*model.Recipient]
	auth.ArchivableDB[*model.Recipient]
}

func Create(ctx context.Context,
	logger mlogger.Logger,
	enforcer auth.Enforcer,
	pdb policy.DB,
	db *mongo.Database,
) (*RecipientDB, error) {
	p, err := auth.CreateDBImp[*model.Recipient](ctx, logger, pdb, enforcer, mservice.Recipients, db)
	if err != nil {
		return nil, err
	}

	createEmpty := func() *model.Recipient {
		return &model.Recipient{}
	}

	getArchivable := func(c *model.Recipient) model.Archivable {
		return &c.ArchivableBase
	}

	res := &RecipientDB{
		ProtectedDBImp: *p,
		ArchivableDB: auth.NewArchivableDB(
			p.DBImp,
			p.DBImp.Logger,
			enforcer,
			createEmpty,
			getArchivable,
		),
	}
	return res, nil
}
