package division

import (
	ecore "simrs-vx/internal/domain/base-entities/core"
	edpb "simrs-vx/internal/domain/main-entities/division-position/base"
)

type CreateDto struct {
	Id          *uint   `json:"id"`
	Code        string  `json:"code" validate:"maxLength=10"`
	Name        string  `json:"name" validate:"maxLength=50"`
	Parent_Code *string `json:"parent_code"`
}

type ReadListDto struct {
	FilterDto
	Includes         string `json:"includes"`
	Sort             string `json:"sort"`
	Pagination       ecore.Pagination
	OnlyHaveChildren bool `json:"only-have-children"`
}

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

type ReadDetailDto struct {
	Id       *uint16 `json:"id"`
	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
	Code             string       `json:"code"`
	Name             string       `json:"name"`
	Parent_Code      *string      `json:"parent_code"`
	Parent           *Division    `json:"parent,omitempty"`
	Childrens        []Division   `json:"childrens,omitempty"`
	DivisionPosition []edpb.Basic `json:"divisionPositions,omitempty"`
}

func (d Division) ToResponse() ResponseDto {
	resp := ResponseDto{
		Code:             d.Code,
		Name:             d.Name,
		Parent_Code:      d.Parent_Code,
		Parent:           d.Parent,
		Childrens:        d.Childrens,
		DivisionPosition: d.DivisionPositions,
	}
	resp.SmallMain = d.SmallMain
	return resp
}

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