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

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

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

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

export function create(data: any) {
  const isNew = true
  let url = !isNew ? pathOld : path
  let payload: any = data
  if (isNew && data?.encounterId) {
    payload = {
      encounter_id: Number(data.encounterId) || 0,
      requestPayload: data?.request ? JSON.stringify({ request: data.request }) : null,
    }
  } else {
    url = pathOld
    delete payload.encounterId
  }
  return base.create(url, payload, name)
}

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

export function getDetail(id: number | string) {
  const isNew = true
  const url = !isNew ? pathOld : path
  return base.getDetail(url, id, name)
}

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

export function removeOld(payload: any) {
  return base.removeCustom(pathOld, 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,
      },
    },
  }
}
