import { z } from 'zod'

const ERROR_MESSAGES = {
  required: {
    sepDate: 'Tanggal wajib diisi',
    serviceType: 'Jenis Pelayanan wajib diisi',
    admissionType: 'Jenis Pendaftaran wajib diisi',
    cardNumber: 'No. Kartu BPJS wajib diisi',
    nationalId: 'Nomor ID wajib diisi',
    medicalRecordNumber: 'Nomor Rekam Medis wajib diisi',
    patientName: 'Nama wajib diisi',
    phoneNumber: 'Nomor Telepon wajib diisi',
    referralLetterNumber: 'Nomor Surat Kontrol wajib diisi',
    referralLetterDate: 'Tanggal Surat Kontrol wajib diisi',
    fromClinic: 'Faskes Asal wajib diisi',
    destinationClinic: 'Klinik Tujuan wajib diisi',
    attendingDoctor: 'Dokter wajib diisi',
    initialDiagnosis: 'Diagnosa Awal wajib diisi',
    cob: 'COB wajib diisi',
    cataract: 'Katarak wajib diisi',
    clinicExcecutive: 'Klinkik eksekutif wajib diisi',
    subSpecialistId: 'Subspesialis wajib diisi',
    procedureType: 'Jenis Prosedur wajib diisi',
    supportCode: 'Kode Penunjang wajib diisi',
    note: 'Catatan wajib diisi',
    trafficAccident: 'Kejadian lalu lintas wajib diisi',
    purposeOfVisit: 'Tujuan Kunjungan wajib diisi',
    serviceAssessment: 'Assemen Pelayanan wajib diisi',
    lpNumber: 'Nomor LP wajib diisi',
    accidentDate: 'Tanggal Kejadian lalu lintas wajib diisi',
    accidentNote: 'Keterangan Kejadian lalu lintas wajib diisi',
    accidentProvince: 'Provinsi Kejadian lalu lintas wajib diisi',
    accidentCity: 'Kota Kejadian lalu lintas wajib diisi',
    accidentDistrict: 'Kecamatan Kejadian lalu lintas wajib diisi',
    suplesi: 'Suplesi wajib diisi',
    suplesiNumber: 'Nomor Suplesi wajib diisi',
    classLevel: 'Kelas Rawat wajib diisi',
    classLevelUpgrade: 'Kelas Rawat Naik wajib diisi',
    classPaySource: 'Pembiayaan wajib diisi',
    responsiblePerson: 'Penanggung Jawab wajib diisi',
  },
}

