<?php

namespace App\Filament\Widgets;

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

class LatestRequestsWidget extends TableWidget
{
    protected static ?string $heading = 'Latest Requests';

    protected static ?int $sort = 2;

    protected int | string | array $columnSpan = 1;

    public function table(Table $table): Table
    {
        return $table
            ->query(fn (): Builder => ApiRequest::query()
                ->with('website')
                ->latest()
                ->limit(5)
            )
            ->columns([
                TextColumn::make('website.name')
                    ->label('Website'),
                TextColumn::make('registration_number')
                    ->label('Reg'),
                TextColumn::make('response_status')
                    ->label('Status')
                    ->badge(),
                TextColumn::make('created_at')
                    ->label('When')
                    ->since()
                    ->alignEnd(),
            ])
            ->searchable(false)
            ->paginated(false);
    }
}
