<?php

namespace App\Filament\Resources\EmployeeDocuments\Tables;

use App\Enums\DocumentType;
use App\Models\EmployeeDocument;
use Filament\Actions\Action;
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\Tables\Columns\TextColumn;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Filters\TrashedFilter;
use Filament\Tables\Table;
use Illuminate\Support\Facades\Storage;

class EmployeeDocumentsTable
{
    public static function configure(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('employee.full_name')
                    ->label(__('hr.fields.employee'))
                    ->searchable()
                    ->sortable(),
                TextColumn::make('document_type')
                    ->label(__('hr.fields.type'))
                    ->badge()
                    ->color(fn (DocumentType $state): string => $state->color())
                    ->formatStateUsing(fn (DocumentType $state): string => $state->label())
                    ->searchable()
                    ->sortable(),
                TextColumn::make('title')
                    ->label(__('hr.fields.title'))
                    ->searchable()
                    ->sortable(),
                TextColumn::make('uploaded_at')
                    ->label(__('hr.fields.uploaded_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('document_type')
                    ->label(__('hr.fields.type'))
                    ->options(DocumentType::class),
                TrashedFilter::make(),
            ])
            ->recordActions([
                Action::make('download')
                    ->label(__('hr.actions.download'))
                    ->icon('heroicon-o-arrow-down-tray')
                    ->visible(fn (EmployeeDocument $record): bool => filled($record->file_path))
                    ->action(function (EmployeeDocument $record) {
                        $disk = Storage::disk(config('hr.file_uploads.disk'));

                        return response()->download(
                            $disk->path($record->file_path),
                            basename($record->file_path),
                        );
                    }),
                ViewAction::make(),
                EditAction::make(),
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make(),
                    ForceDeleteBulkAction::make(),
                    RestoreBulkAction::make(),
                ]),
            ]);
    }
}
