package generatefile

import (
	"fmt"
	"time"

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

	er "simrs-vx/internal/domain/main-entities/resume"

	pu "simrs-vx/pkg/use-case-helper"
)

type GeneralConsentPDF struct {
	Relatives   []string `json:"relatives"`
	Responsible string   `json:"responsible"`
	Informant   string   `json:"informant"`
	Witness1    string   `json:"witness1"`
	Witness2    string   `json:"witness2"`
	Date        string   `json:"date"`
}

type ControlLetterPDF struct {
	Number                 string
	Doctor_Name            string
	DstUnit_Name           string
	CardNumber             string
	Name                   string
	BirthDate              string
	Diagnose               string
	PlanDate               string
	ResponsibleDoctor_Name string
	PrintDate              string
}

type ResumePDF struct {
	MedicalRecord         string
	NIK                   string
	Name                  string
	BirthPlaceDate        string
	Gender                string
	Phone                 string
	Class                 string
	Unit                  string
	Doctor_Name           string
	StartedAt             string
	FinishedAt            string
	DiagnosisIn           string
	DiagnosisOut          string
	Diagnosis             []er.DiagnosisEntry
	MainComplaint         string
	MedicalHistory        string
	PhysicalExamination   string
	SupportingExamination string
	MedicalActions        []er.ActionEntry
	MedicalAction         string
	Consultations         []er.ConsultationEntry
	Allergy               string
	ConsciousnessLevel    string
	BloodPressure         string
	BodyTemperature       float64
	RespirationRate       float64
	HeartRate             float64
	PainScale             int
	ConditionOnDischarge  string
	DischargeMethod       string
	Medications           []er.MedicationEntry
	// fasyankes
	// date
	// klinik
	Date string
}

type ScreeningPDF struct {
	IssuedDate            string
	Date                  string
	MedicalRecord         string
	Name                  string
	BirthDate             string
	Employee_Name         string
	EarlyMedic            []string
	Assessment            string
	ProblemIdentification []string
	Planning              string
	FormB                 []ScreeningFormBPDF
}

type ScreeningFormBPDF struct {
	Number        int
	Date          string
	Employee_Name string
	Value         string
}

type GenerateDto struct {
	EntityType_Code ere.EntityTypeCode    `json:"entityType_code" validate:"required"`
	Ref_Id          *string               `json:"ref_id" validate:"required"`
	Type_Code       ere.DocTypeCode       `json:"type_code" validate:"required"`
	FormatType      erc.DocFormatTypeCode `json:"formatType"`
	TemplateName    TemplateDocsName      `json:"-"`
	Encounter_Id    *uint                 `json:"-"`
	UseA5Lanscape   bool                  `json:"-"`

	Data *string `json:"data"`
}

type VclaimControlLetter struct {
	Number                 string    `json:"noSuratKontrol"`
	PlannedControlDate     string    `json:"tglRencanaKontrol"`
	Doctor_Name            string    `json:"namaDokter"`
	DstUnit_Name           string    `json:"namaPoliTujuan"`
	ResponsibleDoctor_Name string    `json:"namaDokterPembuat"`
	VclaimSep              VclaimSep `json:"sep"`
}

type VclaimSep struct {
	VclaimMember VclaimMember `json:"peserta"`
	Diagnose     string       `json:"diagnosa"`
	Number       string       `json:"noSep"`
}

type VclaimMember struct {
	CardNumber string `json:"noKartu"`
	Name       string `json:"nama"`
	BirthDate  string `json:"tglLahir"`
	Gender     string `json:"kelamin"`
}

type GeneratePDFdto struct {
	TemplatePath  string
	TemplateData  any
	PdfPath       string
	UseA5Lanscape bool
}

type ResponseDto struct {
	FileUrl string `json:"fileUrl"`
}

type TemplateDocsName string

// TemplateDocsName is the name of the template file in the assets/docs folder
const (
	TDNGC TemplateDocsName = "general-consent.html"
	TDNCL TemplateDocsName = "control-letter.html"
	TDNR  TemplateDocsName = "resume.html"
	TDNSA TemplateDocsName = "screening-form-a.html"
	TDNSB TemplateDocsName = "screening-form-b.html"
)

func (v VclaimControlLetter) generateTemplateData() ControlLetterPDF {

	return ControlLetterPDF{
		Number:                 v.Number,
		Doctor_Name:            v.Doctor_Name,
		DstUnit_Name:           v.DstUnit_Name,
		CardNumber:             v.VclaimSep.VclaimMember.CardNumber,
		Name:                   v.generateNameWithGender(),
		BirthDate:              v.generateBirthDate(),
		Diagnose:               v.VclaimSep.Diagnose,
		PlanDate:               v.PlannedControlDate,
		ResponsibleDoctor_Name: v.ResponsibleDoctor_Name,
		PrintDate:              generatePrintDate(),
	}
}

func (v VclaimControlLetter) generateNameWithGender() string {
	gender := "Perempuan"
	if v.VclaimSep.VclaimMember.Gender == "L" {
		gender = "Laki-Laki"
	}

	return fmt.Sprintf("%s (%s)", v.VclaimSep.VclaimMember.Name, gender)
}

func (v VclaimControlLetter) generateBirthDate() string {
	t, _ := time.Parse("2006-01-02", v.VclaimSep.VclaimMember.BirthDate)
	return pu.FormatIndonesianDate(t)
}

func generatePrintDate() string {
	now := time.Now()
	return now.Format("2006/01/02")
}
