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

type FcrStandardDTO struct {
	Id        uint    `json:"id"`
	Weight    float64 `json:"weight"`
	FcrNumber float64 `json:"fcr_number"`
	Mortality float64 `json:"mortality"`
}

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

type FcrDetailDTO struct {
	FcrListDTO
	Standards []FcrStandardDTO `json:"fcr_standards"`
}

// === Mapper Functions ===

func ToFcrRelationDTO(e entity.Fcr) FcrRelationDTO {
	return FcrRelationDTO{
		Id:   e.Id,
		Name: e.Name,
	}
}

func ToFcrListDTO(e entity.Fcr) FcrListDTO {
	var createdUser *userDTO.UserRelationDTO
	if e.CreatedUser.Id != 0 {
		mapped := userDTO.ToUserRelationDTO(e.CreatedUser)
		createdUser = &mapped
	}

	return FcrListDTO{
		FcrRelationDTO: ToFcrRelationDTO(e),
		CreatedAt:      e.CreatedAt,
		UpdatedAt:      e.UpdatedAt,
		CreatedUser:    createdUser,
	}
}

func ToFcrListDTOs(e []entity.Fcr) []FcrListDTO {
	result := make([]FcrListDTO, len(e))
	for i, r := range e {
		result[i] = ToFcrListDTO(r)
	}
	return result
}

func ToFcrDetailDTO(e entity.Fcr) FcrDetailDTO {
	return FcrDetailDTO{
		FcrListDTO: ToFcrListDTO(e),
		Standards:  ToFcrStandardDTOs(e.Standards),
	}
}

func ToFcrStandardDTOs(standards []entity.FcrStandard) []FcrStandardDTO {
	result := make([]FcrStandardDTO, len(standards))
	for i, s := range standards {
		result[i] = FcrStandardDTO{
			Id:        s.Id,
			Weight:    s.Weight,
			FcrNumber: s.FcrNumber,
			Mortality: s.Mortality,
		}
	}
	return result
}
