package prescription

import (
	"time"

	ecore "simrs-vx/internal/domain/base-entities/core"
	ed "simrs-vx/internal/domain/main-entities/doctor"
	ee "simrs-vx/internal/domain/main-entities/encounter"
	epi "simrs-vx/internal/domain/main-entities/prescription-item/base"

	pa "simrs-vx/internal/lib/auth"

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

type CreateDto struct {
	Encounter_Id *uint              `json:"encounter_id"`
	Doctor_Code  *string            `json:"doctor_code"`
	IssuedAt     *time.Time         `json:"issuedAt"`
	Status_Code  erc.DataStatusCode `json:"status_code"`

	pa.AuthInfo
}

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

type FilterDto struct {
	Encounter_Id *uint               `json:"encounter-id"`
	Doctor_Code  *string             `json:"doctor-code"`
	IssuedAt     *time.Time          `json:"issuedAt"`
	Status_Code  *erc.DataStatusCode `json:"status-code"`
}
type ReadDetailDto struct {
	Id           uint   `json:"id"`
	Encounter_Id *uint  `json:"encounter_id"`
	Includes     string `json:"includes"`
}

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

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

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

type ResponseDto struct {
	ecore.Main
	Encounter_Id *uint                  `json:"encounter_id"`
	Encounter    *ee.Encounter          `json:"encounter,omitempty"`
	Doctor_Code  *string                `json:"doctor_code"`
	Doctor       *ed.Doctor             `json:"doctor,omitempty"`
	IssuedAt     *time.Time             `json:"issuedAt"`
	Status_Code  erc.DataStatusCode     `json:"status_code"`
	Items        []epi.PrescriptionItem `json:"items" gorm:"foreignKey:Prescription_Id;references:Id"`
}

func (d Prescription) ToResponse() ResponseDto {
	resp := ResponseDto{
		Encounter_Id: d.Encounter_Id,
		Encounter:    d.Encounter,
		Doctor_Code:  d.Doctor_Code,
		Doctor:       d.Doctor,
		IssuedAt:     d.IssuedAt,
		Status_Code:  d.Status_Code,
		Items:        d.Items,
	}
	resp.Main = d.Main
	return resp
}

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