<?php

declare(strict_types=1);

namespace App\Filament\Resources\Employees;

use App\Enums\NavigationGroup;
use App\Filament\Concerns\HasHrResourceLabels;
use App\Filament\Resources\Employees\Pages\CreateEmployee;
use App\Filament\Resources\Employees\Pages\EditEmployee;
use App\Filament\Resources\Employees\Pages\ListEmployees;
use App\Filament\Resources\Employees\Pages\ViewEmployee;
use App\Filament\Resources\Employees\RelationManagers\BonusesRelationManager;
use App\Filament\Resources\Employees\RelationManagers\DisciplinaryReportsRelationManager;
use App\Filament\Resources\Employees\RelationManagers\DocumentsRelationManager;
use App\Filament\Resources\Employees\RelationManagers\ExplanationsRelationManager;
use App\Filament\Resources\Employees\RelationManagers\GiftsRelationManager;
use App\Filament\Resources\Employees\RelationManagers\SickLeavesRelationManager;
use App\Filament\Resources\Employees\RelationManagers\UnpaidLeavesRelationManager;
use App\Filament\Resources\Employees\RelationManagers\VacationsRelationManager;
use App\Filament\Resources\Employees\Schemas\EmployeeForm;
use App\Filament\Resources\Employees\Schemas\EmployeeInfolist;
use App\Filament\Resources\Employees\Tables\EmployeesTable;
use App\Models\Employee;
use BackedEnum;
use Filament\Resources\Resource;
use Filament\Schemas\Schema;
use Filament\Support\Icons\Heroicon;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use UnitEnum;

class EmployeeResource extends Resource
{
    use HasHrResourceLabels;

    protected static ?string $model = Employee::class;

    protected static function hrLabelKeys(): array
    {
        return [
            'model' => 'employee',
            'plural' => 'employees',
        ];
    }

    protected static string|BackedEnum|null $navigationIcon = Heroicon::OutlinedUsers;

    protected static string|UnitEnum|null $navigationGroup = NavigationGroup::Employees;

    protected static ?int $navigationSort = 1;

    protected static ?string $recordTitleAttribute = 'full_name';

    protected static bool $isGloballySearchable = true;

    public static function form(Schema $schema): Schema
    {
        return EmployeeForm::configure($schema);
    }

    public static function infolist(Schema $schema): Schema
    {
        return EmployeeInfolist::configure($schema);
    }

    public static function table(Table $table): Table
    {
        return EmployeesTable::configure($table);
    }

    /**
     * @return array<string>
     */
    public static function getGloballySearchableAttributes(): array
    {
        return [
            'employee_number',
            'full_name',
            'phone',
            'department.name',
            'position.name',
        ];
    }

    /**
     * @return array<string, string>
     */
    public static function getGlobalSearchResultDetails(Model $record): array
    {
        /** @var Employee $record */
        return [
            __('hr.fields.employee_number') => $record->employee_number,
            __('hr.fields.department') => $record->department?->name ?? '—',
            __('hr.fields.position') => $record->position?->name ?? '—',
        ];
    }

    public static function getRelations(): array
    {
        return [
            VacationsRelationManager::class,
            SickLeavesRelationManager::class,
            UnpaidLeavesRelationManager::class,
            DisciplinaryReportsRelationManager::class,
            ExplanationsRelationManager::class,
            BonusesRelationManager::class,
            GiftsRelationManager::class,
            DocumentsRelationManager::class,
        ];
    }

    public static function getPages(): array
    {
        return [
            'index' => ListEmployees::route('/'),
            'create' => CreateEmployee::route('/create'),
            'view' => ViewEmployee::route('/{record}'),
            'edit' => EditEmployee::route('/{record}/edit'),
        ];
    }

    public static function getRecordRouteBindingEloquentQuery(): Builder
    {
        return parent::getRecordRouteBindingEloquentQuery()
            ->withoutGlobalScopes([
                SoftDeletingScope::class,
            ]);
    }
}
