<?php

namespace App\Filament\Widgets;

use App\Models\Subscription;
use App\Models\Transaction;
use App\Models\UsageLog;
use App\Models\User;
use Filament\Widgets\StatsOverviewWidget;
use Filament\Widgets\StatsOverviewWidget\Stat;

class PlatformOverview extends StatsOverviewWidget
{
    protected function getStats(): array
    {
        $customerCount = User::query()->where('role', 'customer')->count();
        $activeSubscriptions = Subscription::query()->where('status', 'active')->count();
        $monthlyRevenue = (float) Subscription::query()
            ->where('status', 'active')
            ->with('plan')
            ->get()
            ->sum(fn (Subscription $subscription): float => (float) ($subscription->plan?->price ?? 0));
        $aiUsage = (int) UsageLog::query()
            ->where('feature', 'ai_parse')
            ->sum('count');
        $dailyEntries = Transaction::query()
            ->whereDate('created_at', today())
            ->count();

        return [
            Stat::make('Customers', (string) $customerCount)
                ->description('Active product accounts'),
            Stat::make('Active Subscriptions', (string) $activeSubscriptions)
                ->description('Current paying/free active tenants'),
            Stat::make('MRR Snapshot', 'Rs. ' . number_format($monthlyRevenue, 2))
                ->description('Monthly recurring revenue estimate'),
            Stat::make('AI Parses', (string) $aiUsage)
                ->description('Logged AI parsing usage'),
            Stat::make('Entries Today', (string) $dailyEntries)
                ->description('Transactions created today'),
        ];
    }
}
