package dto

import (
	"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
)

// ConvertNutrientToDTO 将 models.Nutrient 转换为 NutrientResponse DTO
func ConvertNutrientToDTO(nutrient *models.Nutrient) *NutrientResponse {
	if nutrient == nil {
		return nil
	}

	rawMaterials := make([]NutrientRawMaterialDTO, 0, len(nutrient.RawMaterialNutrients))
	for _, rmn := range nutrient.RawMaterialNutrients {
		// 根据您的反馈，移除了不必要的 nil 检查，以保持代码简洁和一致性
		rawMaterials = append(rawMaterials, NutrientRawMaterialDTO{
			ID:    rmn.RawMaterial.ID,
			Name:  rmn.RawMaterial.Name,
			Value: rmn.Value,
		})
	}

	return &NutrientResponse{
		ID:           nutrient.ID,
		Name:         nutrient.Name,
		Description:  nutrient.Description,
		RawMaterials: rawMaterials,
	}
}

// ConvertNutrientListToDTO 将 []models.Nutrient 转换为 ListNutrientResponse DTO
func ConvertNutrientListToDTO(nutrients []models.Nutrient, total int64, page, pageSize int) *ListNutrientResponse {
	nutrientDTOs := make([]NutrientResponse, len(nutrients))
	for i, n := range nutrients {
		nutrientDTOs[i] = *ConvertNutrientToDTO(&n)
	}

	return &ListNutrientResponse{
		List: nutrientDTOs,
		Pagination: PaginationDTO{
			Page:     page,
			PageSize: pageSize,
			Total:    total,
		},
	}
}

// ConvertRawMaterialToDTO 将 models.RawMaterial 转换为 RawMaterialResponse DTO
func ConvertRawMaterialToDTO(rm *models.RawMaterial) *RawMaterialResponse {
	if rm == nil {
		return nil
	}

	rawMaterialNutrientDTOs := make([]RawMaterialNutrientDTO, len(rm.RawMaterialNutrients))
	for i, rmn := range rm.RawMaterialNutrients {
		rawMaterialNutrientDTOs[i] = RawMaterialNutrientDTO{
			ID:         rmn.ID,
			NutrientID: rmn.NutrientID,
			Nutrient:   rmn.Nutrient.Name, // 假设 Nutrient 已经被预加载
			Value:      rmn.Value,
		}
	}

	return &RawMaterialResponse{
		ID:                   rm.ID,
		Name:                 rm.Name,
		Description:          rm.Description,
		ReferencePrice:       rm.ReferencePrice,
		MaxAdditionRatio:     rm.MaxAdditionRatio,
		RawMaterialNutrients: rawMaterialNutrientDTOs,
	}
}

// ConvertRawMaterialListToDTO 将 []models.RawMaterial 转换为 ListRawMaterialResponse DTO
func ConvertRawMaterialListToDTO(rawMaterials []models.RawMaterial, total int64, page, pageSize int) *ListRawMaterialResponse {
	rawMaterialDTOs := make([]RawMaterialResponse, len(rawMaterials))
	for i, rm := range rawMaterials {
		rawMaterialDTOs[i] = *ConvertRawMaterialToDTO(&rm)
	}

	return &ListRawMaterialResponse{
		List: rawMaterialDTOs,
		Pagination: PaginationDTO{
			Page:     page,
			PageSize: pageSize,
			Total:    total,
		},
	}
}

// ConvertPigBreedToDTO 将 models.PigBreed 转换为 PigBreedResponse DTO
func ConvertPigBreedToDTO(breed *models.PigBreed) *PigBreedResponse {
	if breed == nil {
		return nil
	}
	return &PigBreedResponse{
		ID:                 breed.ID,
		Name:               breed.Name,
		Description:        breed.Description,
		ParentInfo:         breed.ParentInfo,
		AppearanceFeatures: breed.AppearanceFeatures,
		BreedAdvantages:    breed.BreedAdvantages,
		BreedDisadvantages: breed.BreedDisadvantages,
	}
}

// ConvertPigBreedListToDTO 将 []models.PigBreed 转换为 ListPigBreedResponse DTO
func ConvertPigBreedListToDTO(breeds []models.PigBreed, total int64, page, pageSize int) *ListPigBreedResponse {
	breedDTOs := make([]PigBreedResponse, len(breeds))
	for i, b := range breeds {
		breedDTOs[i] = *ConvertPigBreedToDTO(&b)
	}

	return &ListPigBreedResponse{
		List: breedDTOs,
		Pagination: PaginationDTO{
			Page:     page,
			PageSize: pageSize,
			Total:    total,
		},
	}
}

// ConvertPigAgeStageToDTO 将 models.PigAgeStage 转换为 PigAgeStageResponse DTO
func ConvertPigAgeStageToDTO(ageStage *models.PigAgeStage) *PigAgeStageResponse {
	if ageStage == nil {
		return nil
	}
	return &PigAgeStageResponse{
		ID:          ageStage.ID,
		Name:        ageStage.Name,
		Description: ageStage.Description,
	}
}

// ConvertPigAgeStageListToDTO 将 []models.PigAgeStage 转换为 ListPigAgeStageResponse DTO
func ConvertPigAgeStageListToDTO(ageStages []models.PigAgeStage, total int64, page, pageSize int) *ListPigAgeStageResponse {
	ageStageDTOs := make([]PigAgeStageResponse, len(ageStages))
	for i, as := range ageStages {
		ageStageDTOs[i] = *ConvertPigAgeStageToDTO(&as)
	}

	return &ListPigAgeStageResponse{
		List: ageStageDTOs,
		Pagination: PaginationDTO{
			Page:     page,
			PageSize: pageSize,
			Total:    total,
		},
	}
}

