// Base
import * as base from './_crud-base'

// Types
import type { IntegrationBpjsFormData } from '~/schemas/integration-bpjs.schema'

const path = '/api/vclaim-swagger/sep'
const name = 'sep'

// TODO: temporary destinationClinic
const destinationClinic = '1323R001'

export function create(data: any) {
  return base.create(path, data, name)
}

export function getList(params: any = null) {
  let url = path
  if (params?.number) {
    url += `/${params.number}`
    delete params.number
  }
  return base.getList(url, params, name)
}

export function getDetail(id: number | string) {
  return base.getDetail(path, id, name)
}

export function remove(payload: any) {
  const url = `${path}`
  return base.removeCustom(url, payload, name)
}

export function makeSepData(
  data: IntegrationBpjsFormData & {
    userName: string
    polyCode?: string
    referralFrom?: string
    referralTo?: string
    referralLetterDate?: string
    referralLetterNumber?: string
  },
) {
  const content = {
    noKartu: data.cardNumber || '',
    tglSep: data.sepDate,
    ppkPelayanan: destinationClinic || data.fromClinic || '',
    jnsPelayanan: data.serviceType ? String(data.serviceType) : '2',
    noMR: data.medicalRecordNumber || '',
    catatan: data.note || '',
    diagAwal: data.initialDiagnosis || '',
    poli: {
      tujuan: data.polyCode || '',
      eksekutif: data.clinicExcecutive === 'yes' ? '1' : '0',
    },
    cob: {
      cob: data.cob === 'yes' ? '1' : '0',
    },
    katarak: {
      katarak: data.cataract === 'yes' ? '1' : '0',
    },
    tujuanKunj: data.purposeOfVisit || '0',
    flagProcedure: data.procedureType || '',
    kdPenunjang: data.supportCode || '',
    assesmentPel: data.serviceAssessment || '',
    skdp: {
      noSurat: ['3'].includes(data.admissionType) ? data.referralLetterNumber : '',
      kodeDPJP: ['3'].includes(data.admissionType) ? data.attendingDoctor : '',
    },
    rujukan: {
      // Handle referral data for admissionType !== '3'
      // asalRujukan: 1 = Faskes 1, 2 = Faskes RS
      asalRujukan: !['3'].includes(data.admissionType) ? data?.referralFrom || '' : '',
      tglRujukan: !['3'].includes(data.admissionType) ? data?.referralLetterDate || '' : '',
      noRujukan: !['3'].includes(data.admissionType) ? data?.referralLetterNumber || '' : '',
      ppkRujukan: !['3'].includes(data.admissionType) ? data?.referralTo || '' : '',
    },
    klsRawat: {
      klsRawatHak: data.classLevel || '',
      klsRawatNaik: data.classLevelUpgrade || '',
      pembiayaan: data.classPaySource || '',
      penanggungJawab: data.responsiblePerson || '',
    },
    dpjpLayan: data.attendingDoctor || '',
    noTelp: data.phoneNumber || '',
    user: data.userName || '',
    jaminan: {
      lakaLantas: data.trafficAccident || '0',
      noLP: data.lpNumber || '',
      penjamin: {
        tglKejadian: data.accidentDate || '',
        keterangan: data.accidentNote || '',
        suplesi: {
          suplesi: data.suplesi === 'yes' ? '1' : '0',
          noSepSuplesi: data.suplesiNumber || '',
          lokasiLaka: {
            kdPropinsi: data.accidentProvince || '',
            kdKabupaten: data.accidentCity || '',
            kdKecamatan: data.accidentDistrict || '',
          },
        },
      },
    },
  }
  return {
    request: {
      t_sep: content,
    },
  }
}

export function makeSepDataForRemove(data: any) {
  return {
    request: {
      t_sep: {
        noSep: data.sepNumber,
        user: data.userName,
      },
    },
  }
}
