<?php

namespace App\Filament\Widgets;

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

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

    protected static ?int $sort = 2;

    public function table(Table $table): Table
    {
        return $table
            ->heading('Próximos Partidos')
            ->query($this->getTableQuery())
            ->paginated(false)
            ->columns([
                TextColumn::make('homeTeam.name')
                    ->label('Local')
                    ->weight('medium'),
                TextColumn::make('awayTeam.name')
                    ->label('Visitante')
                    ->weight('medium'),
                TextColumn::make('stage')
                    ->label('Etapa')
                    ->placeholder('-'),
                TextColumn::make('venue')
                    ->label('Estadio')
                    ->placeholder('-'),
                TextColumn::make('starts_at')
                    ->label('Empieza')
                    ->dateTime('M j, Y g:i A')
                    ->timezone('America/Mexico_City')
                    ->sortable(),
            ])
            ->defaultSort('starts_at')
            ->recordUrl(null);
    }

    protected function getTableQuery(): Builder
    {
        return Fixture::query()
            ->with(['homeTeam', 'awayTeam'])
            ->where('starts_at', '>=', now())
            ->orderBy('starts_at')
            ->limit(5);
    }
}
