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
	}
	return &NutrientResponse{
		ID:          nutrient.ID,
		CreatedAt:   nutrient.CreatedAt,
		UpdatedAt:   nutrient.UpdatedAt,
		Name:        nutrient.Name,
		Description: nutrient.Description,
	}
}

// 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,
			CreatedAt:  rmn.CreatedAt,
			UpdatedAt:  rmn.UpdatedAt,
			NutrientID: rmn.NutrientID,
			Nutrient:   rmn.Nutrient.Name, // 假设 Nutrient 已经被预加载
			Value:      rmn.Value,
		}
	}

	return &RawMaterialResponse{
		ID:                   rm.ID,
		CreatedAt:            rm.CreatedAt,
		UpdatedAt:            rm.UpdatedAt,
		Name:                 rm.Name,
		Description:          rm.Description,
		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,
		CreatedAt:          breed.CreatedAt,
		UpdatedAt:          breed.UpdatedAt,
		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,
		CreatedAt:   ageStage.CreatedAt,
		UpdatedAt:   ageStage.UpdatedAt,
		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,
			CreatedAt:      pnr.CreatedAt,
			UpdatedAt:      pnr.UpdatedAt,
			NutrientID:     pnr.NutrientID,
			NutrientName:   pnr.Nutrient.Name, // 假设 Nutrient 已经被预加载
			MinRequirement: pnr.MinRequirement,
			MaxRequirement: pnr.MaxRequirement,
		}
	}

	return &PigTypeResponse{
		ID:                      pt.ID,
		CreatedAt:               pt.CreatedAt,
		UpdatedAt:               pt.UpdatedAt,
		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,
		},
	}
}
