<?php

namespace App\Filament\Widgets;

use App\Models\Application;
use App\Models\ImportedJob;
use App\Models\Payment;
use App\Models\QuestionnaireResponse;
use App\Models\ResumeReport;
use App\Models\User;
use Filament\Widgets\Widget;

class AdminDashboardOverview extends Widget
{
    protected static bool $isLazy = false;

    protected static ?int $sort = 0;

    protected string $view = 'filament.widgets.admin-dashboard-overview';

    protected int|string|array $columnSpan = 'full';

    /**
     * @return array<string, mixed>
     */
    protected function getViewData(): array
    {
        $totalUsers = User::query()->count();
        $questionnaires = QuestionnaireResponse::query()->whereNotNull('completed_at')->count();
        $paidUsers = User::query()->whereNotNull('access_unlocked_at')->count();
        $resumeAnalyses = ResumeReport::query()->where('status', ResumeReport::STATUS_COMPLETED)->count();
        $readyApplicants = User::query()
            ->whereNotNull('questionnaire_completed_at')
            ->whereNotNull('access_unlocked_at')
            ->whereNotNull('resume_analyzed_at')
            ->whereNotNull('profile_completed_at')
            ->count();
        $revenue = Payment::query()->whereNotNull('verified_at')->sum('amount_paise') / 100;
        $applications = Application::query()->count();
        $activeJobs = ImportedJob::query()->active()->count();

        return [
            'currency' => (string) setting('payment.default_currency', 'INR'),
            'metrics' => [
                [
                    'label' => 'Verified revenue',
                    'value' => number_format($revenue, 2),
                    'prefix' => (string) setting('payment.default_currency', 'INR').' ',
                    'note' => 'Captured payment value',
                    'tone' => 'emerald',
                    'icon' => 'currency',
                ],
                [
                    'label' => 'Ready applicants',
                    'value' => number_format($readyApplicants),
                    'note' => 'Completed full access flow',
                    'tone' => 'teal',
                    'icon' => 'check',
                ],
                [
                    'label' => 'Active jobs',
                    'value' => number_format($activeJobs),
                    'note' => 'Available imported jobs',
                    'tone' => 'sky',
                    'icon' => 'briefcase',
                ],
                [
                    'label' => 'Tracked applications',
                    'value' => number_format($applications),
                    'note' => 'Apply redirects recorded',
                    'tone' => 'amber',
                    'icon' => 'cursor',
                ],
            ],
            'funnel' => [
                ['label' => 'Users', 'value' => $totalUsers, 'percent' => 100],
                ['label' => 'Questionnaire', 'value' => $questionnaires, 'percent' => $this->percent($questionnaires, $totalUsers)],
                ['label' => 'Paid', 'value' => $paidUsers, 'percent' => $this->percent($paidUsers, $totalUsers)],
                ['label' => 'Resume analyzed', 'value' => $resumeAnalyses, 'percent' => $this->percent($resumeAnalyses, $totalUsers)],
                ['label' => 'Ready', 'value' => $readyApplicants, 'percent' => $this->percent($readyApplicants, $totalUsers)],
            ],
        ];
    }

    private function percent(int $value, int $total): int
    {
        if ($total <= 0) {
            return 0;
        }

        return (int) round(($value / $total) * 100);
    }
}
