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 WarehouseRepository interface {
	repository.BaseRepository[entity.Warehouse]
	AreaExists(ctx context.Context, areaId uint) (bool, error)
	LocationExists(ctx context.Context, locationId uint) (bool, error)
	KandangExists(ctx context.Context, kandangId uint) (bool, error)
	NameExists(ctx context.Context, name string, excludeID *uint) (bool, error)
	IdExists(ctx context.Context, id uint) (bool, error)
	GetByKandangID(ctx context.Context, kandangId uint) (*entity.Warehouse, error)
	GetByKandangIDAndLocationID(ctx context.Context, kandangId uint, locationId uint) (*entity.Warehouse, error)
	GetLatestByKandangID(ctx context.Context, kandangId uint) (*entity.Warehouse, error)
}

type WarehouseRepositoryImpl struct {
	*repository.BaseRepositoryImpl[entity.Warehouse]
	db *gorm.DB
}

func NewWarehouseRepository(db *gorm.DB) WarehouseRepository {
	return &WarehouseRepositoryImpl{
		BaseRepositoryImpl: repository.NewBaseRepository[entity.Warehouse](db),
		db:                 db,
	}
}

func (r *WarehouseRepositoryImpl) AreaExists(ctx context.Context, areaId uint) (bool, error) {
	return repository.Exists[entity.Area](ctx, r.db, areaId)
}

func (r *WarehouseRepositoryImpl) LocationExists(ctx context.Context, locationId uint) (bool, error) {
	return repository.Exists[entity.Location](ctx, r.db, locationId)
}

func (r *WarehouseRepositoryImpl) KandangExists(ctx context.Context, kandangId uint) (bool, error) {
	return repository.Exists[entity.Kandang](ctx, r.db, kandangId)
}

func (r *WarehouseRepositoryImpl) NameExists(ctx context.Context, name string, excludeID *uint) (bool, error) {
	return repository.ExistsByName[entity.Warehouse](ctx, r.db, name, excludeID)
}
func (r *WarehouseRepositoryImpl) IdExists(ctx context.Context, id uint) (bool, error) {
	return repository.Exists[entity.Warehouse](ctx, r.db, id)
}

func (r *WarehouseRepositoryImpl) GetByKandangID(ctx context.Context, kandangId uint) (*entity.Warehouse, error) {
	var warehouse entity.Warehouse
	err := r.db.WithContext(ctx).
		Where("kandang_id = ?", kandangId).
		Where("deleted_at IS NULL").
		First(&warehouse).Error
	if err != nil {
		return nil, err
	}
	return &warehouse, nil
}

func (r *WarehouseRepositoryImpl) GetByKandangIDAndLocationID(ctx context.Context, kandangId uint, locationId uint) (*entity.Warehouse, error) {
	var warehouse entity.Warehouse
	err := r.db.WithContext(ctx).
		Where("kandang_id = ?", kandangId).
		Where("location_id = ?", locationId).
		Where("deleted_at IS NULL").
		Order("id ASC").
		First(&warehouse).Error
	if err != nil {
		return nil, err
	}
	return &warehouse, nil
}

func (r *WarehouseRepositoryImpl) GetLatestByKandangID(ctx context.Context, kandangId uint) (*entity.Warehouse, error) {
	var warehouse entity.Warehouse
	err := r.db.WithContext(ctx).
		Where("kandang_id = ?", kandangId).
		Where("deleted_at IS NULL").
		Order("id DESC").
		First(&warehouse).Error
	if err != nil {
		return nil, err
	}
	return &warehouse, nil
}