// ConvertPigTypeToDTO 将 models.PigType 转换为 PigTypeResponse DTO
func ConvertPigTypeToDTO(pt *models.PigType) *PigTypeResponse {
	if pt == nil {
		return nil
	}

	pigNutrientRequirementDTOs := make([]PigNutrientRequirementDTO, len(pt.PigNutrientRequirements))
	for i, pnr := range pt.PigNutrientRequirements {
		pigNutrientRequirementDTOs[i] = PigNutrientRequirementDTO{
			ID:             pnr.ID,
			NutrientID:     pnr.NutrientID,
			NutrientName:   pnr.Nutrient.Name, // 假设 Nutrient 已经被预加载
			MinRequirement: pnr.MinRequirement,
			MaxRequirement: pnr.MaxRequirement,
		}
	}

	return &PigTypeResponse{
		ID:                      pt.ID,
		BreedID:                 pt.BreedID,
		BreedName:               pt.Breed.Name, // 假设 Breed 已经被预加载
		AgeStageID:              pt.AgeStageID,
		AgeStageName:            pt.AgeStage.Name, // 假设 AgeStage 已经被预加载
		Description:             pt.Description,
		DailyFeedIntake:         pt.DailyFeedIntake,
		DailyGainWeight:         pt.DailyGainWeight,
		MinDays:                 pt.MinDays,
		MaxDays:                 pt.MaxDays,
		MinWeight:               pt.MinWeight,
		MaxWeight:               pt.MaxWeight,
		PigNutrientRequirements: pigNutrientRequirementDTOs,
	}
}

// ConvertPigTypeListToDTO 将 []models.PigType 转换为 ListPigTypeResponse DTO
func ConvertPigTypeListToDTO(pigTypes []models.PigType, total int64, page, pageSize int) *ListPigTypeResponse {
	pigTypeDTOs := make([]PigTypeResponse, len(pigTypes))
	for i, pt := range pigTypes {
		pigTypeDTOs[i] = *ConvertPigTypeToDTO(&pt)
	}

	return &ListPigTypeResponse{
		List: pigTypeDTOs,
		Pagination: PaginationDTO{
			Page:     page,
			PageSize: pageSize,
			Total:    total,
		},
	}
}

// ConvertRecipeToDto 将 models.Recipe 转换为 RecipeResponse DTO
func ConvertRecipeToDto(recipe *models.Recipe) *RecipeResponse {
	if recipe == nil {
		return nil
	}

	ingredients := make([]RecipeIngredientDto, len(recipe.RecipeIngredients))
	for i, ri := range recipe.RecipeIngredients {
		ingredients[i] = RecipeIngredientDto{
			RawMaterialID: ri.RawMaterialID,
			Percentage:    ri.Percentage,
		}
	}

	return &RecipeResponse{
		ID:                recipe.ID,
		Name:              recipe.Name,
		Description:       recipe.Description,
		RecipeIngredients: ingredients,
	}
}

// ConvertRecipeListToDTO 将 []models.Recipe 转换为 ListRecipeResponse DTO
func ConvertRecipeListToDTO(recipes []models.Recipe, total int64, page, pageSize int) *ListRecipeResponse {
	recipeDTOs := make([]RecipeResponse, len(recipes))
	for i, r := range recipes {
		recipeDTOs[i] = *ConvertRecipeToDto(&r)
	}

	return &ListRecipeResponse{
		List: recipeDTOs,
		Pagination: PaginationDTO{
			Page:     page,
			PageSize: pageSize,
			Total:    total,
		},
	}
}

// ConvertCreateRecipeRequestToModel 将 CreateRecipeRequest DTO 转换为 models.Recipe 模型
func ConvertCreateRecipeRequestToModel(req *CreateRecipeRequest) *models.Recipe {
	if req == nil {
		return nil
	}

	ingredients := make([]models.RecipeIngredient, len(req.RecipeIngredients))
	for i, ri := range req.RecipeIngredients {
		ingredients[i] = models.RecipeIngredient{
			RawMaterialID: ri.RawMaterialID,
			Percentage:    ri.Percentage,
		}
	}

	return &models.Recipe{
		Name:              req.Name,
		Description:       req.Description,
		RecipeIngredients: ingredients,
	}
}

// ConvertUpdateRecipeRequestToModel 将 UpdateRecipeRequest DTO 转换为 models.Recipe 模型
func ConvertUpdateRecipeRequestToModel(req *UpdateRecipeRequest) *models.Recipe {
	if req == nil {
		return nil
	}

	ingredients := make([]models.RecipeIngredient, len(req.RecipeIngredients))
	for i, ri := range req.RecipeIngredients {
		ingredients[i] = models.RecipeIngredient{
			RawMaterialID: ri.RawMaterialID,
			Percentage:    ri.Percentage,
		}
	}

	return &models.Recipe{
		Name:              req.Name,
		Description:       req.Description,
		RecipeIngredients: ingredients,
	}
}

// ToGenerateRecipeResponse 将 models.Recipe 转换为 GenerateRecipeResponse DTO
func ToGenerateRecipeResponse(recipe *models.Recipe) *GenerateRecipeResponse {
	if recipe == nil {
		return nil
	}
	return &GenerateRecipeResponse{
		ID:          recipe.ID,
		Name:        recipe.Name,
		Description: recipe.Description,
	}
}
