<?php

namespace App\Filament\Widgets;

use App\Models\Application;
use App\Models\ImportedJob;
use App\Models\JobSource;
use App\Models\Payment;
use App\Models\ResumeReport;
use App\Models\User;
use Carbon\CarbonImmutable;
use Filament\Support\Icons\Heroicon;
use Filament\Widgets\StatsOverviewWidget;
use Filament\Widgets\StatsOverviewWidget\Stat;

class PlatformOverviewStats extends StatsOverviewWidget
{
    protected static bool $isLazy = false;

    protected static ?int $sort = 1;

    protected ?string $heading = 'Operational snapshot';

    protected ?string $description = 'Fast health metrics with 7-day movement.';

    /**
     * @return array<Stat>
     */
    protected function getStats(): array
    {
        $dates = collect(range(6, 0))->map(fn (int $days): CarbonImmutable => CarbonImmutable::now()->subDays($days));
        $revenue = Payment::query()
            ->whereNotNull('verified_at')
            ->sum('amount_paise') / 100;
        $currency = (string) setting('payment.default_currency', 'INR');

        return [
            Stat::make('Verified revenue', $currency.' '.number_format($revenue, 2))
                ->description('Captured payment amount')
                ->descriptionColor('success')
                ->descriptionIcon(Heroicon::ArrowTrendingUp)
                ->icon(Heroicon::CurrencyDollar)
                ->chart($dates->map(fn (CarbonImmutable $date): float => Payment::query()
                    ->whereDate('verified_at', $date)
                    ->sum('amount_paise') / 100)->all())
                ->chartColor('success')
                ->color('success'),

            Stat::make('Resume analyses', number_format(ResumeReport::query()->where('status', ResumeReport::STATUS_COMPLETED)->count()))
                ->description('Completed ATS reports')
                ->descriptionColor('primary')
                ->icon(Heroicon::DocumentCheck)
                ->chart($dates->map(fn (CarbonImmutable $date): int => ResumeReport::query()
                    ->where('status', ResumeReport::STATUS_COMPLETED)
                    ->whereDate('analyzed_at', $date)
                    ->count())->all())
                ->chartColor('primary')
                ->color('primary'),

            Stat::make('Active jobs', number_format(ImportedJob::query()->active()->count()))
                ->description('Eligible imported jobs')
                ->descriptionColor('success')
                ->icon(Heroicon::Briefcase)
                ->chart($dates->map(fn (CarbonImmutable $date): int => ImportedJob::query()
                    ->active()
                    ->whereDate('created_at', '<=', $date)
                    ->count())->all())
                ->chartColor('success')
                ->color('success'),

            Stat::make('Applications', number_format(Application::query()->count()))
                ->description('Tracked apply actions')
                ->descriptionColor('warning')
                ->icon(Heroicon::CursorArrowRays)
                ->chart($dates->map(fn (CarbonImmutable $date): int => Application::query()
                    ->whereDate('applied_at', $date)
                    ->count())->all())
                ->chartColor('warning')
                ->color('warning'),

            Stat::make('Active sources', number_format(JobSource::query()->where('is_active', true)->count()))
                ->description('Enabled import sources')
                ->descriptionColor('gray')
                ->icon(Heroicon::Link)
                ->color('gray'),
        ];
    }
}
