package infra

import (
	ecore "simrs-vx/internal/domain/base-entities/core"
	ei "simrs-vx/internal/domain/main-entities/item"

	erb "simrs-vx/internal/domain/main-entities/procedure-room/base"

	ero "simrs-vx/internal/domain/references/organization"
)

type CreateDto struct {
	Code               string             `json:"code" validate:"maxLength=20"`
	Name               string             `json:"name" validate:"maxLength=50"`
	InfraGroup_Code    ero.InfraGroupCode `json:"infraGroup_code" validate:"maxLength=20"`
	Parent_Code        *string            `json:"parent_code"`
	Item_Code          *string            `json:"-"`
	Unit_Code          *string            `json:"unit_code"`
	Specialist_Code    *string            `json:"specialist_code"`
	Subspecialist_Code *string            `json:"subspecialist_code"`
	Infra_Code         *string            `json:"infra_code"` // for room
}

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"`
	InfraGroup_Code ero.InfraGroupCode `json:"infra-group-code"`
	Parent_Code     *string            `json:"parent-code"`
	Item_Id         *string            `json:"item-code"`
	Search          string             `json:"search" gormhelper:"searchColumns=Code,Name"`
}

type ReadDetailDto struct {
	Id        *uint16 `json:"id"`
	Code      *string `json:"code"`
	Item_Code *string `json:"item_code"`
}

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"`
	InfraGroup_Code ero.InfraGroupCode `json:"infraGroup_code"`
	Parent_Code     *string            `json:"parent_code"`
	Parent          *Infra             `json:"parent,omitempty"`
	Childrens       []Infra            `json:"childrens,omitempty"`
	Item_Code       *string            `json:"item_code"`
	Item            *ei.Item           `json:"item,omitempty"`
	ProcedureRooms  *erb.ProcedureRoom `json:"rooms,omitempty"`
}

func (d Infra) ToResponse() ResponseDto {
	resp := ResponseDto{
		Code:            d.Code,
		Name:            d.Name,
		InfraGroup_Code: d.InfraGroup_Code,
		Parent_Code:     d.Parent_Code,
		// Parent:          d.Parent,
		Childrens:      d.Childrens,
		Item_Code:      d.Item_Code,
		Item:           d.Item,
		ProcedureRooms: d.ProcedureRooms,
	}
	resp.SmallMain = d.SmallMain
	return resp
}

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