package dto

import (
	"time"

	entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
	locationDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/locations/dto"
	userDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/users/dto"
)

// === DTO Structs ===

type KandangRelationDTO struct {
	Id           uint                             `json:"id"`
	Name         string                           `json:"name"`
	Status       string                           `json:"status"`
	Capacity     float64                          `json:"capacity"`
	KandangGroup *KandangGroupRelationDTO         `json:"kandang_group,omitempty"`
	Location     *locationDTO.LocationRelationDTO `json:"location,omitempty"`
	Pic          *userDTO.UserRelationDTO         `json:"pic,omitempty"`
}

type KandangGroupRelationDTO struct {
	Id     uint   `json:"id"`
	Name   string `json:"name"`
	Status string `json:"status"`
}

type KandangListDTO struct {
	Id           uint                            `json:"id"`
	Name         string                          `json:"name"`
	Status       string                          `json:"status"`
	Capacity     float64                         `json:"capacity"`
	KandangGroup KandangGroupRelationDTO         `json:"kandang_group"`
	Location     locationDTO.LocationRelationDTO `json:"location"`
	Pic          userDTO.UserRelationDTO         `json:"pic"`
	CreatedUser  userDTO.UserRelationDTO         `json:"created_user"`
	CreatedAt    time.Time                       `json:"created_at"`
	UpdatedAt    time.Time                       `json:"updated_at"`
}

type KandangDetailDTO struct {
	KandangListDTO
}

// === Mapper Functions ===

func ToKandangRelationDTO(e entity.Kandang) KandangRelationDTO {
	var kandangGroup *KandangGroupRelationDTO
	if e.KandangGroup.Id != 0 {
		mapped := ToKandangGroupRelationDTO(e.KandangGroup)
		kandangGroup = &mapped
	}

	var location *locationDTO.LocationRelationDTO
	if e.Location.Id != 0 {
		mapped := locationDTO.ToLocationRelationDTO(e.Location)
		location = &mapped
	}

	var pic *userDTO.UserRelationDTO
	if e.Pic.Id != 0 {
		mapped := userDTO.ToUserRelationDTO(e.Pic)
		pic = &mapped
	}

	return KandangRelationDTO{
		Id:           e.Id,
		Name:         e.Name,
		Status:       e.Status,
		Capacity:     e.Capacity,
		KandangGroup: kandangGroup,
		Location:     location,
		Pic:          pic,
	}
}

func ToKandangGroupRelationDTO(e entity.KandangGroup) KandangGroupRelationDTO {
	return KandangGroupRelationDTO{
		Id:     e.Id,
		Name:   e.Name,
		Status: e.Status,
	}
}

func ToKandangListDTO(e entity.Kandang) KandangListDTO {
	var kandangGroup KandangGroupRelationDTO
	if e.KandangGroup.Id != 0 {
		mapped := ToKandangGroupRelationDTO(e.KandangGroup)
		kandangGroup = mapped
	}

	var location locationDTO.LocationRelationDTO
	if e.Location.Id != 0 {
		mapped := locationDTO.ToLocationRelationDTO(e.Location)
		location = mapped
	}

	var pic userDTO.UserRelationDTO
	if e.Pic.Id != 0 {
		mapped := userDTO.ToUserRelationDTO(e.Pic)
		pic = mapped
	}

	var createdUser userDTO.UserRelationDTO
	if e.CreatedUser.Id != 0 {
		mapped := userDTO.ToUserRelationDTO(e.CreatedUser)
		createdUser = mapped
	}

	return KandangListDTO{
		Id:           e.Id,
		Name:         e.Name,
		Status:       e.Status,
		Location:     location,
		KandangGroup: kandangGroup,
		Capacity:     e.Capacity,
		Pic:          pic,
		CreatedAt:    e.CreatedAt,
		UpdatedAt:    e.UpdatedAt,
		CreatedUser:  createdUser,
	}
}

func ToKandangListDTOs(e []entity.Kandang) []KandangListDTO {
	result := make([]KandangListDTO, len(e))
	for i, r := range e {
		result[i] = ToKandangListDTO(r)
	}
	return result
}

func ToKandangDetailDTO(e entity.Kandang) KandangDetailDTO {
	return KandangDetailDTO{
		KandangListDTO: ToKandangListDTO(e),
	}
}
