<?php

declare(strict_types=1);

namespace App\Providers;

use App\Models\Bonus;
use App\Models\Department;
use App\Models\DisciplinaryReport;
use App\Models\Employee;
use App\Models\EmployeeDocument;
use App\Models\Explanation;
use App\Models\Gift;
use App\Models\Position;
use App\Models\Shift;
use App\Models\SickLeave;
use App\Models\UnpaidLeave;
use App\Models\Vacation;
use App\Models\User;
use App\Observers\DisciplinaryReportObserver;
use App\Observers\EmployeeDocumentObserver;
use App\Observers\SickLeaveObserver;
use App\Observers\UnpaidLeaveObserver;
use App\Observers\VacationObserver;
use App\Policies\BonusPolicy;
use App\Policies\DepartmentPolicy;
use App\Policies\DisciplinaryReportPolicy;
use App\Policies\EmployeeDocumentPolicy;
use App\Policies\EmployeePolicy;
use App\Policies\ExplanationPolicy;
use App\Policies\GiftPolicy;
use App\Policies\PositionPolicy;
use App\Policies\ShiftPolicy;
use App\Policies\SickLeavePolicy;
use App\Policies\UnpaidLeavePolicy;
use App\Policies\UserPolicy;
use App\Policies\VacationPolicy;
use BezhanSalleh\LanguageSwitch\Events\LocaleChanged;
use BezhanSalleh\LanguageSwitch\LanguageSwitch;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\ServiceProvider;

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

    /**
     * Bootstrap any application services.
     */
    public function boot(): void
    {
        Gate::policy(Department::class, DepartmentPolicy::class);
        Gate::policy(Position::class, PositionPolicy::class);
        Gate::policy(Shift::class, ShiftPolicy::class);
        Gate::policy(Employee::class, EmployeePolicy::class);
        Gate::policy(Vacation::class, VacationPolicy::class);
        Gate::policy(SickLeave::class, SickLeavePolicy::class);
        Gate::policy(UnpaidLeave::class, UnpaidLeavePolicy::class);
        Gate::policy(DisciplinaryReport::class, DisciplinaryReportPolicy::class);
        Gate::policy(Explanation::class, ExplanationPolicy::class);
        Gate::policy(Bonus::class, BonusPolicy::class);
        Gate::policy(Gift::class, GiftPolicy::class);
        Gate::policy(EmployeeDocument::class, EmployeeDocumentPolicy::class);
        Gate::policy(User::class, UserPolicy::class);

        Vacation::observe(VacationObserver::class);
        SickLeave::observe(SickLeaveObserver::class);
        UnpaidLeave::observe(UnpaidLeaveObserver::class);
        DisciplinaryReport::observe(DisciplinaryReportObserver::class);
        EmployeeDocument::observe(EmployeeDocumentObserver::class);

        LanguageSwitch::configureUsing(function (LanguageSwitch $switch): void {
            $switch
                ->locales(config('hr.supported_locales'))
                ->labels(config('hr.locale_labels'))
                ->displayLocale('native')
                ->userPreferredLocale(fn () => user()->locale);
        });

        Event::listen(function (LocaleChanged $event): void {
            user()->update(['locale' => $event->locale]);
        });
    }
}
