<?php

namespace App\Filament\Widgets;

use App\Models\ClickLog;
use App\Models\InterstitialAd;
use App\Models\Link;
use App\Models\Report;
use App\Models\User;
use Filament\Support\Icons\Heroicon;
use Filament\Widgets\StatsOverviewWidget;
use Filament\Widgets\StatsOverviewWidget\Stat;

class AdminStatsOverview extends StatsOverviewWidget
{
    protected ?string $heading = 'Platform Pulse';

    protected ?string $description = 'Realtime operational health across urless.';

    protected ?string $pollingInterval = '10s';

    protected static ?int $sort = 1;

    /**
     * @return array<Stat>
     */
    protected function getStats(): array
    {
        $todayClicks = ClickLog::query()
            ->where('clicked_at', '>=', now()->startOfDay())
            ->count();

        return [
            Stat::make('Total Users', number_format(User::query()->count()))
                ->description('Active accounts and admins')
                ->descriptionIcon(Heroicon::Users)
                ->icon(Heroicon::Users)
                ->color('primary'),

            Stat::make('Short Links', number_format(Link::query()->count()))
                ->description(number_format(Link::query()->where('status', 'active')->count()).' active')
                ->descriptionIcon(Heroicon::Link)
                ->icon(Heroicon::Link)
                ->color('info'),

            Stat::make('Clicks Today', number_format($todayClicks))
                ->description(number_format(ClickLog::query()->count()).' lifetime clicks')
                ->descriptionIcon(Heroicon::ArrowTrendingUp)
                ->icon(Heroicon::CursorArrowRays)
                ->color('success'),

            Stat::make('Active Ads', number_format(InterstitialAd::query()->where('status', 'active')->count()))
                ->description(number_format(Report::query()->whereIn('status', ['open', 'pending'])->count()).' reports need review')
                ->descriptionIcon(Heroicon::Megaphone)
                ->icon(Heroicon::Megaphone)
                ->color('warning'),
        ];
    }
}
