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 ProjectChickinDetailRepository interface {
	repository.BaseRepository[entity.ProjectChickinDetail]
	CreateOne(ctx context.Context, entity *entity.ProjectChickinDetail, modifier func(*gorm.DB) *gorm.DB) error
	DeleteMany(ctx context.Context, modifier func(*gorm.DB) *gorm.DB) error
	GetByProjectChickinID(ctx context.Context, projectChickinID uint) ([]entity.ProjectChickinDetail, error)
	WithTxRepo(tx *gorm.DB) ProjectChickinDetailRepository
}

type ChickinDetailRepositoryImpl struct {
	*repository.BaseRepositoryImpl[entity.ProjectChickinDetail]
}

func NewChickinDetailRepository(db *gorm.DB) ProjectChickinDetailRepository {
	return &ChickinDetailRepositoryImpl{
		BaseRepositoryImpl: repository.NewBaseRepository[entity.ProjectChickinDetail](db),
	}
}

func (r *ChickinDetailRepositoryImpl) WithTxRepo(tx *gorm.DB) ProjectChickinDetailRepository {
	return &ChickinDetailRepositoryImpl{BaseRepositoryImpl: repository.NewBaseRepository[entity.ProjectChickinDetail](tx)}
}

func (r *ChickinDetailRepositoryImpl) DB() *gorm.DB {
	return r.BaseRepositoryImpl.DB()
}

func (r *ChickinDetailRepositoryImpl) GetByProjectChickinID(ctx context.Context, projectChickinID uint) ([]entity.ProjectChickinDetail, error) {
	var records []entity.ProjectChickinDetail
	if err := r.DB().WithContext(ctx).Where("project_chickin_id = ?", projectChickinID).Find(&records).Error; err != nil {
		return nil, err
	}
	if len(records) == 0 {
		return nil, gorm.ErrRecordNotFound
	}
	return records, nil
}
