<?php

declare(strict_types=1);

namespace App\Filament\Resources\Employees\RelationManagers;

use App\Enums\DocumentType;
use App\Filament\Support\HrForm;
use App\Models\EmployeeDocument;
use Filament\Actions\Action;
use Filament\Actions\BulkActionGroup;
use Filament\Actions\CreateAction;
use Filament\Actions\DeleteAction;
use Filament\Actions\DeleteBulkAction;
use Filament\Actions\EditAction;
use Filament\Actions\ForceDeleteBulkAction;
use Filament\Actions\RestoreBulkAction;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Schemas\Schema;
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 DocumentsRelationManager extends RelationManager
{
    protected static string $relationship = 'documents';

    public static function getTitle(\Illuminate\Database\Eloquent\Model $ownerRecord, string $pageClass): string
    {
        return __("hr.relation_managers.employee_documents");
    }

    public function form(Schema $schema): Schema
    {
        return $schema
            ->components([
                Select::make('document_type')
                    ->label(__('hr.fields.document_type'))
                    ->options(DocumentType::class)
                    ->required()
                    ->native(false),
                TextInput::make('title')
                    ->label(__('hr.fields.title'))
                    ->required()
                    ->maxLength(255),
                HrForm::fileUpload('file_path')
                    ->label(__('hr.fields.document'))
                    ->required()
                    ->acceptedFileTypes(['application/pdf', 'image/*', 'application/msword', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'])
                    ->maxSize(15360),
            ]);
    }

    public function table(Table $table): Table
    {
        return $table
            ->recordTitleAttribute('title')
            ->columns([
                TextColumn::make('document_type')
                    ->label(__('hr.fields.type'))
                    ->badge()
                    ->color(fn (DocumentType $state): string => $state->color())
                    ->formatStateUsing(fn (DocumentType $state): string => $state->label())
                    ->sortable(),
                TextColumn::make('title')
                    ->label(__('hr.fields.title'))
                    ->searchable()
                    ->sortable(),
                TextColumn::make('uploaded_at')
                    ->label(__('hr.fields.uploaded_at'))
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
            ])
            ->filters([
                SelectFilter::make('document_type')
                    ->label(__('hr.fields.type'))
                    ->options(DocumentType::class),
                TrashedFilter::make(),
            ])
            ->headerActions([
                CreateAction::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),
                        );
                    }),
                EditAction::make(),
                DeleteAction::make(),
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make(),
                    ForceDeleteBulkAction::make(),
                    RestoreBulkAction::make(),
                ]),
            ]);
    }
}
