package repository

import (
	"context"

	"gitlab.com/mbugroup/lti-api.git/internal/common/repository"
	entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
	"gorm.io/gorm"
)

type SupplierRepository interface {
	repository.BaseRepository[entity.Supplier]
	NameExists(ctx context.Context, name string, excludeID *uint) (bool, error)
	AliasExists(ctx context.Context, alias string, excludeID *uint) (bool, error)
	IdExists(ctx context.Context, id uint) (bool, error)
}

type SupplierRepositoryImpl struct {
	*repository.BaseRepositoryImpl[entity.Supplier]
	db *gorm.DB
}

func NewSupplierRepository(db *gorm.DB) SupplierRepository {
	return &SupplierRepositoryImpl{
		BaseRepositoryImpl: repository.NewBaseRepository[entity.Supplier](db),
		db:                 db,
	}
}

func (r *SupplierRepositoryImpl) NameExists(ctx context.Context, name string, excludeID *uint) (bool, error) {
	return repository.ExistsByName[entity.Supplier](ctx, r.db, name, excludeID)
}

func (r *SupplierRepositoryImpl) AliasExists(ctx context.Context, alias string, excludeID *uint) (bool, error) {
	return repository.ExistsByField[entity.Supplier](ctx, r.db, "alias", alias, excludeID)
}

func (r *SupplierRepositoryImpl) IdExists(ctx context.Context, id uint) (bool, error) {
	return repository.Exists[entity.Supplier](ctx, r.db, id)
}
