package unit

import (
	ecore "simrs-vx/internal/domain/base-entities/core"
	ei "simrs-vx/internal/domain/main-entities/installation"
	eipb "simrs-vx/internal/domain/main-entities/unit-position/base"
)

type CreateDto struct {
	Id                *uint   `json:"id"`
	Installation_Code *string `json:"installation_code"`
	Code              string  `json:"code" validate:"maxLength=20"`
	Name              string  `json:"name" validate:"maxLength=50"`
}

type ReadListDto struct {
	FilterDto
	Includes   string `json:"includes"`
	Sort       string `json:"sort"`
	Pagination ecore.Pagination
}

type FilterDto struct {
	Installation_Code *string `json:"installation-code"`
	Code              string  `json:"code"`
	Name              string  `json:"name"`
	Search            string  `json:"search" gormhelper:"searchColumns=Code,Name"`
}

type ReadDetailDto struct {
	Id                *uint16 `json:"id"`
	Installation_Code *string `json:"installation_code"`
	Code              *string `json:"code"`
	Includes          string  `json:"includes"`
}

type UpdateDto struct {
	Id *uint16 `json:"id"`
	CreateDto
}

type DeleteDto struct {
	Id   *uint16 `json:"id"`
	Code *string `json:"code"`
}

type MetaDto struct {
	PageNumber int `json:"page_number"`
	PageSize   int `json:"page_size"`
	Count      int `json:"count"`
}

type ResponseDto struct {
	ecore.SmallMain
	Installation_Code *string          `json:"installation_code"`
	Installation      *ei.Installation `json:"installation,omitempty"`
	Code              string           `json:"code"`
	Name              string           `json:"name"`
	UnitPositions     []eipb.Basic     `json:"unitPositions,omitempty"`
}

func (d Unit) ToResponse() ResponseDto {
	resp := ResponseDto{
		Installation_Code: d.Installation_Code,
		Code:              d.Code,
		Name:              d.Name,
		UnitPositions:     d.UnitPositions,
	}
	resp.SmallMain = d.SmallMain
	if d.Installation != nil {
		resp.Installation = d.Installation
	}
	return resp
}

func ToResponseList(data []Unit) []ResponseDto {
	resp := make([]ResponseDto, len(data))
	for i, u := range data {
		resp[i] = u.ToResponse()
	}
	return resp
}
