package dto

import (
	"time"

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

// === DTO Structs ===

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

type UomListDTO struct {
	Id          uint                     `json:"id"`
	Name        string                   `json:"name"`
	CreatedUser *userDTO.UserRelationDTO `json:"created_user"`
	CreatedAt   time.Time                `json:"created_at"`
	UpdatedAt   time.Time                `json:"updated_at"`
}

type UomDetailDTO struct {
	UomListDTO
}

// === Mapper Functions ===

func ToUomRelationDTO(e entity.Uom) UomRelationDTO {
	return UomRelationDTO{
		Id:   e.Id,
		Name: e.Name,
	}
}

func ToUomListDTO(e entity.Uom) UomListDTO {
	var createdUser *userDTO.UserRelationDTO
	if e.CreatedUser.Id != 0 {
		mapped := userDTO.ToUserRelationDTO(e.CreatedUser)
		createdUser = &mapped
	}

	return UomListDTO{
		Id:          e.Id,
		Name:        e.Name,
		CreatedAt:   e.CreatedAt,
		UpdatedAt:   e.UpdatedAt,
		CreatedUser: createdUser,
	}
}

func ToUomListDTOs(e []entity.Uom) []UomListDTO {
	result := make([]UomListDTO, len(e))
	for i, r := range e {
		result[i] = ToUomListDTO(r)
	}
	return result
}

func ToUomDetailDTO(e entity.Uom) UomDetailDTO {
	return UomDetailDTO{
		UomListDTO: ToUomListDTO(e),
	}
}
