<?php

namespace Database\Seeders;

use App\Models\OLDLMS\Healthcare;
use App\Models\OLDLMS\JadwalDokter;
use App\Models\OLDLMS\Speciality;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class UpdateTarifLMSSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        // [
        //     [
        //       "sKodeRS" => "A",
        //       "sHealthCare" => "Rumah Sakit Awal Bros Sudirman Pekanbaru",
        //     ],
        //     [
        //       "sKodeRS" => "B",
        //       "sHealthCare" => "Rumah Sakit Awal Bros Batam",
        //     ],
        //     [
        //       "sKodeRS" => "BW",
        //       "sHealthCare" => "Primaya Hospital Bhakti Wara",
        //     ],
        //     [
        //       "sKodeRS" => "C",
        //       "sHealthCare" => "Primaya Hospital Tangerang",
        //     ],
        //     [
        //       "sKodeRS" => "D",
        //       "sHealthCare" => "Primaya Hospital Bekasi Barat",
        //     ],
        //     [
        //       "sKodeRS" => "E",
        //       "sHealthCare" => "Primaya Evasari Hospital",
        //     ],
        //     [
        //       "sKodeRS" => "F",
        //       "sHealthCare" => "Primaya Hospital Makassar",
        //     ],
        //     [
        //       "sKodeRS" => "G",
        //       "sHealthCare" => "Rumah Sakit Awal Bros Ujung Batu",
        //     ],
        //     [
        //       "sKodeRS" => "I",
        //       "sHealthCare" => "Rumah Sakit Awal Bros Panam",
        //     ],
        //     [
        //       "sKodeRS" => "K",
        //       "sHealthCare" => "Rumah Sakit Awal Bros Ahmad Yani",
        //     ],
        //     [
        //       "sKodeRS" => "M",
        //       "sHealthCare" => "Primaya Hospital Bekasi Timur",
        //     ],
        //     [
        //       "sKodeRS" => "N",
        //       "sHealthCare" => "Primaya Hospital Betang Pambelum",
        //     ],
        //     [
        //       "sKodeRS" => "O",
        //       "sHealthCare" => "Primaya Hospital Inco Sorowako",
        //     ],
        //     [
        //       "sKodeRS" => "P",
        //       "sHealthCare" => "Primaya Hospital Bekasi Utara",
        //     ],
        //     [
        //       "sKodeRS" => "PK",
        //       "sHealthCare" => "Primaya Hospital Pasar Kemis",
        //     ],
        //     [
        //       "sKodeRS" => "Q",
        //       "sHealthCare" => "Primaya Hospital Karawang",
        //     ],
        //     [
        //       "sKodeRS" => "SK",
        //       "sHealthCare" => "Primaya Hospital Sukabumi",
        //     ],
        //   ]

        $dataTarifs = [
            // [
            //     'adm_rajal' => 0,

            //     'appt_umum' => null,
            //     'walkin_umum' => null,
            //     'telekonsul_umum' => null,

            //     'appt_spesialis' => 250000,
            //     'walkin_spesialis' => 250000,
            //     'telekonsul_spesialis' => 213000,

            //     'appt_subspesialis' => 345000,
            //     'walkin_subspesialis' => 345000,
            //     'telekonsul_subspesialis' => 293000,

            //     'applied_to' => ['A', 'KODERSLMS']
            // ],
            [
                'adm_rajal' => 55000,

                'appt_umum' => 120000,
                'walkin_umum' => 120000,
                'telekonsul_umum' => 102000,

                'appt_spesialis' => 250000,
                'walkin_spesialis' => 250000,
                'telekonsul_spesialis' => 213000,

                'appt_subspesialis' => 345000,
                'walkin_subspesialis' => 345000,
                'telekonsul_subspesialis' => 293000,

                'applied_to' => ['P']
            ]
        ];

        $spesialisUmum = Speciality::where('sSlug', 'general-practitioner')->first();

        foreach ($dataTarifs as $tarif) {
            $healthcareAppliedIds = Healthcare::whereIn('sKodeRS', $tarif['applied_to'])->pluck('nID');

            // [MARK] : UMUM
            // Update Tarif Dokter Umum
            $updateUmum = [];
            if ($tarif['walkin_umum'] != null) {
                $updateUmum['nBiaya'] = $tarif['walkin_umum'] + $tarif['adm_rajal'];
            }
            if ($tarif['telekonsul_umum'] != null) {
                $updateUmum['nBiayaTC'] = $tarif['telekonsul_umum'] + $tarif['adm_rajal'];
                $updateUmum['nBiayaATC'] = $tarif['telekonsul_umum'] + $tarif['adm_rajal'];
            }
            if (count($updateUmum)) {
                // $updateUmum['dUpdateOn'] = now();
                // Updating
                $dokUmum = JadwalDokter::query()
                    ->where('nIDSpesialis', $spesialisUmum->nID)
                    ->whereIn('nIDHealthCare', $healthcareAppliedIds)
                    ->withTrashed()
                    ->update($updateUmum);
                $this->command->info('Updating : Dokter Umum');
            }

            // [MARK] : SPESIALIS
            // Update Tarif Dokter Spesialis
            $updateSpesialis = [];
            if ($tarif['walkin_spesialis'] != null) {
                $updateSpesialis['nBiaya'] = $tarif['walkin_spesialis'] + $tarif['adm_rajal'];
            }
            if ($tarif['telekonsul_spesialis'] != null) {
                $updateSpesialis['nBiayaTC'] = $tarif['telekonsul_spesialis'] + $tarif['adm_rajal'];
                $updateSpesialis['nBiayaATC'] = $tarif['telekonsul_spesialis'] + $tarif['adm_rajal'];
            }
            if (count($updateSpesialis)) {
                // $updateSpesialis['dUpdateOn'] = now();
                // Updating
                $dokSpesialis = JadwalDokter::query()
                    ->whereNot(function ($q) use ($spesialisUmum) {
                        $q->where('nIDSpesialis', $spesialisUmum->nID);
                    })
                    ->where('sIsSubSpesialis', 0)
                    ->whereIn('nIDHealthCare', $healthcareAppliedIds)
                    ->withTrashed()
                    ->update($updateSpesialis);
                $this->command->info('Updating : Dokter Spesialis');
            }


            // [MARK] : SUBSPESIALIS
            // Update Tarif Dokter Sub Spesialis
            $updateSubSpesialis = [];
            if ($tarif['walkin_subspesialis'] != null) {
                $updateSubSpesialis['nBiaya'] = $tarif['walkin_subspesialis'] + $tarif['adm_rajal'];
            }
            if ($tarif['telekonsul_subspesialis'] != null) {
                $updateSubSpesialis['nBiayaTC'] = $tarif['telekonsul_subspesialis'] + $tarif['adm_rajal'];
                $updateSubSpesialis['nBiayaATC'] = $tarif['telekonsul_subspesialis'] + $tarif['adm_rajal'];
            }
            if (count($updateSubSpesialis)) {
                // $updateSubSpesialis['dUpdateOn'] = now();

                // Updating
                $dokSubSpesialis = JadwalDokter::query()
                    ->whereNot(function ($q) use ($spesialisUmum) {
                        $q->where('nIDSpesialis', $spesialisUmum->nID);
                    })
                    ->where('sIsSubSpesialis', 1)
                    ->whereIn('nIDHealthCare', $healthcareAppliedIds)
                    ->withTrashed()
                    ->update($updateSubSpesialis);
                $this->command->info('Updating : Dokter Sub Spesialis');
            }
        }
    }
}
