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"
	userDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/users/dto"
)

// === DTO Structs ===

type WarehouseRelationDTO struct {
	Id       uint                             `json:"id"`
	Name     string                           `json:"name"`
	Type     string                           `json:"type"`
	Area     *areaDTO.AreaRelationDTO         `json:"area,omitempty"`
	Location *locationDTO.LocationRelationDTO `json:"location,omitempty"`
	Kandang  *kandangDTO.KandangRelationDTO   `json:"kandang,omitempty"`
}

type WarehouseListDTO struct {
	Id          uint                             `json:"id"`
	Name        string                           `json:"name"`
	Type        string                           `json:"type"`
	Area        *areaDTO.AreaRelationDTO         `json:"area"`
	Location    *locationDTO.LocationRelationDTO `json:"location"`
	Kandang     *kandangDTO.KandangRelationDTO   `json:"kandang"`
	CreatedUser *userDTO.UserRelationDTO         `json:"created_user"`
	CreatedAt   time.Time                        `json:"created_at"`
	UpdatedAt   time.Time                        `json:"updated_at"`
}

type WarehouseDetailDTO struct {
	WarehouseListDTO
}

// === Mapper Functions ===

func ToWarehouseRelationDTO(e entity.Warehouse) WarehouseRelationDTO {
	var area *areaDTO.AreaRelationDTO
	if e.Area.Id != 0 {
		mapped := areaDTO.ToAreaRelationDTO(e.Area)
		area = &mapped
	}

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

	var kandang *kandangDTO.KandangRelationDTO
	if e.Kandang != nil && e.Kandang.Id != 0 {
		mapped := kandangDTO.ToKandangRelationDTO(*e.Kandang)
		kandang = &mapped
	}

	return WarehouseRelationDTO{
		Id:       e.Id,
		Name:     e.Name,
		Type:     e.Type,
		Area:     area,
		Location: location,
		Kandang:  kandang,
	}
}

func ToWarehouseListDTO(e entity.Warehouse) WarehouseListDTO {
	var createdUser *userDTO.UserRelationDTO
	if e.CreatedUser.Id != 0 {
		mapped := userDTO.ToUserRelationDTO(e.CreatedUser)
		createdUser = &mapped
	}

	var area *areaDTO.AreaRelationDTO
	if e.Area.Id != 0 {
		mapped := areaDTO.ToAreaRelationDTO(e.Area)
		area = &mapped
	}

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

	var kandang *kandangDTO.KandangRelationDTO
	if e.Kandang != nil && e.Kandang.Id != 0 {
		mapped := kandangDTO.ToKandangRelationDTO(*e.Kandang)
		kandang = &mapped
	}

	return WarehouseListDTO{
		Id:          e.Id,
		Name:        e.Name,
		Type:        e.Type,
		Area:        area,
		Location:    location,
		Kandang:     kandang,
		CreatedAt:   e.CreatedAt,
		UpdatedAt:   e.UpdatedAt,
		CreatedUser: createdUser,
	}
}

func ToWarehouseListDTOs(e []entity.Warehouse) []WarehouseListDTO {
	result := make([]WarehouseListDTO, len(e))
	for i, r := range e {
		result[i] = ToWarehouseListDTO(r)
	}
	return result
}

func ToWarehouseDetailDTO(e entity.Warehouse) WarehouseDetailDTO {
	return WarehouseDetailDTO{
		WarehouseListDTO: ToWarehouseListDTO(e),
	}
}
