<?php

declare(strict_types=1);

namespace App\Filament\Widgets;

use App\Enums\DisciplinarySeverity;
use App\Models\DisciplinaryReport;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
use Filament\Widgets\TableWidget;
use Illuminate\Database\Eloquent\Builder;

class RecentReportsWidget extends TableWidget
{
    protected int | string | array $columnSpan = 'full';

    public function getHeading(): ?string
    {
        return __('hr.widgets.recent_disciplinary_reports');
    }

    public function table(Table $table): Table
    {
        return $table
            ->query(
                DisciplinaryReport::query()
                    ->with(['employee', 'creator'])
                    ->latest('report_date')
                    ->limit(5),
            )
            ->columns([
                TextColumn::make('employee.full_name')
                    ->label(__('hr.fields.employee')),
                TextColumn::make('report_date')
                    ->date(),
                TextColumn::make('title')
                    ->limit(40),
                TextColumn::make('severity')
                    ->badge()
                    ->color(fn (DisciplinarySeverity $state): string => $state->color())
                    ->formatStateUsing(fn (DisciplinarySeverity $state): string => $state->label()),
                TextColumn::make('creator.name')
                    ->label(__('hr.fields.created_by')),
            ])
            ->paginated(false);
    }

    /**
     * @return Builder<DisciplinaryReport>|null
     */
    protected function getTableQuery(): ?Builder
    {
        return null;
    }
}
