package item

import (
	ecore "simrs-vx/internal/domain/base-entities/core"
	eu "simrs-vx/internal/domain/main-entities/uom"
	ero "simrs-vx/internal/domain/references/organization"
)

type CreateDto struct {
	Code           string            `json:"code" validate:"maxLength=50"`
	Name           string            `json:"name" validate:"maxLength=100"`
	ItemGroup_Code ero.ItemGroupCode `json:"itemGroup_code" validate:"maxLength=10"`
	Uom_Code       *string           `json:"uom_code" validate:"maxLength=10"`
	Infra_Code     *string           `json:"infra_code"`
	Stock          *int              `json:"stock"`
	BuyingPrice    *float64          `json:"buyingPrice"`
	SellingPrice   *float64          `json:"sellingPrice"`
}

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"`
	ItemGroup_Code ero.ItemGroupCode `json:"itemGroup-code"`
	Uom_Code       *string           `json:"uom-code"`
	Infra_Code     *string           `json:"infra-code"`
	Stock          *int              `json:"stock"`
	Search         string            `json:"search" gormhelper:"searchColumns=Code,Name"`
}

type ReadDetailDto struct {
	Id   *uint   `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"`
	ItemGroup_Code ero.ItemGroupCode `json:"itemGroup_code"`
	Uom_Code       *string           `json:"uom_code"`
	Uom            *eu.Uom           `json:"uom,omitempty"`
	Infra_Code     *string           `json:"infra_code"`
	Stock          *int              `json:"stock"`
	BuyingPrice    *float64          `json:"buyingPrice"`
	SellingPrice   *float64          `json:"sellingPrice"`
}

func (d Item) ToResponse() ResponseDto {
	resp := ResponseDto{
		Code:           d.Code,
		Name:           d.Name,
		ItemGroup_Code: d.ItemGroup_Code,
		Uom_Code:       d.Uom_Code,
		Uom:            d.Uom,
		Infra_Code:     d.Infra_Code,
		Stock:          d.Stock,
		BuyingPrice:    d.BuyingPrice,
		SellingPrice:   d.SellingPrice,
	}
	resp.Main = d.Main
	return resp
}

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