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

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

type FlockDetailDTO struct {
	FlockListDTO
}

// === Mapper Functions ===

func ToFlockRelationDTO(e entity.Flock) FlockRelationDTO {
	return FlockRelationDTO{
		Id:   e.Id,
		Name: e.Name,
	}
}

func ToFlockListDTO(e entity.Flock) FlockListDTO {
	var createdUser *userDTO.UserRelationDTO
	if e.CreatedUser.Id != 0 {
		mapped := userDTO.ToUserRelationDTO(e.CreatedUser)
		createdUser = &mapped
	}

	return FlockListDTO{
		FlockRelationDTO: ToFlockRelationDTO(e),
		CreatedAt:        e.CreatedAt,
		UpdatedAt:        e.UpdatedAt,
		CreatedUser:      createdUser,
	}
}

func ToFlockListDTOs(e []entity.Flock) []FlockListDTO {
	result := make([]FlockListDTO, len(e))
	for i, r := range e {
		result[i] = ToFlockListDTO(r)
	}
	return result
}

func ToFlockDetailDTO(e entity.Flock) FlockDetailDTO {
	return FlockDetailDTO{
		FlockListDTO: ToFlockListDTO(e),
	}
}
