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 ProjectBudgetRepository interface {
	repository.BaseRepository[entity.ProjectBudget]
	GetByProjectFlockID(ctx context.Context, projectFlockID uint) ([]entity.ProjectBudget, error)
}

type ProjectBudgetRepositoryImpl struct {
	*repository.BaseRepositoryImpl[entity.ProjectBudget]
	db *gorm.DB
}

func NewProjectBudgetRepository(db *gorm.DB) ProjectBudgetRepository {
	return &ProjectBudgetRepositoryImpl{
		BaseRepositoryImpl: repository.NewBaseRepository[entity.ProjectBudget](db),
		db:                 db,
	}
}

func (r *ProjectBudgetRepositoryImpl) GetByProjectFlockID(ctx context.Context, projectFlockID uint) ([]entity.ProjectBudget, error) {
	var budgets []entity.ProjectBudget
	err := r.db.WithContext(ctx).
		Where("project_flock_id = ?", projectFlockID).
		Preload("Nonstock").
		Preload("Nonstock.Uom").
		Preload("Nonstock.Flags").
		Find(&budgets).Error
	return budgets, err
}
