package dto

import (
	entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
	areaDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/areas/dto"
	fcrDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/fcrs/dto"
	flockDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/flocks/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"
	userDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/users/dto"
)

// internal DTO used only for lookup response: project flock with kandangs carrying pivot ids
type KandangWithPivotDTO struct {
	kandangDTO.KandangBaseDTO
	ProjectFlockKandangId *uint `json:"project_flock_kandang_id,omitempty"`
}

type ProjectFlockWithPivotDTO struct {
	ProjectFlockBaseDTO
	Flock       *flockDTO.FlockBaseDTO       `json:"flock,omitempty"`
	Area        *areaDTO.AreaBaseDTO         `json:"area,omitempty"`
	Category    string                       `json:"category"`
	Fcr         *fcrDTO.FcrBaseDTO           `json:"fcr,omitempty"`
	Location    *locationDTO.LocationBaseDTO `json:"location,omitempty"`
	Kandangs    []KandangWithPivotDTO        `json:"kandangs,omitempty"`
	CreatedUser *userDTO.UserBaseDTO         `json:"created_user,omitempty"`
}

type ProjectFlockKandangDTO struct {
	Id             uint                       `json:"id"`
	ProjectFlockId uint                       `json:"project_flock_id"`
	KandangId      uint                       `json:"kandang_id"`
	Kandang        *kandangDTO.KandangBaseDTO `json:"kandang,omitempty"`
	ProjectFlock   *ProjectFlockWithPivotDTO  `json:"project_flock,omitempty"`
}

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

	var pf *ProjectFlockWithPivotDTO
	if e.ProjectFlock.Id != 0 {
		// build project flock with kandangs that include pivot ids
		pfLocal := ProjectFlockWithPivotDTO{
			ProjectFlockBaseDTO: ProjectFlockBaseDTO{
				Id:     e.ProjectFlock.Id,
				Period: e.ProjectFlock.Period,
			},
			Category: e.ProjectFlock.Category,
		}

		// fill related small summaries
		if e.ProjectFlock.Flock.Id != 0 {
			mapped := ToFlockSummaryDTO(e.ProjectFlock.Flock)
			pfLocal.Flock = &mapped
		}
		if e.ProjectFlock.Area.Id != 0 {
			mapped := areaDTO.ToAreaBaseDTO(e.ProjectFlock.Area)
			pfLocal.Area = &mapped
		}
		if e.ProjectFlock.Fcr.Id != 0 {
			mapped := fcrDTO.ToFcrBaseDTO(e.ProjectFlock.Fcr)
			pfLocal.Fcr = &mapped
		}
		if e.ProjectFlock.Location.Id != 0 {
			mapped := locationDTO.ToLocationBaseDTO(e.ProjectFlock.Location)
			pfLocal.Location = &mapped
		}
		if e.ProjectFlock.CreatedUser.Id != 0 {
			mapped := userDTO.ToUserBaseDTO(e.ProjectFlock.CreatedUser)
			pfLocal.CreatedUser = &mapped
		}

		// build pivot map
		pivotMap := make(map[uint]uint)
		for _, ph := range e.ProjectFlock.KandangHistory {
			pivotMap[ph.KandangId] = ph.Id
		}

		// populate kandangs with pivot ids
		for _, k := range e.ProjectFlock.Kandangs {
			kb := kandangDTO.ToKandangBaseDTO(k)
			var pid *uint
			if v, ok := pivotMap[k.Id]; ok {
				vv := v
				pid = &vv
			}
			pfLocal.Kandangs = append(pfLocal.Kandangs, KandangWithPivotDTO{
				KandangBaseDTO:        kb,
				ProjectFlockKandangId: pid,
			})
		}

		pf = &pfLocal
	}

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