<?php

namespace App\Filament\Resources\Employees\Tables;

use App\Enums\EmploymentStatus;
use App\Enums\Gender;
use App\Exports\EmployeesExport;
use App\Filament\Support\ExportExcelAction;
use App\Models\Department;
use App\Models\Employee;
use App\Support\Countries;
use Filament\Actions\BulkAction;
use Filament\Actions\BulkActionGroup;
use Filament\Actions\DeleteBulkAction;
use Filament\Actions\EditAction;
use Filament\Actions\ForceDeleteBulkAction;
use Filament\Actions\RestoreBulkAction;
use Filament\Actions\ViewAction;
use Filament\Forms\Components\Select;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Filters\TrashedFilter;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Collection;

class EmployeesTable
{
    public static function configure(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('employee_number')
                    ->label(__('hr.fields.number'))
                    ->searchable()
                    ->sortable(),
                TextColumn::make('full_name')
                    ->label(__('hr.fields.full_name'))
                    ->searchable()
                    ->sortable(),
                TextColumn::make('national_id')
                    ->label(__('hr.fields.national_id'))
                    ->formatStateUsing(fn (?string $state): ?string => Countries::name($state) ?? $state)
                    ->searchable()
                    ->toggleable(),
                TextColumn::make('gender')
                    ->label(__('hr.fields.gender'))
                    ->badge()
                    ->color(fn (Gender $state): string => $state->color())
                    ->formatStateUsing(fn (Gender $state): string => $state->label())
                    ->searchable(),
                TextColumn::make('phone')
                    ->label(__('hr.fields.phone'))
                    ->searchable()
                    ->toggleable(),
                TextColumn::make('department.name')
                    ->label(__('hr.fields.department'))
                    ->badge()
                    ->color('info')
                    ->searchable()
                    ->sortable(),
                TextColumn::make('position.name')
                    ->label(__('hr.fields.position'))
                    ->badge()
                    ->color('primary')
                    ->searchable()
                    ->sortable(),
                TextColumn::make('shift.name')
                    ->label(__('hr.fields.shift'))
                    ->badge()
                    ->color('gray')
                    ->searchable()
                    ->sortable(),
                TextColumn::make('employment_status')
                    ->label(__('hr.fields.status'))
                    ->badge()
                    ->color(fn (EmploymentStatus $state): string => $state->color())
                    ->formatStateUsing(fn (EmploymentStatus $state): string => $state->label())
                    ->searchable()
                    ->sortable(),
                TextColumn::make('hire_date')
                    ->label(__('hr.fields.hire_date'))
                    ->date()
                    ->sortable(),
                TextColumn::make('termination_date')
                    ->label(__('hr.fields.termination_date'))
                    ->date()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
                TextColumn::make('created_at')
                    ->label(__('hr.fields.created_at'))
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
                TextColumn::make('updated_at')
                    ->label(__('hr.fields.updated_at'))
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
                TextColumn::make('deleted_at')
                    ->label(__('hr.fields.deleted_at'))
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
            ])
            ->filters([
                SelectFilter::make('department_id')
                    ->label(__('hr.fields.department'))
                    ->relationship('department', 'name')
                    ->searchable()
                    ->preload(),
                SelectFilter::make('position_id')
                    ->label(__('hr.fields.position'))
                    ->relationship('position', 'name')
                    ->searchable()
                    ->preload(),
                SelectFilter::make('shift_id')
                    ->label(__('hr.fields.shift'))
                    ->relationship('shift', 'name')
                    ->searchable()
                    ->preload(),
                SelectFilter::make('employment_status')
                    ->label(__('hr.fields.employment_status'))
                    ->options(EmploymentStatus::class),
                TrashedFilter::make(),
            ])
            ->recordActions([
                ViewAction::make(),
                EditAction::make(),
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    ExportExcelAction::bulk('exportSelected', EmployeesExport::class, 'employees.xlsx'),
                    BulkAction::make('changeDepartment')
                        ->label(__('hr.actions.change_department'))
                        ->icon('heroicon-o-building-office-2')
                        ->schema([
                            Select::make('department_id')
                                ->label(__('hr.fields.department'))
                                ->options(fn (): array => Department::query()->orderBy('name')->pluck('name', 'id')->all())
                                ->searchable()
                                ->required(),
                        ])
                        ->action(function (Collection $records, array $data): void {
                            $records->each(
                                fn (Employee $employee) => $employee->update([
                                    'department_id' => $data['department_id'],
                                ]),
                            );
                        })
                        ->deselectRecordsAfterCompletion(),
                    DeleteBulkAction::make(),
                    ForceDeleteBulkAction::make(),
                    RestoreBulkAction::make(),
                ]),
            ]);
    }
}
