<?php

namespace Database\Seeders;

use App\Models\OLDLMS\Healthcare;
use App\Models\OLDLMS\JadwalDokter;
use App\Models\OLDLMS\JadwalDokterDay;
use App\Models\OLDLMS\Dokter;
use App\Models\OLDLMS\User;
use App\Services\LmsApi;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;

class JadwalDokterSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $mapHealthcare = [
            // 'Q',
            // 'C',
            // 'D',
            // 'E',
            // 'F',
            // 'M',
            // 'O',
            // 'P',
            // 'Q',
            // 'N',
            // 'SG',
            // 'BW',
            // 'SK',
            // 'PK',
            // 'CK',
            // 'DE',
            'A',
            // 'B',
            // 'G',
            // 'I',
            // 'K',
            // 'R',
            // 'S'
        ];

        $healthcares = Healthcare::whereIn('sKodeRS', $mapHealthcare)->get();

        foreach ($healthcares as $healthcare) {
            $jadwalDokterDay = LmsApi::dokter($healthcare->sKodeRS, 'hospitaloka', 300);
            if (empty($jadwalDokterDay['data'])) {
                continue;
            }

            $listDokter = $jadwalDokterDay['data'];
            foreach ($listDokter as $dokter) {
                $jadwalDokter = JadwalDokter::where('sIDDokter', $dokter['id'])->where('sDepartmenID', $dokter['DepartemenID'])->first() ?? null;
                if ($jadwalDokter == null) {
                    continue;
                }
                $jadwalDokterApi = LmsApi::jadwalDokter($healthcare->sKodeRS, $dokter['id'], 'walkin');
              
                // if ($jadwalDokter->nID != 1901) {
                //     continue;
                // }

                $jadwalDokterDay = $jadwalDokter->jadwalDokterDay()->get() ?? null;
                // JadwalDokterDay::where('nIDJadwalDokter', $jadwalDokter->nID)->get() ?? null;
                if ($jadwalDokterDay == null) {
                    continue;
                }
                // dd($dokter);

                $listShcedule = $jadwalDokterApi['data'];
                foreach ($jadwalDokterDay as $day) {
                    $NewJam = "";
                    foreach ($listShcedule as $key => $schedule) {
                        // $jam = $schedule['Jam'];
                        // $cek = $schedule['Jam'];
                        // //jika  jam memiliki hurug A-Z
                        // if (preg_match('/[A-Z]/', $jam)) {
                        //     $jam = $jam;
                        // } else {
                        //     //jika terdapat titik
                        //     if (strpos($jam, ".") !== false) {
                        //         //ganti titik dengan : dan tambahkan 00
                        //         $jam = str_replace(".", ":", $jam);
                        //         //jika jam nya terdapat strip
                        //         if (strpos($jam, "-") !== false) {
                        //             //jika didepan strip dan belakang strip ada spasi
                        //             if (strpos($jam, " ") !== false) {
                        //                 //hapus spasi
                        //                 $jam = str_replace(" ", "", $jam);
                        //             }
                        //             //   try{
                        //             $jam = explode("-", $jam);
                        //             $jam = $jam[0] . " - " . $jam[1];
                        //             // } catch (\Exception $e) {
                        //             //     $this->command->error($cek . "Id dokter : " . $jadwalDokter->nID);
                        //             // }
                        //         } else {
                        //             $jam = $jam;
                        //         }
                        //     } else {
                        //         //jika terdapat strip 
                        //         if (strpos($jam, "-") !== false) {
                        //             //jika hanya ada strip tidak ada yang lain 
                        //             if (strlen($jam) > 1) {
                        //                 //jika sudah ada : maka diabaikan 
                        //                 if (strpos($jam, ":") !== false) {
                        //                     $jam = explode("-", $jam);
                        //                     $jam = $jam[0] . " - " . $jam[1];
                        //                 } else {
                        //                     //jika didepan strip ada spasi
                        //                     if (strpos($jam, " ") !== false) {
                        //                         //hapus spasi
                        //                         $jam = str_replace(" ", "", $jam);
                        //                     }
                        //                     $jam = explode("-", $jam);
                        //                     $jam = $jam[0] . ":00 - " . $jam[1] . ":00";
                        //                 }
                        //             }
                        //         } else if (strpos($jam, ":") !== false) {
                        //             $jam = $jam;
                        //         } else {
                        //             $jam = $jam . ":00";
                        //         }
                        //     }
                        // }
                        $jam = "";
                        if ($schedule['Jam'] != null){
                            $jamAwal = date('h:i', strtotime($schedule['Jam'][0]));
                            $jamAkhir = date('h:i', strtotime($schedule['Jam'][count($schedule['Jam'])-1]));
                            $jam = $jamAwal ." - ". $jamAkhir;
                        }
                        if ($day->sHari == $schedule['Hari']) {
                            if ($schedule['Jam'] == null) {
                                $NewJam = "";
                            } else {
                                $NewJam =  $jam;
                            }
                        } else {
                            continue;
                        }
                    }
                    $day->sJam = $NewJam;
                    $day->sJamTC = $NewJam;
                    $day->save();
                }
            }
        }
    }
}
