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

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

type AreaDetailDTO struct {
	AreaListDTO
}

// === Mapper Functions ===

func ToAreaBaseDTO(e entity.Area) AreaBaseDTO {
	return AreaBaseDTO{
		Id:   e.Id,
		Name: e.Name,
	}
}

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

	return AreaListDTO{
		AreaBaseDTO: ToAreaBaseDTO(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),
	}
}
