<?php

namespace App\Filament\Resources\Vacations\Tables;

use App\Enums\ApprovalStatus;
use App\Enums\VacationType;
use App\Models\Vacation;
use App\Services\Vacation\VacationService;
use Filament\Actions\Action;
use App\Filament\Support\ExportExcelAction;
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\DatePicker;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\Filter;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Filters\TrashedFilter;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\Auth;

class VacationsTable
{
    public static function configure(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('employee.full_name')
                    ->label(__('hr.fields.employee'))
                    ->searchable()
                    ->sortable(),
                TextColumn::make('type')
                    ->label(__('hr.fields.type'))
                    ->badge()
                    ->color(fn (VacationType $state): string => $state->color())
                    ->formatStateUsing(fn (VacationType $state): string => $state->label())
                    ->searchable()
                    ->sortable(),
                TextColumn::make('start_date')
                    ->label(__('hr.fields.start_date'))
                    ->date()
                    ->sortable(),
                TextColumn::make('end_date')
                    ->label(__('hr.fields.end_date'))
                    ->date()
                    ->sortable(),
                TextColumn::make('days')
                    ->label(__('hr.fields.days'))
                    ->numeric()
                    ->sortable(),
                TextColumn::make('status')
                    ->label(__('hr.fields.status'))
                    ->badge()
                    ->color(fn (ApprovalStatus $state): string => $state->color())
                    ->formatStateUsing(fn (ApprovalStatus $state): string => $state->label())
                    ->searchable()
                    ->sortable(),
                TextColumn::make('approver.name')
                    ->label(__('hr.fields.approved_by'))
                    ->toggleable(isToggledHiddenByDefault: true),
                TextColumn::make('approved_at')
                    ->label(__('hr.fields.approved_at'))
                    ->dateTime()
                    ->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('status')
                    ->label(__('hr.fields.status'))
                    ->options(ApprovalStatus::class),
                SelectFilter::make('type')
                    ->label(__('hr.fields.type'))
                    ->options(VacationType::class),
                Filter::make('date_range')
                    ->label(__('hr.fields.date_range'))
                    ->schema([
                        DatePicker::make('start_from')
                            ->label(__('hr.fields.start_from')),
                        DatePicker::make('start_until')
                            ->label(__('hr.fields.start_until')),
                    ])
                    ->query(function (Builder $query, array $data): Builder {
                        return $query
                            ->when(
                                $data['start_from'],
                                fn (Builder $query, $date): Builder => $query->whereDate('start_date', '>=', $date),
                            )
                            ->when(
                                $data['start_until'],
                                fn (Builder $query, $date): Builder => $query->whereDate('start_date', '<=', $date),
                            );
                    }),
                TrashedFilter::make(),
            ])
            ->recordActions([
                Action::make('approve')
                    ->label(__('hr.actions.approve'))
                    ->icon('heroicon-o-check-badge')
                    ->color('success')
                    ->requiresConfirmation()
                    ->visible(fn (Vacation $record): bool => $record->status === ApprovalStatus::Pending)
                    ->action(function (Vacation $record, VacationService $vacationService): void {
                        $vacationService->approve($record, Auth::user());
                    }),
                Action::make('reject')
                    ->label(__('hr.actions.reject'))
                    ->icon('heroicon-o-x-mark')
                    ->color('danger')
                    ->requiresConfirmation()
                    ->visible(fn (Vacation $record): bool => $record->status === ApprovalStatus::Pending)
                    ->action(function (Vacation $record, VacationService $vacationService): void {
                        $vacationService->reject($record, Auth::user());
                    }),
                ViewAction::make(),
                EditAction::make(),
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    ExportExcelAction::bulk('exportSelected', \App\Exports\VacationsExport::class, 'vacations.xlsx'),
                    DeleteBulkAction::make(),
                    ForceDeleteBulkAction::make(),
                    RestoreBulkAction::make(),
                ]),
            ]);
    }
}
