package prescriptionitem

import (
	ecore "simrs-vx/internal/domain/base-entities/core"
	em "simrs-vx/internal/domain/main-entities/medicine"
	emm "simrs-vx/internal/domain/main-entities/medicine-mix"
	ep "simrs-vx/internal/domain/main-entities/prescription"

	erc "simrs-vx/internal/domain/references/common"
)

type CreateDto struct {
	Prescription_Id    *uint            `json:"prescription_id"`
	IsMix              bool             `json:"isMix"`
	Medicine_Code      *string          `json:"medicine_code"`
	MedicineMix_Id     *uint            `json:"medicineMix_id"`
	Frequency          *uint16          `json:"frequency" validate:"required"`
	Dose               float64          `json:"dose" validate:"required"`
	Usage              string           `json:"usage"`
	Interval           uint8            `json:"interval"`
	IntervalUnit_Code  erc.TimeUnitCode `json:"intervalUnit_code"`
	IntervalMultiplier *uint16          `json:"intervalMultiplier"`
	Quantity           float64          `json:"quantity"`
}

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

type FilterDto struct {
	Prescription_Id   *uint            `json:"prescription-id"`
	IsMix             bool             `json:"isMix"`
	Medicine_Code     *string          `json:"medicine-code"`
	MedicineMix_Id    *uint            `json:"medicineMix-id"`
	Usage             float64          `json:"usage"`
	Interval          uint8            `json:"interval"`
	IntervalUnit_Code erc.TimeUnitCode `json:"intervalUnit-code"`
	Quantity          float64          `json:"quantity"`
}
type ReadDetailDto struct {
	Id uint16 `json:"id"`
}

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

type DeleteDto struct {
	Id uint16 `json:"id"`
}

type MetaDto struct {
	PageNumber int `json:"page_number"`
	PageSize   int `json:"page_size"`
	Count      int `json:"count"`
}

type ResponseDto struct {
	ecore.Main
	Prescription_Id   *uint            `json:"prescription_id"`
	Prescription      *ep.Prescription `json:"prescription,omitempty"`
	IsMix             bool             `json:"isMix"`
	Medicine_Code     *string          `json:"medicine_code"`
	Medicine          *em.Medicine     `json:"medicine,omitempty"`
	MedicineMix_Id    *uint            `json:"medicineMix_id"`
	MedicineMix       *emm.MedicineMix `json:"medicineMix,omitempty"`
	Frequency         *uint16          `json:"frequency"`
	Dose              float64          `json:"dose"`
	Usage             string           `json:"usage"`
	Interval          uint8            `json:"interval"`
	IntervalUnit_Code erc.TimeUnitCode `json:"intervalUnit_code"`
	Quantity          float64          `json:"quantity"`
}

func (d PrescriptionItem) ToResponse() ResponseDto {
	resp := ResponseDto{
		Prescription_Id:   d.Prescription_Id,
		Prescription:      d.Prescription,
		IsMix:             d.IsMix,
		Medicine_Code:     d.Medicine_Code,
		Medicine:          d.Medicine,
		MedicineMix_Id:    d.MedicineMix_Id,
		MedicineMix:       d.MedicineMix,
		Frequency:         d.Frequency,
		Dose:              d.Dose,
		Usage:             d.Usage,
		Interval:          d.Interval,
		IntervalUnit_Code: d.IntervalUnit_Code,
		Quantity:          d.Quantity,
	}
	resp.Main = d.Main
	return resp
}

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