// stores/clinicStore.js
import { defineStore } from 'pinia';
import { ref, computed } from 'vue';

export const useClinicStore = defineStore('clinic', () => {
  // Data clinics - Single source of truth untuk semua data klinik
  // Includes basic info (name, kode, icon, doctors, shifts) + master config (totalQuota, jamShiftList, jadwalKlinik, autoShift)
  const clinics = ref([
    {
      id: 1,
      kode: "AN",
      name: "ANAK",
      subtitle: "",
      icon: "mdi-baby-face",
      shift: "SHIFT 1",
      schedule: "Mulai Pukul 07:00",
      available: true,
      doctors: ["dr. Sarah Putri, Sp.A", "dr. Andi Wijaya, Sp.A"],
      shifts: [
        { name: "Shift 1", quota: 15 },
        { name: "Shift 2", quota: 20 }
      ],
      
      totalQuota: 1000,
      jamShiftList: [{ dari: '07:00', sampai: '11:00', kuota: 1000 }],
      autoShift: false,
      jadwalKlinik: ['Senin', 'Rabu', 'Jumat'],
    },
    {
      id: 2,
      kode: "AS",
      name: "ANESTESI",
      subtitle: "",
      icon: "mdi-face-mask",
      shift: "SHIFT 1",
      schedule: "Mulai Pukul 07:00",
      available: true,
      doctors: ["dr. Ahmad Fauzi, Sp.An"],
      shifts: [
        { name: "Shift 1", quota: 0 },
        { name: "Shift 2", quota: 12 }
      ],
      // Master config
      totalQuota: 1500,
      jamShiftList: [
        { dari: '07:00', sampai: '11:00', kuota: 1000 },
        { dari: '13:00', sampai: '16:00', kuota: 500 }
      ],
      autoShift: false,
      jadwalKlinik: ['Senin', 'Selasa', 'Rabu', 'Kamis', 'Jum\'at'],
    },
    {
      id: 3,
      kode: "BD",
      name: "BEDAH",
      subtitle: "",
      icon: "mdi-medical-bag",
      shift: "SHIFT 1",
      schedule: "Mulai Pukul 07:00",
      available: true,
      doctors: ["dr. Budi Santoso, Sp.B", "dr. Eko Prasetyo, Sp.B", "dr. Dian Permata, Sp.B"],
      shifts: [
        { name: "Shift 1", quota: 8 },
        { name: "Shift 2", quota: 10 }
      ],
      // Master config
      totalQuota: 800,
      jamShiftList: [{ dari: '07:00', sampai: '11:00', kuota: 800 }],
      autoShift: false,
      jadwalKlinik: ['Senin', 'Rabu', 'Jumat'],
    },
    {
      id: 4,
      kode: "GR",
      name: "GERIATRI",
      subtitle: "",
      icon: "mdi-human-cane",
      shift: "SHIFT 1",
      schedule: "Mulai Pukul 07:00",
      available: true,
      doctors: ["dr. Siti Aminah, Sp.PD-KGer"],
      shifts: [
        { name: "Shift 1", quota: 10 },
        { name: "Shift 2", quota: 8 }
      ],
      // Master config
      totalQuota: 600,
      jamShiftList: [{ dari: '07:00', sampai: '11:00', kuota: 600 }],
      autoShift: false,
      jadwalKlinik: ['Senin', 'Rabu'],
    },
    {
      id: 5,
      kode: "GI",
      name: "GIGI DAN MULUT",
      subtitle: "",
      icon: "mdi-tooth",
      shift: "SHIFT 1",
      schedule: "Mulai Pukul 07:00",
      available: true,
      doctors: ["drg. Rina Wati, Sp.KG", "drg. Made Surya, Sp.KG"],
      shifts: [
        { name: "Shift 1", quota: 18 },
        { name: "Shift 2", quota: 15 }
      ],
      // Master config
      totalQuota: 2100,
      jamShiftList: [
        { dari: '07:00', sampai: '11:00', kuota: 1000 },
        { dari: '13:00', sampai: '16:00', kuota: 700 },
        { dari: '18:00', sampai: '20:00', kuota: 400 }
      ],
      autoShift: true,
      jadwalKlinik: ['Senin', 'Selasa', 'Rabu', 'Kamis', 'Jum\'at'],
    },
    {
      id: 6,
      kode: "GZ",
      name: "GIZI",
      subtitle: "",
      icon: "mdi-food-apple",
      shift: "SHIFT 1",
      schedule: "Mulai Pukul 07:00",
      available: true,
      doctors: ["dr. Lisa Andriani, Sp.GK"],
      shifts: [
        { name: "Shift 1", quota: 12 },
        { name: "Shift 2", quota: 10 }
      ],
      // Master config
      totalQuota: 500,
      jamShiftList: [{ dari: '07:00', sampai: '11:00', kuota: 500 }],
      autoShift: false,
      jadwalKlinik: ['Senin', 'Rabu', 'Jumat'],
    },
    {
      id: 7,
      kode: "HO",
      name: "HEMATO ONKOLOGI MEDIS",
      subtitle: "",
      icon: "mdi-water",
      shift: "TUTUP",
      schedule: "",
      available: false,
      doctors: [],
      shifts: [],
      // Master config
      totalQuota: 750,
      jamShiftList: [{ dari: '07:00', sampai: '11:00', kuota: 750 }],
      autoShift: false,
      jadwalKlinik: ['Senin', 'Selasa', 'Rabu'],
    },
    {
      id: 8,
      kode: "IP",
      name: "IPD (PENYAKIT DALAM)",
      subtitle: "",
      icon: "mdi-hospital",
      shift: "SHIFT 1",
      schedule: "Mulai Pukul 07:00",
      available: true,
      doctors: ["dr. Hendra Wijaya, Sp.PD", "dr. Agus Salim, Sp.PD", "dr. Retno Wulan, Sp.PD", "dr. Bambang Susilo, Sp.PD"],
      shifts: [
        { name: "Shift 1", quota: 25 },
        { name: "Shift 2", quota: 20 }
      ],
      // Master config
      totalQuota: 900,
      jamShiftList: [{ dari: '07:00', sampai: '11:00', kuota: 900 }],
      autoShift: false,
      jadwalKlinik: ['Senin', 'Rabu', 'Jumat'],
    },
    {
      id: 9,
      kode: "JT",
      name: "JANTUNG (CARDIOLOGI)",
      subtitle: "",
      icon: "mdi-heart-pulse",
      shift: "SHIFT 1",
      schedule: "Mulai Pukul 07:00",
      available: true,
      doctors: ["dr. Rudi Hartono, Sp.JP", "dr. Sinta Dewi, Sp.JP"],
      shifts: [
        { name: "Shift 1", quota: 14 },
        { name: "Shift 2", quota: 12 }
      ],
      // Master config
      totalQuota: 1200,
      jamShiftList: [
        { dari: '07:00', sampai: '11:00', kuota: 800 },
        { dari: '13:00', sampai: '16:00', kuota: 400 }
      ],
      autoShift: false,
      jadwalKlinik: ['Senin', 'Selasa', 'Rabu', 'Kamis', 'Jum\'at'],
    },
    {
      id: 10,
      kode: "JW",
      name: "JIWA",
      subtitle: "",
      icon: "mdi-head-dots-horizontal",
      shift: "SHIFT 1",
      schedule: "Mulai Pukul 07:00",
      available: true,
      doctors: ["dr. Maya Kusuma, Sp.KJ"],
      shifts: [
        { name: "Shift 1", quota: 10 },
        { name: "Shift 2", quota: 8 }
      ],
      // Master config
      totalQuota: 700,
      jamShiftList: [{ dari: '07:00', sampai: '11:00', kuota: 700 }],
      autoShift: false,
      jadwalKlinik: ['Senin', 'Rabu'],
    },
    {
      id: 11,
      kode: "OB",
      name: "KANDUNGAN",
      subtitle: "",
      icon: "mdi-human-pregnant",
      shift: "SHIFT 1",
      schedule: "Mulai Pukul 07:00",
      available: true,
      doctors: ["dr. Dewi Lestari, Sp.OG", "dr. Putri Andini, Sp.OG", "dr. Nova Riani, Sp.OG"],
      shifts: [
        { name: "Shift 1", quota: 16 },
        { name: "Shift 2", quota: 14 }
      ],
      // Master config
      totalQuota: 1000,
      jamShiftList: [
        { dari: '07:00', sampai: '11:00', kuota: 600 },
        { dari: '13:00', sampai: '16:00', kuota: 400 }
      ],
      autoShift: false,
      jadwalKlinik: ['Senin', 'Selasa', 'Rabu', 'Kamis', 'Jum\'at'],
    },
    {
      id: 12,
      kode: "KH", // kode fiktif untuk KEMOTERAPI (belum ada di master klinik)
      name: "KEMOTERAPI",
      subtitle: "",
      icon: "mdi-virus",
      shift: "SHIFT 1",
      schedule: "Mulai Pukul 07:00",
      available: true,
      doctors: ["dr. Andi Pratama, Sp.PD-KHOM"],
      shifts: [
        { name: "Shift 1", quota: 8 },
        { name: "Shift 2", quota: 6 }
      ],
      // Master config (default values, not in masterStore)
      totalQuota: 0,
      jamShiftList: [],
      autoShift: false,
      jadwalKlinik: [],
    },
    {
      id: 13,
      kode: "KN", // kode fiktif untuk KOMPLEMENTER (NYERI)
      name: "KOMPLEMENTER (NYERI)",
      subtitle: "",
      icon: "mdi-medical-bag",
      shift: "SHIFT 1",
      schedule: "Mulai Pukul 07:00",
      available: true,
      doctors: ["dr. Ratna Sari, Sp.KFR"],
      shifts: [
        { name: "Shift 1", quota: 10 },
        { name: "Shift 2", quota: 8 }
      ],
      // Master config (default values, not in masterStore)
      totalQuota: 0,
      jamShiftList: [],
      autoShift: false,
      jadwalKlinik: [],
    },
    {
      id: 14,
      kode: "KK",
      name: "KULIT KELAMIN",
      subtitle: "",
      icon: "mdi-human-male-female",
      shift: "SHIFT 1",
      schedule: "Mulai Pukul 07:00",
      available: true,
      doctors: ["dr. Tika Anggraini, Sp.KK", "dr. Fajar Ramadhan, Sp.KK"],
      shifts: [
        { name: "Shift 1", quota: 12 },
        { name: "Shift 2", quota: 10 }
      ],
      // Master config
      totalQuota: 600,
      jamShiftList: [{ dari: '07:00', sampai: '11:00', kuota: 600 }],
      autoShift: false,
      jadwalKlinik: ['Selasa', 'Kamis'],
    },
    {
      id: 15,
      kode: "MT",
      name: "MATA",
      subtitle: "",
      icon: "mdi-eye",
      shift: "SHIFT 1",
      schedule: "Mulai Pukul 07:00",
      available: true,
      doctors: ["dr. Yudi Prasetyo, Sp.M", "dr. Linda Kartika, Sp.M"],
      shifts: [
        { name: "Shift 1", quota: 15 },
        { name: "Shift 2", quota: 12 }
      ],
      // Master config
      totalQuota: 800,
      jamShiftList: [{ dari: '07:00', sampai: '11:00', kuota: 800 }],
      autoShift: false,
      jadwalKlinik: ['Senin', 'Rabu', 'Jumat'],
    },
    {
      id: 16,
      kode: "MC", // kode fiktif untuk MCU
      name: "MCU",
      subtitle: "",
      icon: "mdi-clipboard-check",
      shift: "SHIFT 1",
      schedule: "Mulai Pukul 07:00",
      available: true,
      doctors: ["dr. Fitri Handayani, Sp.OK"],
      shifts: [
        { name: "Shift 1", quota: 20 },
        { name: "Shift 2", quota: 15 }
      ],
      // Master config (default values, not in masterStore)
      totalQuota: 0,
      jamShiftList: [],
      autoShift: false,
      jadwalKlinik: [],
    },
    {
      id: 17,
      kode: "ON", // kode fiktif untuk ONKOLOGI
      name: "ONKOLOGI",
      subtitle: "",
      icon: "mdi-virus",
      shift: "SHIFT 1",
      schedule: "Mulai Pukul 07:00",
      available: true,
      doctors: ["dr. Dimas Prakoso, Sp.Onk-Rad"],
      shifts: [
        { name: "Shift 1", quota: 10 },
        { name: "Shift 2", quota: 8 }
      ],
      // Master config (default values, not in masterStore)
      totalQuota: 0,
      jamShiftList: [],
      autoShift: false,
      jadwalKlinik: [],
    },
    {
      id: 18,
      kode: "PR",
      name: "PARU",
      subtitle: "",
      icon: "mdi-lungs",
      shift: "SHIFT 1",
      schedule: "Mulai Pukul 07:00",
      available: true,
      doctors: ["dr. Nina Marlina, Sp.P", "dr. Arief Budiman, Sp.P"],
      shifts: [
        { name: "Shift 1", quota: 14 },
        { name: "Shift 2", quota: 12 }
      ],
      // Master config
      totalQuota: 700,
      jamShiftList: [{ dari: '07:00', sampai: '11:00', kuota: 700 }],
      autoShift: false,
      jadwalKlinik: ['Senin', 'Rabu'],
    },
    {
      id: 19,
      kode: "TD", // mengikuti kode pelayanan TD di loket
      name: "R. TINDAKAN (EMG, ECG, DLL)",
      subtitle: "",
      icon: "mdi-waveform",
      shift: "SHIFT 1",
      schedule: "Mulai Pukul 07:00",
      available: true,
      doctors: ["dr. Fajar Nugroho, Sp.S"],
      shifts: [
        { name: "Shift 1", quota: 12 },
        { name: "Shift 2", quota: 10 }
      ],
      // Master config (default values, not in masterStore)
      totalQuota: 0,
      jamShiftList: [],
      autoShift: false,
      jadwalKlinik: [],
    },
    {
      id: 20,
      kode: "RT",
      name: "RADIOTERAPI",
      subtitle: "",
      icon: "mdi-radioactive",
      shift: "SHIFT 1",
      schedule: "Mulai Pukul 07:00",
      available: true,
      doctors: ["dr. Wulan Safitri, Sp.Rad"],
      shifts: [
        { name: "Shift 1", quota: 8 },
        { name: "Shift 2", quota: 6 }
      ],
      // Master config
      totalQuota: 500,
      jamShiftList: [{ dari: '07:00', sampai: '11:00', kuota: 500 }],
      autoShift: false,
      jadwalKlinik: ['Senin', 'Selasa', 'Rabu', 'Kamis', 'Jum\'at'],
    },
    {
      id: 21,
      kode: "RM",
      name: "REHAB MEDIK",
      subtitle: "",
      icon: "mdi-human-cane",
      shift: "SHIFT 1",
      schedule: "Mulai Pukul 07:00",
      available: true,
      doctors: ["dr. Gita Permata, Sp.KFR", "dr. Rian Saputra, Sp.KFR"],
      shifts: [
        { name: "Shift 1", quota: 16 },
        { name: "Shift 2", quota: 14 }
      ],
      // Master config
      totalQuota: 600,
      jamShiftList: [{ dari: '07:00', sampai: '11:00', kuota: 600 }],
      autoShift: false,
      jadwalKlinik: ['Senin', 'Rabu', 'Jumat'],
    },
    {
      id: 22,
      kode: "SR",
      name: "SARAF (NEUROLOGI)",
      subtitle: "",
      icon: "mdi-head-cog",
      shift: "SHIFT 1",
      schedule: "Mulai Pukul 07:00",
      available: false,
      doctors: [],
      shifts: [],
      // Master config
      totalQuota: 700,
      jamShiftList: [{ dari: '07:00', sampai: '11:00', kuota: 700 }],
      autoShift: false,
      jadwalKlinik: ['Senin', 'Rabu'],
    },
    {
      id: 23,
      kode: "TH", // kode fiktif untuk THT
      name: "THT",
      subtitle: "",
      icon: "mdi-ear-hearing",
      shift: "SHIFT 1",
      schedule: "Mulai Pukul 07:00",
      available: true,
      doctors: ["dr. Reza Maulana, Sp.THT-KL"],
      shifts: [
        { name: "Shift 1", quota: 12 },
        { name: "Shift 2", quota: 10 }
      ],
      // Master config (default values, not in masterStore)
      totalQuota: 0,
      jamShiftList: [],
      autoShift: false,
      jadwalKlinik: [],
    },
  ]);

  // Get all clinics
  const getAllClinics = computed(() => clinics.value);

  // Get clinic by name (untuk mapping dengan kode klinik)
  const getClinicByName = (name) => {
    return clinics.value.find(c => c.name === name);
  };

  // Get clinic by kode (lebih stabil untuk integrasi dengan master klinik & anjungan)
  const getClinicByKode = (kode) => {
    return clinics.value.find(c => c.kode === kode);
  };

  // Get clinics list untuk dropdown (format: { name, kode })
  // Sekarang langsung pakai kode dari clinic, tidak bergantung ke nama di masterStore
  const getClinicsForDropdown = () => {
    return clinics.value.map(clinic => ({
      name: clinic.name,
      kode: clinic.kode,
      icon: clinic.icon,
      available: clinic.available,
      id: clinic.id
    }));
  };

  // Actions untuk update master config (sync dengan masterStore operations)
  const updateClinicMasterConfig = (kode, masterConfig) => {
    const clinic = clinics.value.find(c => c.kode === kode);
    if (clinic) {
      clinic.totalQuota = masterConfig.totalQuota || clinic.totalQuota;
      clinic.jamShiftList = masterConfig.jamShiftList || clinic.jamShiftList;
      clinic.autoShift = masterConfig.autoShift !== undefined ? masterConfig.autoShift : clinic.autoShift;
      clinic.jadwalKlinik = masterConfig.jadwalKlinik || clinic.jadwalKlinik;
      return { success: true, clinic };
    }
    return { success: false, message: 'Clinic not found' };
  };

  return {
    clinics,
    getAllClinics,
    getClinicByName,
    getClinicByKode,
    getClinicsForDropdown,
    updateClinicMasterConfig,
  };
}, {
  persist: {
    key: 'clinic-store-state',
    storage: typeof window !== 'undefined' ? localStorage : undefined,
    paths: ['clinics'],
  },
});
