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 InjectionRepository interface {
	repository.BaseRepository[entity.Payment]
	BankExists(ctx context.Context, bankId uint) (bool, error)
	NextPaymentSequence(ctx context.Context) (int64, error)
}

type InjectionRepositoryImpl struct {
	*repository.BaseRepositoryImpl[entity.Payment]
	db *gorm.DB
}

func NewInjectionRepository(db *gorm.DB) InjectionRepository {
	return &InjectionRepositoryImpl{
		BaseRepositoryImpl: repository.NewBaseRepository[entity.Payment](db),
		db:                 db,
	}
}

func (r *InjectionRepositoryImpl) BankExists(ctx context.Context, bankId uint) (bool, error) {
	return repository.Exists[entity.Bank](ctx, r.db, bankId)
}

func (r *InjectionRepositoryImpl) NextPaymentSequence(ctx context.Context) (int64, error) {
	var next int64
	if err := r.db.WithContext(ctx).
		Raw("SELECT nextval('payments_code_seq')").
		Scan(&next).Error; err != nil {
		return 0, err
	}
	return next, nil
}
