// composables/usePatientForm.ts
import type { FhirPatient, FhirHumanName } from "~/types/fhir/humanName";

interface PatientFormData {
  dataDiri: {
    namaLengkap: string;
    jenisKelamin: string;
    tanggalLahir: string;
    nomorIdentitas: string;
    jenisIdentitas: string;
    fhirName?: FhirHumanName | null;
  };
  // ... other interfaces
}

export const usePatientForm = () => {
  const convertToFhirPatient = (formData: PatientFormData): FhirPatient => {
    const fhirPatient: FhirPatient = {
      resourceType: "Patient",
      identifier: [],
      active: true,
      name: [],
      telecom: [],
      gender: undefined,
      birthDate: undefined,
      address: []
    };

    // Add parsed FHIR name
    if (formData.dataDiri.fhirName) {
      fhirPatient.name!.push(formData.dataDiri.fhirName);
    }

    // Gender mapping
    if (formData.dataDiri.jenisKelamin) {
      fhirPatient.gender =
        formData.dataDiri.jenisKelamin === "L" ? "male" : "female";
    }

    // Birth date
    if (formData.dataDiri.tanggalLahir) {
      fhirPatient.birthDate = formData.dataDiri.tanggalLahir;
    }

    // Identifiers
    if (formData.dataDiri.nomorIdentitas) {
      fhirPatient.identifier!.push({
        use: "official",
        type: {
          coding: [
            {
              system: "http://terminology.hl7.org/CodeSystem/v2-0203",
              code:
                formData.dataDiri.jenisIdentitas === "KTP" ? "NNESP" : "PPN",
              display: formData.dataDiri.jenisIdentitas
            }
          ]
        },
        value: formData.dataDiri.nomorIdentitas
      });
    }

    return fhirPatient;
  };

  return {
    convertToFhirPatient
  };
};
interface PersonalInfo {
  nik?: string;
  fullName?: string;
  birthPlace?: string;
  birthDate?: string;
  gender?: string;
}

interface ContactInfo {
  phone?: string;
  address?: string;
  province?: string;
  city?: string;
}

// export const usePatientForm = () => {
//   const validatePersonalInfo = (data: PersonalInfo) => {
//     const errors: Record<string, string> = {};

//     if (!data.nik) errors.nik = "NIK wajib diisi";
//     if (!data.fullName) errors.fullName = "Nama lengkap wajib diisi";
//     if (!data.birthPlace) errors.birthPlace = "Tempat lahir wajib diisi";
//     if (!data.birthDate) errors.birthDate = "Tanggal lahir wajib diisi";
//     if (!data.gender) errors.gender = "Jenis kelamin wajib diisi";

//     return {
//       valid: Object.keys(errors).length === 0,
//       errors
//     };
//   };

//   const validateContactInfo = (data: ContactInfo) => {
//     const errors: Record<string, string> = {};

//     if (!data.phone) errors.phone = "Nomor telepon wajib diisi";
//     if (!data.address) errors.address = "Alamat wajib diisi";
//     if (!data.province) errors.province = "Provinsi wajib diisi";
//     if (!data.city) errors.city = "Kota wajib diisi";

//     return {
//       valid: Object.keys(errors).length === 0,
//       errors
//     };
//   };

//   const generateMRNumber = () => {
//     const date = new Date();
//     const year = date.getFullYear().toString().substr(-2);
//     const month = (date.getMonth() + 1).toString().padStart(2, "0");
//     const random = Math.floor(Math.random() * 10000)
//       .toString()
//       .padStart(4, "0");

//     return `MR${year}${month}${random}`;
//   };

//   return {
//     validatePersonalInfo,
//     validateContactInfo,
//     generateMRNumber
//   };
// };
