package resume

import (
	ecore "simrs-vx/internal/domain/base-entities/core"
	"time"

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

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

type CreateDto struct {
	Encounter_Id *uint                `json:"encounter_id"`
	Value        *string              `json:"value"`
	Status_Code  erc.DataVerifiedCode `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"`
}

type ReadDetailDto struct {
	Id       uint   `json:"id"`
	Includes string `json:"includes"`
}

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

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

	pa.AuthInfo
}

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"`
	Doctor_Code  *string              `json:"doctor_code"`
	Value        *string              `json:"value"`
	FileUrl      *string              `json:"fileUrl"`
	Status_Code  erc.DataVerifiedCode `json:"status_code"`
}

func (d Resume) ToResponse() ResponseDto {
	resp := ResponseDto{
		Encounter_Id: d.Encounter_Id,
		Doctor_Code:  d.Doctor_Code,
		Value:        d.Value,
		FileUrl:      d.FileUrl,
		Status_Code:  d.Status_Code,
	}
	resp.Main = d.Main
	return resp
}

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

// ValueDto is for resume value
type ValueDto struct {
	Assessment   Assessment             `json:"assessment"`
	Diagnosis    Diagnosis              `json:"diagnosis"`
	Action       Action                 `json:"action"`
	Consultation Consultation           `json:"consultation"`
	Supporting   SupportingExaminations `json:"supporting"`
	Pharmacy     PharmacyData           `json:"pharmacy"`
	Discharge    DischargeCondition     `json:"discharge"`
	National     NationalProgram        `json:"national"`
	Management   Management             `json:"management"`
	Medication   Medication             `json:"medication"`
}

type Assessment struct {
	StartedAt            *time.Time `json:"startedAt`
	FinishedAt           *time.Time `json:"finishedAt`
	Doctor_Code          string     `json:"doctor_code`
	DiagnosisIn          string     `json:"diagnosisIn`
	AmbulatoryIndication string     `json:"ambulatoryIndication"`
	MainComplaint        string     `json:"mainComplaint"`
	PhysicalExamination  string     `json:"physicalExamination"`
	MedicalHistory       string     `json:"medicalHistory"`
	MedicalDiagnosis     string     `json:"medicalDiagnosis"`
}

type Diagnosis struct {
	PrimaryDiagnosis   DiagnosisEntry   `json:"primaryDiagnosis"`
	SecondaryDiagnosis []DiagnosisEntry `json:"secondaryDiagnosis"`
}

type DiagnosisEntry struct {
	DiagnosisName string `json:"diagnosis"`
	ICD10         string `json:"icd_10"`
	Basis         string `json:"basis"` // Clinical basis of diagnosis / dasar diagnosa
}

type Action struct {
	PrimaryAction     ActionEntry   `json:"primaryAction"`
	AdditionalActions []ActionEntry `json:"additionalActions"`
	MedicalActions    string        `json:"medicalActions"` // free-text: "Tindakan Medis"
}

type ActionEntry struct {
	Action string `json:"action"` // Tindakan
	ICD9   string `json:"icd_9"`  // ICD-9
	Basis  string `json:"basis"`  // Dasar Tindakan
}

type Consultation struct {
	Consultations []ConsultationEntry `json:"consultations"`
}

type ConsultationEntry struct {
	Consultation       string `json:"consultation"`       // Konsultasi
	ConsultationAnswer string `json:"consultationAnswer"` // Jawaban Konsultasi
}

type SupportingExaminations struct {
	Notes string `json:"notes"` // Free-text list of lab/imaging results
}

type PharmacyData struct {
	AllergySpecialConditions string `json:"allergySpecialConditions"` // Kelainan Khusus Alergi
	OtherConditions          string `json:"otherConditions"`          // Kelainan Lain
	TherapyDuringCare        string `json:"therapyDuringCare"`        // Terapi selama dirawat
	TherapyAtDischarge       string `json:"therapyAtDischarge"`       // Terapi waktu pulang
	FollowUpInstructions     string `json:"followUpInstructions"`     // Edukasi / Anjuran / Follow-up
}

type DischargeCondition struct {
	BloodPressureSystolic  float64 `json:"bloodPressureSystolic"`  // Tekanan Darah Sistol (mmHg)
	BloodPressureDiastolic float64 `json:"bloodPressureDiastolic"` // Tekanan Darah Diastol (mmHg)
	RespirationRate        float64 `json:"respirationRate"`        // Pernafasan (kali/menit)
	HeartRate              float64 `json:"heartRate"`              // Denyut Jantung (kali/menit)
	BodyTemperature        float64 `json:"bodyTemperature"`        // Suhu Tubuh (°C)

	ConsciousnessLevel string `json:"consciousnessLevel"` // Tingkat Kesadaran
	PainScale          int    `json:"painScale"`          // Skala Nyeri (0–10)
}

type NationalProgram struct {
	ProgramService       string `json:"programService"`       // e.g. "Antenatal Care"
	ProgramServiceStatus string `json:"programServiceStatus"` // e.g. "Suspected"
}

type Management struct {
	NationalProgramService string `json:"nationalProgramService"` // e.g. selected program
	FollowUpManagement     string `json:"followUpManagement"`     // e.g. further management plan
	ConditionOnDischarge   string `json:"conditionOnDischarge"`   // e.g. "Stable"
	DischargeMethod        string `json:"dischargeMethod"`        // e.g. "Discharged with Doctor's Approval"
}

type MedicationEntry struct {
	DuringTreatment string `json:"duringTreatment"`
	AtDischarge     string `json:"atDischarge"`
}

type Medication struct {
	Medications []MedicationEntry `json:"medications"`
}
