<?php

declare(strict_types=1);

use App\Enums\EmploymentStatus;
use App\Enums\Gender;
use App\Enums\NavigationGroup;
use App\Filament\Resources\ActivityLogs\ActivityLogResource;
use App\Filament\Resources\Departments\DepartmentResource;
use App\Models\User;
use BezhanSalleh\LanguageSwitch\Events\LocaleChanged;
use Filament\Forms\Components\Select;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Event;

it('returns translated enum labels for the active locale', function (): void {
    App::setLocale('ru');

    expect(Gender::Male->label())->toBe('Мужской');

    App::setLocale('tk');

    expect(Gender::Male->label())->toBe('Erkek');

    App::setLocale('en');

    expect(Gender::Male->label())->toBe('Male');
});

it('returns translated navigation group labels', function (): void {
    App::setLocale('ru');

    expect(NavigationGroup::Employees->getLabel())->toBe('Сотрудники');

    App::setLocale('tk');

    expect(NavigationGroup::LeaveManagement->getLabel())->toBe('Dynç alyş dolandyryşy');
});

it('returns translated enum options for filament selects', function (): void {
    App::setLocale('tk');

    $options = Select::make('employment_status')
        ->options(EmploymentStatus::class)
        ->getOptions();

    expect($options)->toBe([
        'active' => 'Işleýär',
        'inactive' => 'Wagtlaýyn işlemeýär',
        'terminated' => 'Işden boşadyldy',
        'on_leave' => 'Rugsatda',
    ]);
});

it('returns translated filament navigation group labels at request time', function (): void {
    $admin = createAdminUser(['locale' => 'tk']);

    $this->actingAs($admin);

    App::setLocale('tk');

    $groups = filament()->getNavigation();
    $labels = collect($groups)->map->getLabel()->filter()->values()->all();

    expect($labels)->toContain('Gurama', 'Işgärler', 'Dynç alyş dolandyryşy', 'Ýazgylar');
});

it('returns translated filament resource navigation labels', function (): void {
    App::setLocale('tk');

    expect(ActivityLogResource::getNavigationLabel())->toBe('Audit žurnaly');

    App::setLocale('ru');

    expect(ActivityLogResource::getNavigationLabel())->toBe('Журнал аудита');
});

it('returns translated filament resource model labels', function (): void {
    App::setLocale('tk');

    expect(DepartmentResource::getPluralModelLabel())->toBe('Bölümler');
    expect(DepartmentResource::getModelLabel())->toBe('Bölüm');

    App::setLocale('ru');

    expect(DepartmentResource::getPluralModelLabel())->toBe('Отделы');
    expect(DepartmentResource::getModelLabel())->toBe('Отдел');
});

it('returns translated filament panel strings for turkmen locale', function (): void {
    App::setLocale('tk');

    expect(__('filament-panels::global-search.field.placeholder'))->toBe('Gözleg');
    expect(__('filament-panels::resources/pages/list-records.breadcrumb'))->toBe('Sanaw');
    expect(__('filament-actions::create.single.label', ['label' => 'Bölüm']))->toBe('Döret');
});

it('persists locale when locale changed event fires', function (): void {
    $user = User::factory()->create(['locale' => 'en']);

    $this->actingAs($user);

    Event::dispatch(new LocaleChanged('ru'));

    expect($user->fresh()->locale)->toBe('ru');
});

it('defaults new users to english locale', function (): void {
    $user = User::factory()->create();

    expect($user->locale)->toBe('en');
});
