package dto

import (
	"time"

	entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
	kandangGroupDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/kandang-groups/dto"
)

// === DTO Structs ===

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

type EmployeesListDTO struct {
	Id        uint                                      `json:"id"`
	Name      string                                    `json:"name"`
	IsActive  bool                                      `json:"is_active"`
	Kandangs  []kandangGroupDTO.KandangGroupRelationDTO `json:"kandangs"`
	CreatedAt time.Time                                 `json:"created_at"`
	UpdatedAt time.Time                                 `json:"updated_at"`
}

type EmployeesDetailDTO struct {
	EmployeesListDTO
}

// === Mapper Functions ===

func ToEmployeesRelationDTO(e entity.Employees) EmployeesRelationDTO {
	return EmployeesRelationDTO{
		Id:   e.Id,
		Name: e.Name,
	}
}

func ToEmployeesListDTO(e entity.Employees) EmployeesListDTO {
	kandangs := make([]kandangGroupDTO.KandangGroupRelationDTO, 0, len(e.EmployeeKandangs))
	for _, rel := range e.EmployeeKandangs {
		if rel.Kandang.Id == 0 {
			continue
		}
		kandangs = append(kandangs, kandangGroupDTO.ToKandangGroupRelationDTO(rel.Kandang))
	}

	return EmployeesListDTO{
		Id:        e.Id,
		Name:      e.Name,
		IsActive:  e.IsActive,
		Kandangs:  kandangs,
		CreatedAt: e.CreatedAt,
		UpdatedAt: e.UpdatedAt,
	}
}

func ToEmployeesListDTOs(e []entity.Employees) []EmployeesListDTO {
	result := make([]EmployeesListDTO, len(e))
	for i, r := range e {
		result[i] = ToEmployeesListDTO(r)
	}
	return result
}

func ToEmployeesDetailDTO(e entity.Employees) EmployeesDetailDTO {
	return EmployeesDetailDTO{
		EmployeesListDTO: ToEmployeesListDTO(e),
	}
}
