package dto

import (
	"time"

	entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
	areaDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/areas/dto"
	kandangDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/kandangs/dto"
	locationDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/locations/dto"
	productionStandardDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/production-standards/dto"
	warehouseDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/warehouses/dto"
	userDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/users/dto"
)

type KandangWithPivotDTO struct {
	kandangDTO.KandangRelationDTO
	AvailableQuantity float64 `json:"available_quantity"`
}

type ProjectFlockWithPivotDTO struct {
	ProjectFlockRelationDTO
	Area                 *areaDTO.AreaRelationDTO                             `json:"area,omitempty"`
	Category             string                                               `json:"category"`
	StandardFcr          *float64                                             `json:"standard_fcr,omitempty"`
	ProductionStandard   *productionStandardDTO.ProductionStandardRelationDTO `json:"production_standard,omitempty"`
	ProductionStandardId uint                                                 `json:"production_standard_id"`
	Location             *locationDTO.LocationRelationDTO                     `json:"location,omitempty"`
	Kandangs             []KandangWithPivotDTO                                `json:"kandangs,omitempty"`
	CreatedUser          *userDTO.UserRelationDTO                             `json:"created_user,omitempty"`
}

type ProjectFlockKandangDTO struct {
	Id                    uint                               `json:"id"`
	ProjectFlockKandangId uint                               `json:"project_flock_kandang_id"`
	ProjectFlockId        uint                               `json:"project_flock_id"`
	KandangId             uint                               `json:"kandang_id"`
	Kandang               *kandangDTO.KandangRelationDTO     `json:"kandang,omitempty"`
	Warehouse             *warehouseDTO.WarehouseRelationDTO `json:"warehouse,omitempty"`
	ProjectFlock          *ProjectFlockWithPivotDTO          `json:"project_flock,omitempty"`
	AvailableQuantity     float64                            `json:"available_quantity"`
	Population            *float64                           `json:"population,omitempty"`
	ChickInDate           *time.Time                         `json:"chick_in_date,omitempty"`
	IsTransition          bool                               `json:"is_transition"`
	IsLaying              bool                               `json:"is_laying"`
}

func ToProjectFlockKandangDTO(e entity.ProjectFlockKandang) ProjectFlockKandangDTO {
	var kandang *kandangDTO.KandangRelationDTO
	if e.Kandang.Id != 0 {
		mapped := kandangDTO.ToKandangRelationDTO(e.Kandang)
		kandang = &mapped
	}

	var pf *ProjectFlockWithPivotDTO
	if e.ProjectFlock.Id != 0 {

		pfLocal := ProjectFlockWithPivotDTO{
			ProjectFlockRelationDTO: ProjectFlockRelationDTO{
				Id:        e.ProjectFlock.Id,
				Period:    e.Period,
				FlockName: e.ProjectFlock.FlockName,
			},
			Category:             e.ProjectFlock.Category,
			ProductionStandardId: e.ProjectFlock.ProductionStandardId,
		}

		if e.ProjectFlock.Area.Id != 0 {
			mapped := areaDTO.ToAreaRelationDTO(e.ProjectFlock.Area)
			pfLocal.Area = &mapped
		}
		if e.ProjectFlock.ProductionStandard.Id != 0 {
			mapped := productionStandardDTO.ToProductionStandardRelationDTO(e.ProjectFlock.ProductionStandard)
			pfLocal.ProductionStandard = &mapped
		}
		if e.ProjectFlock.Location.Id != 0 {
			mapped := locationDTO.ToLocationRelationDTO(e.ProjectFlock.Location)
			pfLocal.Location = &mapped
		}
		if e.ProjectFlock.CreatedUser.Id != 0 {
			mapped := userDTO.ToUserRelationDTO(e.ProjectFlock.CreatedUser)
			pfLocal.CreatedUser = &mapped
		}
		pfLocal.StandardFcr = resolveProjectFlockStandardFcr(e.ProjectFlock)

		for _, k := range e.ProjectFlock.Kandangs {
			kb := kandangDTO.ToKandangRelationDTO(k)
			pfLocal.Kandangs = append(pfLocal.Kandangs, KandangWithPivotDTO{
				KandangRelationDTO: kb,
				AvailableQuantity:  0,
			})
		}

		pf = &pfLocal
	}

	return ProjectFlockKandangDTO{
		Id:                    e.Id,
		ProjectFlockKandangId: e.Id,
		ProjectFlockId:        e.ProjectFlockId,
		KandangId:             e.KandangId,
		Kandang:               kandang,
		ProjectFlock:          pf,
		AvailableQuantity:     0,
	}
}
