package device

import (
	ecore "simrs-vx/internal/domain/base-entities/core"
	ein "simrs-vx/internal/domain/main-entities/infra"
	ei "simrs-vx/internal/domain/main-entities/item"
	eu "simrs-vx/internal/domain/main-entities/uom"
)

type CreateDto struct {
	Code       string  `json:"code" validate:"maxLength=10"`
	Name       string  `json:"name" validate:"maxLength=50"`
	Uom_Code   string  `json:"uom_code" validate:"maxLength=10"`
	Infra_Code *string `json:"infra_code"`
	Item_Code  *string `json:"item_code"`
}

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

type FilterDto struct {
	Code     string  `json:"code"`
	Name     string  `json:"name"`
	Uom_Code string  `json:"uom-code"`
	Infra_Id *uint16 `json:"infra-id"`
	Item_Id  *uint   `json:"item-id"`
	Search   string  `json:"search" gormhelper:"searchColumns=Code,Name"`
}

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

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

type DeleteDto struct {
	Id   *uint   `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.Main
	Code     string     `json:"code"`
	Name     string     `json:"name"`
	Uom_Code string     `json:"uom_code"`
	Uom      *eu.Uom    `json:"uom,omitempty"`
	Infra_Id *uint16    `json:"infra_id"`
	Infra    *ein.Infra `json:"infra,omitempty"`
	Item_Id  *uint      `json:"item_id"`
	Item     *ei.Item   `json:"item,omitempty"`
}

func (d Device) ToResponse() ResponseDto {
	resp := ResponseDto{
		Code:     d.Code,
		Name:     d.Name,
		Uom_Code: d.Uom_Code,
		Uom:      d.Uom,
		Infra_Id: d.Infra_Id,
		Infra:    d.Infra,
		Item_Id:  d.Item_Id,
		Item:     d.Item,
	}
	resp.Main = d.Main
	return resp
}

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