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 ProjectChickinRepository interface {
	repository.BaseRepository[entity.ProjectChickin]
	GetFirstByProjectFlockID(ctx context.Context, projectFlockID uint) (*entity.ProjectChickin, error)
}

type ChickinRepositoryImpl struct {
	*repository.BaseRepositoryImpl[entity.ProjectChickin]
}

func NewChickinRepository(db *gorm.DB) ProjectChickinRepository {
	return &ChickinRepositoryImpl{
		BaseRepositoryImpl: repository.NewBaseRepository[entity.ProjectChickin](db),
	}
}

func (r *ChickinRepositoryImpl) GetFirstByProjectFlockID(ctx context.Context, projectFlockID uint) (*entity.ProjectChickin, error) {
	var chickin entity.ProjectChickin
	err := r.DB().WithContext(ctx).
		Where("project_floc_id = ?", projectFlockID).
		Where("deleted_at IS NULL").
		First(&chickin).Error
	if err != nil {
		return nil, err
	}
	return &chickin, nil
}
