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 LayingTransferSourceRepository interface {
	repository.BaseRepository[entity.LayingTransferSource]
	GetByLayingTransferId(ctx context.Context, layingTransferId uint) ([]entity.LayingTransferSource, error)
}

type LayingTransferSourceRepositoryImpl struct {
	*repository.BaseRepositoryImpl[entity.LayingTransferSource]
	db *gorm.DB
}

func NewLayingTransferSourceRepository(db *gorm.DB) LayingTransferSourceRepository {
	return &LayingTransferSourceRepositoryImpl{
		BaseRepositoryImpl: repository.NewBaseRepository[entity.LayingTransferSource](db),
		db:                 db,
	}
}

func (r *LayingTransferSourceRepositoryImpl) GetByLayingTransferId(ctx context.Context, layingTransferId uint) ([]entity.LayingTransferSource, error) {
	var sources []entity.LayingTransferSource
	err := r.db.WithContext(ctx).
		Where("laying_transfer_id = ?", layingTransferId).
		Order("created_at DESC").
		Find(&sources).Error
	if err != nil {
		return nil, err
	}
	return sources, nil
}
