package service

import (
	"errors"

	domain_pig "git.huangwc.com/pig/pig-farm-controller/internal/domain/pig"
)

var (
	ErrHouseContainsPens                = errors.New("无法删除包含猪栏的猪舍")
	ErrHouseNotFound                    = errors.New("指定的猪舍不存在")
	ErrPenInUse                         = errors.New("猪栏正在被活跃批次使用，无法删除")
	ErrPenNotFound                      = errors.New("指定的猪栏不存在")
	ErrPenStatusInvalidForOccupiedPen   = errors.New("猪栏已被批次使用，无法设置为非使用中状态")
	ErrPenStatusInvalidForUnoccupiedPen = errors.New("猪栏未被批次使用，无法设置为使用中状态")
	ErrPigBatchNotFound                 = errors.New("指定的猪批次不存在")
	ErrPigBatchActive                   = errors.New("活跃的猪批次不能被删除")
	ErrPigBatchNotActive                = errors.New("猪批次不处于活跃状态，无法修改关联猪栏")
	ErrPenOccupiedByOtherBatch          = errors.New("猪栏已被其他批次使用")
	ErrPenStatusInvalidForAllocation    = errors.New("猪栏状态不允许分配")
	ErrPenNotAssociatedWithBatch        = errors.New("猪栏未与该批次关联")
	ErrPenNotEmpty                      = errors.New("猪栏内仍有猪只")
	ErrInvalidOperation                 = errors.New("非法操作")
)

// MapDomainError 将领域层的错误转换为应用服务层的公共错误。
func MapDomainError(err error) error {
	if err == nil {
		return nil
	}

	switch {
	case errors.Is(err, domain_pig.ErrPigBatchNotFound):
		return ErrPigBatchNotFound
	case errors.Is(err, domain_pig.ErrPigBatchActive):
		return ErrPigBatchActive
	case errors.Is(err, domain_pig.ErrPigBatchNotActive):
		return ErrPigBatchNotActive
	case errors.Is(err, domain_pig.ErrPenOccupiedByOtherBatch):
		return ErrPenOccupiedByOtherBatch
	case errors.Is(err, domain_pig.ErrPenStatusInvalidForAllocation):
		return ErrPenStatusInvalidForAllocation
	case errors.Is(err, domain_pig.ErrPenNotAssociatedWithBatch):
		return ErrPenNotAssociatedWithBatch
	case errors.Is(err, domain_pig.ErrPenNotFound):
		return ErrPenNotFound
	case errors.Is(err, domain_pig.ErrPenNotEmpty):
		return ErrPenNotEmpty
	case errors.Is(err, domain_pig.ErrInvalidOperation):
		return ErrInvalidOperation
	// 可以添加更多领域错误到应用层错误的映射
	default:
		return err // 对于未知的领域错误，直接返回
	}
}
