<?php

namespace App\Providers;

use App\Rules\NikRule;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\ServiceProvider;
use App\Models\Corporate;
use App\Models\CorporateFormularium;
use App\Models\CorporateService;
use App\Models\CorporatePlan;
use App\Models\CorporateBenefit;
use App\Models\Member;
use App\Models\MemberPlan;
use App\Models\CorporateHospital;
use App\Models\ExclusionRules;
use App\Models\ExclusionImport;
use App\Models\Icd;
use App\Models\IcdTemplate;
use App\Models\FormulariumTemplate;
use App\Models\AuditTrail;
use App\Models\RequestLog;
use App\Models\RequestLogBenefit;
use Illuminate\Support\Facades\Auth;

use App\Models\OLDLMS\PersonalAccessToken;
use Laravel\Sanctum\Sanctum;

use Str;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Schema::defaultStringLength(191);

        Str::macro('initials', fn ($value, $sep = ' ', $glue = '') => trim(collect(explode($sep, $value))->map(function ($segment) {
            return $segment[0] ?? '';
        })->join($glue)));

        // Menambahkan event listener untuk jejak audit pada model yang ingin di-audit
        // Corporate::created(function ($model) {
        //     $this->logAuditTrail($model, 'created');
        // });

        Corporate::updated(function ($model) {

            $this->logAuditTrail($model, 'updated');
        });

        Corporate::deleted(function ($model) {
            $this->logAuditTrail($model, 'deleted');
        });

        Member::updated(function ($model) {

            $this->logAuditTrail($model, 'updated');
        });

        MemberPlan::updated(function ($model) {

            $this->logAuditTrail($model, 'updated');
        });

        Member::deleted(function ($model) {
            $this->logAuditTrail($model, 'deleted');
        });

        MemberPlan::deleted(function ($model) {

            $this->logAuditTrail($model, 'deleted');
        });

        //Hospital
        CorporateHospital::updated(function ($model) {

            $this->logAuditTrail($model, 'updated');
        });

        CorporateHospital::deleted(function ($model) {
            $this->logAuditTrail($model, 'deleted');
        });


        // Corporate Service
        CorporateService::updated(function ($model) {

            $this->logAuditTrail($model, 'updated');
        });

        CorporateService::deleted(function ($model) {
            $this->logAuditTrail($model, 'deleted');
        });

        // Corporate Plans
        CorporatePlan::updated(function ($model) {

            $this->logAuditTrail($model, 'updated');
        });

        CorporatePlan::deleted(function ($model) {
            $this->logAuditTrail($model, 'deleted');
        });

        // Corporate Benefits
        CorporateBenefit::updated(function ($model) {

            $this->logAuditTrail($model, 'updated');
        });

        CorporateBenefit::deleted(function ($model) {
            $this->logAuditTrail($model, 'deleted');
        });


        // Corporate Exclusion
        ExclusionRules::updated(function ($model) {
            $this->logAuditTrail($model, 'updated');
        });

        ExclusionRules::deleted(function ($model) {
            $this->logAuditTrail($model, 'deleted');
        });

        ExclusionImport::updated(function ($model) {

            $this->logAuditTrail($model, 'updated');
        });

        ExclusionImport::deleted(function ($model) {
            $this->logAuditTrail($model, 'deleted');
        });

        // ICD or exlusion
        Icd::updated(function ($model) {
            $this->logAuditTrail($model, 'updated');
        });

        Icd::deleted(function ($model) {
            $this->logAuditTrail($model, 'deleted');
        });

        // ICD or exlusion
        IcdTemplate::updated(function ($model) {
            $this->logAuditTrail($model, 'updated');
        });
        IcdTemplate::deleted(function ($model) {
            $this->logAuditTrail($model, 'deleted');
        });

        // Formualrium Template
        FormulariumTemplate::updated(function ($model) {
            $this->logAuditTrail($model, 'updated');
        });
        FormulariumTemplate::deleted(function ($model) {
            $this->logAuditTrail($model, 'deleted');
        });

        // Request LOG Template
        RequestLog::updated(function ($model) {
            $this->logAuditTrail($model, 'updated');
        });
        RequestLog::deleted(function ($model) {
            $this->logAuditTrail($model, 'deleted');
        });

        // Request LOG Template
        RequestLogBenefit::updated(function ($model) {
            $this->logAuditTrail($model, 'updated');
        });
        RequestLogBenefit::deleted(function ($model) {
            $this->logAuditTrail($model, 'deleted');
        });

        // Formualrium Template
        CorporateFormularium::updated(function ($model) {
            $this->logAuditTrail($model, 'updated');
        });
        CorporateFormularium::deleted(function ($model) {
            $this->logAuditTrail($model, 'deleted');
        });

        Sanctum::usePersonalAccessTokenModel(PersonalAccessToken::class);
    }

    private function logAuditTrail($model, $action)
    {
        // Membuat jejak audit baru
        $auditTrail = new AuditTrail([
            'model' => get_class($model),
            'model_id' => $model->getKey(),
            'action' => $action,
            'old_values' => json_encode($model->getOriginal()),
            'new_values' => json_encode($model->getAttributes()),
            'user_id' => Auth::id(),
        ]);

        // Simpan jejak audit
        $auditTrail->save();
    }
    private function logAuditTrailExclusion($model, $action)
    {
        // Membuat jejak audit baru
        $auditTrail = new AuditTrail([
            'model' => get_class($model),
            'model_id' => $model->corporate_id,
            'action' => $action,
            'old_values' => json_encode($model->getOriginal()),
            'new_values' => json_encode($model->getAttributes()),
            'user_id' => Auth::id(),
        ]);

        // Simpan jejak audit
        $auditTrail->save();
    }
}
