package medicine

import (
	ecore "simrs-vx/internal/domain/base-entities/core"
	ein "simrs-vx/internal/domain/main-entities/infra"
	eit "simrs-vx/internal/domain/main-entities/item"
	emf "simrs-vx/internal/domain/main-entities/medicine-form"
	emg "simrs-vx/internal/domain/main-entities/medicine-group"
	emm "simrs-vx/internal/domain/main-entities/medicine-method"
	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"`
	MedicineGroup_Code  *string `json:"medicineGroup_code" validate:"maxLength=10"`
	MedicineMethod_Code *string `json:"medicineMethod_code" validate:"maxLength=10"`
	Uom_Code            *string `json:"uom_code" validate:"maxLength=10"`
	Dose                uint8   `json:"dose"`
	Infra_Code          *string `json:"infra_code"`
	Stock               *int    `json:"stock"`
	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"`
	MedicineGroup_Code  *string `json:"medicineGroup-code"`
	MedicineMethod_Code *string `json:"medicineMethod-code"`
	Uom_Code            *string `json:"uom-code"`
	Dose                uint8   `json:"dose"`
	Infra_Code          *string `json:"infra-code"`
	Stock               *int    `json:"stock"`
	Item_Code           *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 *uint   `json:"item_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"`
	MedicineGroup_Code  *string             `json:"medicineGroup_code"`
	MedicineGroup       *emg.MedicineGroup  `json:"medicineGroup"`
	MedicineMethod_Code *string             `json:"medicineMethod_code"`
	MedicineMethod      *emm.MedicineMethod `json:"medicineMethod"`
	MedicineForm_Code   *string             `json:"medicineForm_code"`
	MedicineForm        *emf.MedicineForm   `json:"medicineForm"`
	Uom_Code            *string             `json:"uom_code"`
	Uom                 *eu.Uom             `json:"uom"`
	Dose                uint8               `json:"dose"`
	Infra_Code          *string             `json:"infra_code"`
	Infra               *ein.Infra          `json:"infra,omitempty"`
	Stock               *int                `json:"stock"`
	Item_Code           *string             `json:"item_code"`
	Item                *eit.Item           `json:"item,omitempty"`
}

func (d Medicine) ToResponse() ResponseDto {
	resp := ResponseDto{
		Code:                d.Code,
		Name:                d.Name,
		MedicineGroup_Code:  d.MedicineGroup_Code,
		MedicineGroup:       d.MedicineGroup,
		MedicineMethod_Code: d.MedicineMethod_Code,
		MedicineMethod:      d.MedicineMethod,
		MedicineForm_Code:   d.MedicineForm_Code,
		MedicineForm:        d.MedicineForm,
		Uom_Code:            d.Uom_Code,
		Uom:                 d.Uom,
		Dose:                d.Dose,
		Infra_Code:          d.Infra_Code,
		Infra:               d.Infra,
		Stock:               d.Stock,
		Item_Code:           d.Item_Code,
		Item:                d.Item,
	}
	resp.Main = d.Main
	return resp
}

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