const IntegrationBpjsSchema = z
  .object({
    sepDate: z.string({ required_error: ERROR_MESSAGES.required.sepDate }).min(1, ERROR_MESSAGES.required.sepDate),
    serviceType: z
      .string({ required_error: ERROR_MESSAGES.required.serviceType })
      .min(1, ERROR_MESSAGES.required.serviceType)
      .optional(),
    admissionType: z
      .string({ required_error: ERROR_MESSAGES.required.admissionType })
      .min(1, ERROR_MESSAGES.required.admissionType),
    cardNumber: z
      .string({ required_error: ERROR_MESSAGES.required.cardNumber })
      .min(1, ERROR_MESSAGES.required.cardNumber),
    nationalId: z
      .string({ required_error: ERROR_MESSAGES.required.nationalId })
      .min(1, ERROR_MESSAGES.required.nationalId),
    medicalRecordNumber: z
      .string({ required_error: ERROR_MESSAGES.required.medicalRecordNumber })
      .min(1, ERROR_MESSAGES.required.medicalRecordNumber),
    patientName: z
      .string({ required_error: ERROR_MESSAGES.required.patientName })
      .min(1, ERROR_MESSAGES.required.patientName),
    phoneNumber: z
      .string({ required_error: ERROR_MESSAGES.required.phoneNumber })
      .min(1, ERROR_MESSAGES.required.phoneNumber),
    referralLetterNumber: z
      .string({ required_error: ERROR_MESSAGES.required.referralLetterNumber })
      .min(1, ERROR_MESSAGES.required.referralLetterNumber).optional(),
    referralLetterDate: z
      .string({ required_error: ERROR_MESSAGES.required.referralLetterDate })
      .min(1, ERROR_MESSAGES.required.referralLetterDate).optional(),
    fromClinic: z
      .string({ required_error: ERROR_MESSAGES.required.fromClinic })
      .min(1, ERROR_MESSAGES.required.fromClinic)
      .optional(),
    destinationClinic: z
      .string({ required_error: ERROR_MESSAGES.required.destinationClinic })
      .min(1, ERROR_MESSAGES.required.destinationClinic).optional(),
    attendingDoctor: z
      .string({ required_error: ERROR_MESSAGES.required.attendingDoctor })
      .min(1, ERROR_MESSAGES.required.attendingDoctor),
    initialDiagnosis: z
      .string({ required_error: ERROR_MESSAGES.required.initialDiagnosis })
      .min(1, ERROR_MESSAGES.required.initialDiagnosis),
    cob: z.string({ required_error: ERROR_MESSAGES.required.cob }).min(1, ERROR_MESSAGES.required.cob),
    cataract: z.string({ required_error: ERROR_MESSAGES.required.cataract }).min(1, ERROR_MESSAGES.required.cataract),
    clinicExcecutive: z
      .string({ required_error: ERROR_MESSAGES.required.clinicExcecutive })
      .min(1, ERROR_MESSAGES.required.clinicExcecutive).optional(),
    subSpecialistId: z
      .string({ required_error: ERROR_MESSAGES.required.subSpecialistId })
      .min(1, ERROR_MESSAGES.required.subSpecialistId)
      .optional(),
    procedureType: z
      .string({ required_error: ERROR_MESSAGES.required.procedureType })
      .min(1, ERROR_MESSAGES.required.procedureType)
      .optional(),
    supportCode: z
      .string({ required_error: ERROR_MESSAGES.required.supportCode })
      .min(1, ERROR_MESSAGES.required.supportCode)
      .optional(),
    note: z.string({ required_error: ERROR_MESSAGES.required.note }).min(1, ERROR_MESSAGES.required.note).optional(),
    trafficAccident: z
      .string({ required_error: ERROR_MESSAGES.required.trafficAccident })
      .min(1, ERROR_MESSAGES.required.trafficAccident)
      .optional(),
    purposeOfVisit: z
      .string({ required_error: ERROR_MESSAGES.required.purposeOfVisit })
      .min(1, ERROR_MESSAGES.required.purposeOfVisit)
      .optional(),
    serviceAssessment: z
      .string({ required_error: ERROR_MESSAGES.required.serviceAssessment })
      .min(1, ERROR_MESSAGES.required.serviceAssessment)
      .optional(),
    lpNumber: z.string({ required_error: ERROR_MESSAGES.required.lpNumber }).optional(),
    accidentDate: z.string({ required_error: ERROR_MESSAGES.required.accidentDate }).optional(),
    accidentNote: z.string({ required_error: ERROR_MESSAGES.required.accidentNote }).optional(),
    accidentProvince: z.string({ required_error: ERROR_MESSAGES.required.accidentProvince }).optional(),
    accidentCity: z.string({ required_error: ERROR_MESSAGES.required.accidentCity }).optional(),
    accidentDistrict: z.string({ required_error: ERROR_MESSAGES.required.accidentDistrict }).optional(),
    suplesi: z.string({ required_error: ERROR_MESSAGES.required.suplesi }).optional(),
    suplesiNumber: z.string({ required_error: ERROR_MESSAGES.required.suplesiNumber }).optional(),
    classLevel: z.string({ required_error: ERROR_MESSAGES.required.classLevel }).optional(),
    classLevelUpgrade: z.string({ required_error: ERROR_MESSAGES.required.classLevelUpgrade }).optional(),
    classPaySource: z.string({ required_error: ERROR_MESSAGES.required.classPaySource }).optional(),
    responsiblePerson: z.string({ required_error: ERROR_MESSAGES.required.responsiblePerson }).optional(),
  })
  .refine(
    (data) => {
      if (data.trafficAccident && data.trafficAccident.trim() !== '') {
        return data.accidentDate && data.accidentDate.trim() !== ''
      }
      return true
    },
    {
      message: ERROR_MESSAGES.required.accidentDate,
      path: ['accidentDate'],
    },
  )
  .refine(
    (data) => {
      if (data.trafficAccident && data.trafficAccident.trim() !== '') {
        return data.accidentProvince && data.accidentProvince.trim() !== ''
      }
      return true
    },
    {
      message: ERROR_MESSAGES.required.accidentProvince,
      path: ['accidentProvince'],
    },
  )
  .refine(
    (data) => {
      if (data.trafficAccident && data.trafficAccident.trim() !== '') {
        return data.accidentCity && data.accidentCity.trim() !== ''
      }
      return true
    },
    {
      message: ERROR_MESSAGES.required.accidentCity,
      path: ['accidentCity'],
    },
  )
  .refine(
    (data) => {
      if (data.trafficAccident && data.trafficAccident.trim() !== '') {
        return data.accidentDistrict && data.accidentDistrict.trim() !== ''
      }
      return true
    },
    {
      message: ERROR_MESSAGES.required.accidentDistrict,
      path: ['accidentDistrict'],
    },
  )
  .refine(
    (data) => {
      if (data.trafficAccident && data.trafficAccident.trim() !== '') {
        return data.suplesi && data.suplesi.trim() !== ''
      }
      return true
    },
    {
      message: ERROR_MESSAGES.required.suplesi,
      path: ['suplesi'],
    },
  )
  .refine(
    (data) => {
      if (data.trafficAccident && data.trafficAccident.trim() !== '' && data.suplesi?.trim() === 'yes') {
        return data.suplesiNumber && data.suplesiNumber.trim() !== ''
      }
      return true
    },
    {
      message: ERROR_MESSAGES.required.suplesiNumber,
      path: ['suplesiNumber'],
    },
  )
  .refine(
    (data) => {
      if (data.serviceType === '1') {
        return data.classLevel && data.classLevel.trim() !== ''
      }
      return true
    },
    {
      message: ERROR_MESSAGES.required.classLevel,
      path: ['classLevel'],
    },
  )
  .refine(
    (data) => {
      if (data.serviceType === '1') {
        return data.classLevelUpgrade && data.classLevelUpgrade.trim() !== ''
      }
      return true
    },
    {
      message: ERROR_MESSAGES.required.classLevelUpgrade,
      path: ['classLevelUpgrade'],
    },
  )
  .refine(
    (data) => {
      if (data.serviceType === '1' && data.classLevelUpgrade?.trim() !== '') {
        return data.classPaySource && data.classPaySource.trim() !== ''
      }
      return true
    },
    {
      message: ERROR_MESSAGES.required.classPaySource,
      path: ['classPaySource'],
    },
  )
  .refine(
    (data) => {
      if (data.serviceType === '1' && data.classPaySource?.trim() !== '') {
        return data.responsiblePerson && data.responsiblePerson.trim() !== ''
      }
      return true
    },
    {
      message: ERROR_MESSAGES.required.responsiblePerson,
      path: ['responsiblePerson'],
    },
  )

type IntegrationBpjsFormData = z.infer<typeof IntegrationBpjsSchema>

export { IntegrationBpjsSchema }
export type { IntegrationBpjsFormData }
