<?php

namespace App\Filament\Widgets;

use Filament\Tables;
use Filament\Widgets\TableWidget as BaseWidget;
use Illuminate\Support\Carbon;
use App\Models\Event;

class EventsActiveToday extends BaseWidget
{
    protected static ?string $heading = 'Events active today';
    protected ?string $pollingInterval = '60s';

    public function table(Tables\Table $table): Tables\Table
    {
        $today = Carbon::today()->toDateString();
        return $table
            ->query(
                Event::query()
                    ->where('is_active', true)
                    ->whereDate('date', '<=', $today)
                    ->withCount([
                        'photos as uploads_today' => function ($q) use ($today) {
                            $q->whereDate('created_at', $today);
                        },
                    ])
                    ->orderByDesc('date')
                    ->limit(10)
            )
            ->columns([
                Tables\Columns\TextColumn::make('id')->label('#')->width('60px'),
                Tables\Columns\TextColumn::make('slug')->label('Slug')->searchable(),
                Tables\Columns\TextColumn::make('date')->date(),
                Tables\Columns\TextColumn::make('uploads_today')->label('Uploads today')->numeric(),
            ])
            ->paginated(false);
    }
}
