package medicationitem

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

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

type CreateDto struct {
	Medication_Id      *uint            `json:"medication_id"`
	IsMix              bool             `json:"isMix"`
	Medicine_Code      *string          `json:"medicine_code"`
	MedicineMix_Id     *uint            `json:"medicineMix_id"`
	Frequency          *uint16          `json:"frequency"`
	Dose               float64          `json:"dose"`
	Usage              string           `json:"usage"`
	Interval           uint8            `json:"interval"`
	IntervalUnit_Code  erc.TimeUnitCode `json:"intervalUnit_code"`
	IsRedeemed         bool             `json:"isRedeemed"`
	Quantity           float64          `json:"quantity"`
	Note               *string          `json:"note" gorm:"size:1024"`
	IntervalMultiplier *uint16          `json:"intervalMultiplier"`
}

type ReadListDto struct {
	FilterDto
	Includes   string   `json:"includes"`
	Preloads   []string `json:"-"`
	Pagination ecore.Pagination
}

type FilterDto struct {
	Medication_Id     *uint            `json:"medication-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"`
	IsRedeemed        bool             `json:"isRedeemed"`
	Quantity          float64          `json:"quantity"`
	Note              *string          `json:"note" gorm:"size:1024"`
}
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
	Medication_Id     *uint            `json:"medication_id"`
	Medication        *eme.Medication  `json:"medication,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"`
	Usage             string           `json:"usage"`
	Interval          uint8            `json:"interval"`
	IntervalUnit_Code erc.TimeUnitCode `json:"intervalUnit_code"`
	IsRedeemed        bool             `json:"isRedeemed"`
	Quantity          float64          `json:"quantity"`
	Note              *string          `json:"note" gorm:"size:1024"`
}

func (d MedicationItem) ToResponse() ResponseDto {
	resp := ResponseDto{
		Medication_Id:     d.Medication_Id,
		Medication:        d.Medication,
		IsMix:             d.IsMix,
		Medicine_Code:     d.Medicine_Code,
		Medicine:          d.Medicine,
		MedicineMix_Id:    d.MedicineMix_Id,
		MedicineMix:       d.MedicineMix,
		Usage:             d.Usage,
		Interval:          d.Interval,
		IntervalUnit_Code: d.IntervalUnit_Code,
		IsRedeemed:        d.IsRedeemed,
		Quantity:          d.Quantity,
		Note:              d.Note,
	}
	resp.Main = d.Main
	return resp
}

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