<?php

namespace App\Filament\Widgets;

use App\Filament\Resources\VendorApplications\VendorApplicationResource;
use App\Models\VendorApplication;
use Filament\Actions\Action;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
use Filament\Widgets\TableWidget as BaseWidget;

class PendingVendorApplications extends BaseWidget
{
    protected static ?string $heading = 'Pending vendor applications';

    protected static ?int $sort = 3;

    protected int|string|array $columnSpan = 'full';

    public function table(Table $table): Table
    {
        return $table
            ->query(
                VendorApplication::query()
                    ->whereIn('status', ['submitted', 'under_review'])
                    ->latest()
            )
            ->paginated(false)
            ->columns([
                TextColumn::make('id')->label('#')->sortable(),
                TextColumn::make('business_name')->searchable()->weight('semibold'),
                TextColumn::make('owner_name')->label('Owner'),
                TextColumn::make('phone'),
                TextColumn::make('locality'),
                TextColumn::make('status')
                    ->badge()
                    ->color(fn($state) => match($state) {
                        'submitted'    => 'warning',
                        'under_review' => 'info',
                        default        => 'gray',
                    }),
                TextColumn::make('created_at')
                    ->since()
                    ->label('Submitted'),
            ])
            ->recordActions([
                Action::make('review')
                    ->label('Review')
                    ->icon('heroicon-m-eye')
                    ->url(fn(VendorApplication $r) =>
                        VendorApplicationResource::getUrl('edit', ['record' => $r])
                    ),
            ])
            ->emptyStateHeading('No pending applications')
            ->emptyStateDescription('When vendors apply, they will appear here for review.')
            ->emptyStateIcon('heroicon-o-check-badge');
    }
}
