package repository

import (
	"context"

	"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
	"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"

	"gorm.io/gorm"
)

// PigFarmRepository 定义了与猪场资产（猪舍、猪栏）相关的数据库操作接口
type PigFarmRepository interface {
	// PigHouse methods
	CreatePigHouse(ctx context.Context, house *models.PigHouse) error
	GetPigHouseByID(ctx context.Context, id uint32) (*models.PigHouse, error)
	ListPigHouses(ctx context.Context) ([]models.PigHouse, error)
	// UpdatePigHouse 更新一个猪舍，返回受影响的行数和错误
	UpdatePigHouse(ctx context.Context, house *models.PigHouse) (int64, error)
	// DeletePigHouse 根据ID删除一个猪舍，返回受影响的行数和错误
	DeletePigHouse(ctx context.Context, id uint32) (int64, error)
	CountPensInHouse(ctx context.Context, houseID uint32) (int64, error)
}

// gormPigFarmRepository 是 PigFarmRepository 的 GORM 实现
type gormPigFarmRepository struct {
	ctx context.Context
	db  *gorm.DB
}

// NewGormPigFarmRepository 创建一个新的 PigFarmRepository GORM 实现实例
func NewGormPigFarmRepository(ctx context.Context, db *gorm.DB) PigFarmRepository {
	return &gormPigFarmRepository{ctx: ctx, db: db}
}

// --- PigHouse Implementation ---

// CreatePigHouse 创建一个新的猪舍
func (r *gormPigFarmRepository) CreatePigHouse(ctx context.Context, house *models.PigHouse) error {
	repoCtx := logs.AddFuncName(ctx, r.ctx, "CreatePigHouse")
	return r.db.WithContext(repoCtx).Create(house).Error
}

// GetPigHouseByID 根据ID获取单个猪舍
func (r *gormPigFarmRepository) GetPigHouseByID(ctx context.Context, id uint32) (*models.PigHouse, error) {
	repoCtx := logs.AddFuncName(ctx, r.ctx, "GetPigHouseByID")
	var house models.PigHouse
	if err := r.db.WithContext(repoCtx).First(&house, id).Error; err != nil {
		return nil, err
	}
	return &house, nil
}

// ListPigHouses 列出所有猪舍
func (r *gormPigFarmRepository) ListPigHouses(ctx context.Context) ([]models.PigHouse, error) {
	repoCtx := logs.AddFuncName(ctx, r.ctx, "ListPigHouses")
	var houses []models.PigHouse
	if err := r.db.WithContext(repoCtx).Find(&houses).Error; err != nil {
		return nil, err
	}
	return houses, nil
}

// UpdatePigHouse 更新一个猪舍，返回受影响的行数和错误
func (r *gormPigFarmRepository) UpdatePigHouse(ctx context.Context, house *models.PigHouse) (int64, error) {
	repoCtx := logs.AddFuncName(ctx, r.ctx, "UpdatePigHouse")
	result := r.db.WithContext(repoCtx).Model(&models.PigHouse{}).Where("id = ?", house.ID).Updates(house)
	if result.Error != nil {
		return 0, result.Error
	}
	return result.RowsAffected, nil
}

// DeletePigHouse 根据ID删除一个猪舍，返回受影响的行数和错误
func (r *gormPigFarmRepository) DeletePigHouse(ctx context.Context, id uint32) (int64, error) {
	repoCtx := logs.AddFuncName(ctx, r.ctx, "DeletePigHouse")
	result := r.db.WithContext(repoCtx).Delete(&models.PigHouse{}, id)
	if result.Error != nil {
		return 0, result.Error
	}
	return result.RowsAffected, nil
}

// CountPensInHouse 统计猪舍中的猪栏数量
func (r *gormPigFarmRepository) CountPensInHouse(ctx context.Context, houseID uint32) (int64, error) {
	repoCtx := logs.AddFuncName(ctx, r.ctx, "CountPensInHouse")
	var count int64
	err := r.db.WithContext(repoCtx).Model(&models.Pen{}).Where("house_id = ?", houseID).Count(&count).Error
	return count, err
}
