<?php

namespace App\Filament\Widgets;

use App\Models\Raffle;
use App\Models\ResellerProfile;
use App\Models\Transaction;
use App\Models\User;
use Filament\Widgets\StatsOverviewWidget as BaseWidget;
use Filament\Widgets\StatsOverviewWidget\Stat;

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

    protected function getStats(): array
    {
        $activeRaffles = Raffle::where('status', 'active')->count();

        $activeResellers = ResellerProfile::count();

        $totalRevenue = Transaction::where('type', 'purchase')
            ->where('status', 'approved')
            ->sum('amount');

        // Contar usuarios con rol diferente a administrador (que es el ID 1)
        $totalUsers = User::where('role_id', '!=', 1)->count();

        return [
            Stat::make('Sorteos Activos', $activeRaffles)
                ->description('Sorteos en curso listos para venta')
                ->icon('heroicon-o-gift')
                ->color('primary'),

            Stat::make('Revendedores Activos', $activeResellers)
                ->description('Red nacional de distribución')
                ->icon('heroicon-o-identification')
                ->color('success'),

            Stat::make('Recaudación Total', '₡'.number_format($totalRevenue, 2, '.', ','))
                ->description('Ingresos por venta de boletos aprobadas')
                ->icon('heroicon-o-currency-dollar')
                ->color('warning'),

            Stat::make('Usuarios Registrados', number_format($totalUsers))
                ->description('Clientes y participantes registrados')
                ->icon('heroicon-o-users')
                ->color('info'),
        ];
    }
}
