package encounter

import (
	"time"

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

	ecore "simrs-vx/internal/domain/base-entities/core"
	evr "simrs-vx/internal/domain/bpjs-entities/vclaim-reference"
	evs "simrs-vx/internal/domain/bpjs-entities/vclaim-sep"
	eam "simrs-vx/internal/domain/main-entities/ambulatory"
	ea "simrs-vx/internal/domain/main-entities/appointment"
	edc "simrs-vx/internal/domain/main-entities/death-cause"
	ed "simrs-vx/internal/domain/main-entities/doctor"
	eem "simrs-vx/internal/domain/main-entities/emergency"
	ee "simrs-vx/internal/domain/main-entities/employee"
	eed "simrs-vx/internal/domain/main-entities/encounter-document"
	egc "simrs-vx/internal/domain/main-entities/general-consent"
	eip "simrs-vx/internal/domain/main-entities/inpatient"
	ei "simrs-vx/internal/domain/main-entities/insurance-company"
	eir "simrs-vx/internal/domain/main-entities/internal-reference"
	en "simrs-vx/internal/domain/main-entities/nurse"
	ep "simrs-vx/internal/domain/main-entities/patient"
	er "simrs-vx/internal/domain/main-entities/rehab/base"
	es "simrs-vx/internal/domain/main-entities/specialist"
	ess "simrs-vx/internal/domain/main-entities/subspecialist"
	eu "simrs-vx/internal/domain/main-entities/unit"
)

type Encounter struct {
	ecore.Main                                         // adjust this according to the needs
	Patient_Id                *uint                    `json:"patient_id"`
	Patient                   *ep.Patient              `json:"patient,omitempty" gorm:"foreignKey:Patient_Id;references:Id"`
	RegisteredAt              *time.Time               `json:"registeredAt"`
	Class_Code                ere.EncounterClassCode   `json:"class_code" gorm:"not null;size:10"`
	Unit_Code                 *string                  `json:"unit_code"`
	Unit                      *eu.Unit                 `json:"unit,omitempty" gorm:"foreignKey:Unit_Code;references:Code"`
	Specialist_Code           *string                  `json:"specialist_code"`
	Specialist                *es.Specialist           `json:"specialist,omitempty" gorm:"foreignKey:Specialist_Code;references:Code"`
	Subspecialist_Code        *string                  `json:"subspecialist_code"`
	Subspecialist             *ess.Subspecialist       `json:"subspecialist,omitempty" gorm:"foreignKey:Subspecialist_Code;references:Code"`
	VisitDate                 time.Time                `json:"visitDate"`
	StartedAt                 *time.Time               `json:"startedAt"`
	FinishedAt                *time.Time               `json:"finishedAt"`
	PaymentMethod_Code        ere.AllPaymentMethodCode `json:"paymentMethod_code" gorm:"size:10"`
	InsuranceCompany_Code     *string                  `json:"insuranceCompany_code"`
	InsuranceCompany          *ei.InsuranceCompany     `json:"insuranceCompany,omitempty" gorm:"foreignKey:InsuranceCompany_Code;references:Code"`
	Member_Number             *string                  `json:"memberNumber" gorm:"unique;size:20"`
	RefType_Code              *ere.RefTypeCode         `json:"refType_code"`
	Ref_Number                *string                  `json:"refNumber" gorm:"unique;size:20"`
	Trx_Number                *string                  `json:"trxNumber" gorm:"unique;size:20"`
	Appointment_Doctor_Code   *string                  `json:"appointment_doctor_code"`
	Appointment_Doctor        *ed.Doctor               `json:"appointment_doctor,omitempty" gorm:"foreignKey:Appointment_Doctor_Code;references:Code"`
	Adm_Employee_Id           *uint                    `json:"adm_employee_id"`
	Adm_Employee              *ee.Employee             `json:"adm_employee,omitempty" gorm:"foreignKey:Adm_Employee_Id;references:Id"`
	Responsible_Doctor_Code   *string                  `json:"responsible_doctor_code"`
	Responsible_Doctor        *ed.Doctor               `json:"responsible_doctor,omitempty" gorm:"foreignKey:Responsible_Doctor_Code;references:Code"`
	Discharge_Method_Code     *ere.DischargeMethodCode `json:"discharge_method_code" gorm:"size:16"`
	RefSource_Name            *string                  `json:"refSource_name" gorm:"size:100"`
	Appointment_Id            *uint                    `json:"appointment_id"`
	Appointment               *ea.Appointment          `json:"appointment,omitempty" gorm:"foreignKey:Appointment_Id;references:Id"`
	EarlyEducation            *string                  `json:"earlyEducation"`
	MedicalDischargeEducation *string                  `json:"medicalDischargeEducation"`
	AdmDischargeEducation     *string                  `json:"admDischargeEducation"`
	DischargeReason           *string                  `json:"dischargeReason"`
	Status_Code               erc.DataStatusCode       `json:"status_code" gorm:"size:10"`
	VclaimSep                 *evs.VclaimSep           `json:"vclaimSep,omitempty" gorm:"foreignKey:Encounter_Id;references:Id"`
	Discharge_Date            *time.Time               `json:"discharge_date"`
	InternalReferences        *[]eir.InternalReference `json:"internalReferences,omitempty" gorm:"foreignKey:Encounter_Id;references:Id"`
	DeathCause                *edc.DeathCause          `json:"deathCause,omitempty" gorm:"foreignKey:Encounter_Id;references:Id"`
	NewStatus                 bool                     `json:"newStatus"`
	Ambulatory                *eam.Ambulatory          `json:"ambulatory,omitempty" gorm:"foreignKey:Encounter_Id;references:Id"`
	Emergency                 *eem.Emergency           `json:"emergency,omitempty" gorm:"foreignKey:Encounter_Id;references:Id"`
	Inpatient                 *eip.Inpatient           `json:"inpatient,omitempty" gorm:"foreignKey:Encounter_Id;references:Id"`
	Rehab                     *er.Basic                `json:"rehab,omitempty" gorm:"foreignKey:Encounter_Id;references:Id"`
	RehabChildren             *[]er.Basic              `json:"rehabChildren,omitempty" gorm:"foreignKey:Parent_Encounter_Id;references:Id"`
	EncounterDocuments        *[]eed.EncounterDocument `json:"encounterDocuments,omitempty" gorm:"foreignKey:Encounter_Id;references:Id"`
	GeneralConsents           *[]egc.GeneralConsent    `json:"generalConsents,omitempty" gorm:"foreignKey:Encounter_Id;references:Id"`
	VclaimReference           *evr.VclaimReference     `json:"vclaimReference,omitempty" gorm:"foreignKey:Encounter_Id;references:Id"`
	Responsible_Nurse_Code    *string                  `json:"responsible_nurse_code"`
	Responsible_Nurse         *en.Nurse                `json:"responsible_nurse,omitempty" gorm:"foreignKey:Responsible_Nurse_Code;references:Code"`
}

func (d Encounter) IsDone() bool {
	return d.Status_Code == erc.DSCDone
}

func (d Encounter) IsSameResponsibleDoctor(input *string) bool {
	if input == nil {
		return false
	}
	return *d.Responsible_Doctor_Code == *input
}
