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"
)

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

type RecordingKandangDTO struct {
	Id   uint   `json:"id"`
	Name string `json:"name"`
}

type KandangGroupListDTO struct {
	Id                uint                            `json:"id"`
	Name              string                          `json:"name"`
	Status            string                          `json:"status"`
	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"`
	RecordingKandangs []RecordingKandangDTO           `json:"recording_kandangs"`
}

type KandangGroupDetailDTO struct {
	KandangGroupListDTO
}

func ToKandangGroupRelationDTO(e entity.KandangGroup) KandangGroupRelationDTO {
	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 KandangGroupRelationDTO{
		Id:       e.Id,
		Name:     e.Name,
		Status:   e.Status,
		Location: location,
		Pic:      pic,
	}
}

func ToKandangGroupListDTO(e entity.KandangGroup) KandangGroupListDTO {
	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
	}

	recordingKandangs := make([]RecordingKandangDTO, 0, len(e.Kandangs))
	for _, kandang := range e.Kandangs {
		recordingKandangs = append(recordingKandangs, RecordingKandangDTO{
			Id:   kandang.Id,
			Name: kandang.Name,
		})
	}

	return KandangGroupListDTO{
		Id:                e.Id,
		Name:              e.Name,
		Status:            e.Status,
		Location:          location,
		Pic:               pic,
		CreatedAt:         e.CreatedAt,
		UpdatedAt:         e.UpdatedAt,
		CreatedUser:       createdUser,
		RecordingKandangs: recordingKandangs,
	}
}

func ToKandangGroupListDTOs(e []entity.KandangGroup) []KandangGroupListDTO {
	result := make([]KandangGroupListDTO, len(e))
	for i, r := range e {
		result[i] = ToKandangGroupListDTO(r)
	}
	return result
}

func ToKandangGroupDetailDTO(e entity.KandangGroup) KandangGroupDetailDTO {
	return KandangGroupDetailDTO{
		KandangGroupListDTO: ToKandangGroupListDTO(e),
	}
}
