<?php

declare(strict_types=1);

namespace App\Filament\Resources\Employees\RelationManagers;

use App\Enums\DisciplinarySeverity;
use App\Filament\Support\HrForm;
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\DatePicker;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Textarea;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Schemas\Schema;
use Filament\Tables\Columns\IconColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Filters\TrashedFilter;
use Filament\Tables\Table;

class DisciplinaryReportsRelationManager extends RelationManager
{
    protected static string $relationship = 'disciplinaryReports';

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

    public function form(Schema $schema): Schema
    {
        return $schema
            ->components([
                DatePicker::make('report_date')
                    ->label(__('hr.fields.report_date'))
                    ->required()
                    ->default(now()),
                TextInput::make('title')
                    ->label(__('hr.fields.title'))
                    ->required()
                    ->maxLength(255),
                Textarea::make('description')
                    ->label(__('hr.fields.description'))
                    ->required()
                    ->rows(4)
                    ->columnSpanFull(),
                Select::make('severity')
                    ->label(__('hr.fields.severity'))
                    ->options(DisciplinarySeverity::class)
                    ->required()
                    ->native(false),
                HrForm::fileUpload('attachment_path')
                    ->label(__('hr.fields.attachment'))
                    ->acceptedFileTypes(['application/pdf', 'image/*'])
                    ->maxSize(10240),
            ]);
    }

    public function table(Table $table): Table
    {
        return $table
            ->recordTitleAttribute('title')
            ->columns([
                TextColumn::make('report_date')
                    ->label(__('hr.fields.report_date'))
                    ->date()
                    ->sortable(),
                TextColumn::make('title')
                    ->label(__('hr.fields.title'))
                    ->searchable()
                    ->sortable(),
                TextColumn::make('severity')
                    ->label(__('hr.fields.severity'))
                    ->badge()
                    ->color(fn (DisciplinarySeverity $state): string => $state->color())
                    ->formatStateUsing(fn (DisciplinarySeverity $state): string => $state->label())
                    ->sortable(),
                IconColumn::make('attachment_path')
                    ->label(__('hr.fields.attachment'))
                    ->boolean()
                    ->trueIcon('heroicon-o-paper-clip')
                    ->falseIcon('heroicon-o-minus'),
                TextColumn::make('creator.name')
                    ->label(__('hr.fields.created_by'))
                    ->toggleable(),
            ])
            ->filters([
                SelectFilter::make('severity')
                    ->label(__('hr.fields.severity'))
                    ->options(DisciplinarySeverity::class),
                TrashedFilter::make(),
            ])
            ->headerActions([
                CreateAction::make(),
            ])
            ->recordActions([
                EditAction::make(),
                DeleteAction::make(),
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make(),
                    ForceDeleteBulkAction::make(),
                    RestoreBulkAction::make(),
                ]),
            ]);
    }
}
