<?php

namespace App\Filament\Widgets;

use App\Models\Booking;
use App\Models\PlatformConfig;
use App\Models\Trip;
use App\Models\User;
use Filament\Widgets\StatsOverviewWidget as BaseWidget;
use Filament\Widgets\StatsOverviewWidget\Stat;

class StatsOverview extends BaseWidget
{
    protected static ?int $sort = 1;

    public static function canView(): bool
    {
        return auth()->user()?->isAdmin() ?? false;
    }

    protected function getStats(): array
    {
        $tripsToday      = Trip::whereDate('created_at', today())->count();
        $activeTrips     = Trip::where('status', 'active')->count();
        $liveTrips       = Trip::where('status', 'live')->count();
        $completedMonth  = Trip::where('status', 'completed')
            ->whereMonth('updated_at', now()->month)->count();

        $bookingsToday   = Booking::whereDate('created_at', today())->count();
        $confirmedToday  = Booking::where('status', 'confirmed')
            ->whereDate('confirmed_at', today())->count();

        $totalDrivers    = User::whereIn('role', ['driver', 'both'])->count();
        $totalRiders     = User::whereIn('role', ['rider', 'both'])->count();
        $newUsersToday   = User::whereDate('created_at', today())->count();

        $playDownloads   = (int) PlatformConfig::getValue('play_store_downloads', 0);
        $appDownloads    = (int) PlatformConfig::getValue('app_store_downloads', 0);
        $totalDownloads  = $playDownloads + $appDownloads;

        return [
            Stat::make('Trips Posted Today', $tripsToday)
                ->description("{$liveTrips} live · {$activeTrips} en route")
                ->color('success')
                ->icon('heroicon-o-map'),

            Stat::make('Completed Trips (This Month)', $completedMonth)
                ->description('Revenue-generating rides')
                ->color('primary')
                ->icon('heroicon-o-check-circle'),

            Stat::make('Bookings Today', $bookingsToday)
                ->description("{$confirmedToday} confirmed")
                ->color('warning')
                ->icon('heroicon-o-ticket'),

            Stat::make('Total Drivers', $totalDrivers)
                ->description('Registered drivers')
                ->color('info')
                ->icon('heroicon-o-truck'),

            Stat::make('Total Riders', $totalRiders)
                ->description("{$newUsersToday} new users today")
                ->color('info')
                ->icon('heroicon-o-users'),

            Stat::make('Active Trips Right Now', $activeTrips)
                ->description('Drivers currently en route')
                ->color($activeTrips > 0 ? 'success' : 'gray')
                ->icon('heroicon-o-signal'),

            Stat::make('Total App Downloads', number_format($totalDownloads))
                ->description(number_format($playDownloads) . ' Android · ' . number_format($appDownloads) . ' iOS')
                ->color('success')
                ->icon('heroicon-o-device-phone-mobile'),
        ];
    }
}
