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

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

type AreaDetailDTO struct {
	AreaListDTO
}

// === Mapper Functions ===

func ToAreaRelationDTO(e entity.Area) AreaRelationDTO {
	return AreaRelationDTO{
		Id:   e.Id,
		Name: e.Name,
	}
}

func ToAreaListDTO(e entity.Area) AreaListDTO {
	var createdUser *userDTO.UserRelationDTO
	if e.CreatedUser.Id != 0 {
		mapped := userDTO.ToUserRelationDTO(e.CreatedUser)
		createdUser = &mapped
	}

	return AreaListDTO{
		AreaRelationDTO: ToAreaRelationDTO(e),
		CreatedUser:     createdUser,
		CreatedAt:       e.CreatedAt,
		UpdatedAt:       e.UpdatedAt,
	}
}

func ToAreaListDTOs(e []entity.Area) []AreaListDTO {
	result := make([]AreaListDTO, len(e))
	for i, r := range e {
		result[i] = ToAreaListDTO(r)
	}
	return result
}

func ToAreaDetailDTO(e entity.Area) AreaDetailDTO {
	return AreaDetailDTO{
		AreaListDTO: ToAreaListDTO(e),
	}
}
