import { z } from 'zod'
import type { CreateDto } from '~/models/consultation'

export type ResumeArrangementType = "krs" | "mrs" | "rujukInternal" | "rujukExternal" | "meninggal" | "other"

const SecondaryDiagnosisSchema = z.object({  
  diagnosis: z.string({ required_error: 'Diagnosis harus diisi' }),
  icd10: z.string({ required_error: 'ICD 10 harus diisi' }),
  diagnosisBasis: z.string({ required_error: 'Dasar Diagnosis harus diisi' })
    .min(1, 'Uraian minimum 1 karakter')
    .max(2048, 'Uraian maksimum 2048 karakter'),
})

const SecondaryActionSchema = z.object({
  action: z.string({ required_error: 'Action harus diisi' }),
  icd9: z.string({ required_error: 'ICD 10 harus diisi' }),
  actionBasis: z.string({ required_error: 'Dasar Action harus diisi' })
    .min(1, 'Uraian minimum 1 karakter')
    .max(2048, 'Uraian maksimum 2048 karakter'),
})

const ConsultationSchema = z.object({
  consultation: z.string({ required_error: 'Consultation harus diisi' })
    .min(1, 'Uraian minimum 1 karakter')
    .max(2048, 'Uraian maksimum 2048 karakter'),
  consultationReply: z.string({ required_error: 'Jawaban Consultation harus diisi' })
    .min(1, 'Uraian minimum 1 karakter')
    .max(2048, 'Uraian maksimum 2048 karakter'),
})

const ResumeSchema = z.object({
  inDate: z.string({ required_error: 'Tanggal harus diisi' }),
  outDate: z.string({ required_error: 'Tanggal harus diisi' }),
  anamnesis: z.number({ required_error: 'Anamnesis harus diisi' })
    .min(1, 'Anamnesis minimum 1 karakter')
    .max(2048, 'Anamnesis maksimum 2048 karakter'),
  physicalCheckup: z.string({ required_error: 'Uraian harus diisi' })
    .min(1, 'Uraian minimum 1 karakter')
    .max(2048, 'Uraian maksimum 2048 karakter'),
  supplementCheckup: z.string({ required_error: 'Uraian harus diisi' })
    .min(1, 'Uraian minimum 1 karakter')
    .max(2048, 'Uraian maksimum 2048 karakter'),

  primaryDiagnosis: z.string({ required_error: 'Diagnosis harus diisi' }),
  secondaryDiagnosis: z.array(SecondaryDiagnosisSchema).optional(),

  primaryOperativeNonOperativeAct: z.string({ required_error: 'Diagnosis harus diisi' }),
  secondaryOperativeNonOperativeAct: z.array(SecondaryActionSchema).optional(),
  medikamentosa: z.string({ required_error: 'Uraian harus diisi' })
    .min(1, 'Uraian minimum 1 karakter')
    .max(2048, 'Uraian maksimum 2048 karakter'),

  consultation: z.array(ConsultationSchema).optional(),

  arrangement: z.custom<ResumeArrangementType>().default("krs"),
  inpatientIndication: z.string({ required_error: 'Uraian harus diisi' })
    .min(1, 'Uraian minimum 1 karakter')
    .max(2048, 'Uraian maksimum 2048 karakter'),
  faskes: z.string({ required_error: 'Faskes harus diisi' }).optional(),
  clinic: z.string({ required_error: 'Klinik harus diisi' }).optional(),
  deathDate: z.string({ required_error: 'Tanggal harus diisi' }).optional(),
  deathCause: z.array(z.string()).optional().default([]),
  deathCauseDescription: z.string({ required_error: 'Uraian harus diisi' })
    .min(1, 'Uraian minimum 1 karakter')
    .max(2048, 'Uraian maksimum 2048 karakter')
    .optional(),
  keterangan: z.string({ required_error: 'Uraian harus diisi' })
    .min(1, 'Uraian minimum 1 karakter')
    .max(2048, 'Uraian maksimum 2048 karakter')
    .optional(),
})

type ResumeFormData = z.infer<typeof ResumeSchema> & (CreateDto)

export { ResumeSchema }
export type { ResumeFormData }
