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 ProductCategoryRelationDTO struct {
	Id   uint   `json:"id"`
	Name string `json:"name"`
	Code string `json:"code"`
}

type ProductCategoryListDTO struct {
	ProductCategoryRelationDTO
	CreatedUser *userDTO.UserRelationDTO `json:"created_user"`
	CreatedAt   time.Time                `json:"created_at"`
	UpdatedAt   time.Time                `json:"updated_at"`
}

type ProductCategoryDetailDTO struct {
	ProductCategoryListDTO
}

// === Mapper Functions ===

func ToProductCategoryRelationDTO(e entity.ProductCategory) ProductCategoryRelationDTO {
	return ProductCategoryRelationDTO{
		Id:   e.Id,
		Name: e.Name,
		Code: e.Code,
	}
}

func ToProductCategoryListDTO(e entity.ProductCategory) ProductCategoryListDTO {
	var createdUser *userDTO.UserRelationDTO
	if e.CreatedUser.Id != 0 {
		mapped := userDTO.ToUserRelationDTO(e.CreatedUser)
		createdUser = &mapped
	}

	return ProductCategoryListDTO{
		ProductCategoryRelationDTO: ToProductCategoryRelationDTO(e),
		CreatedAt:                  e.CreatedAt,
		UpdatedAt:                  e.UpdatedAt,
		CreatedUser:                createdUser,
	}
}

func ToProductCategoryListDTOs(e []entity.ProductCategory) []ProductCategoryListDTO {
	result := make([]ProductCategoryListDTO, len(e))
	for i, r := range e {
		result[i] = ToProductCategoryListDTO(r)
	}
	return result
}

func ToProductCategoryDetailDTO(e entity.ProductCategory) ProductCategoryDetailDTO {
	return ProductCategoryDetailDTO{
		ProductCategoryListDTO: ToProductCategoryListDTO(e),
	}
}
