<?php

namespace App\Http\Controllers\Admin;

use App\Domain\Subscription\MetricsService;
use App\Http\Controllers\Controller;
use App\Models\SubscriptionPayment;
use App\Models\Tenant;
use App\Enums\PaymentProofStatus;

class AdminDashboardController extends Controller
{
    public function index(MetricsService $metrics)
    {
        return view('admin.dashboard', [
            'mrr' => $metrics->mrr(),
            'arr' => $metrics->arr(),
            'counts' => $metrics->counts(),
            'churn' => $metrics->churnRate(),
            'conversion' => $metrics->trialConversionRate(),
            'revenueByMonth' => $metrics->revenueByMonth(6),
            'mrrByGateway' => $metrics->mrrByGateway(),
            'revenueByMethod' => $metrics->revenueByMethod(),
            'expiringClinics' => $metrics->expiringClinics(7),
            'expiringCount' => $metrics->expiringCount(7),
            'trialFunnel' => $metrics->trialFunnel(),
            'pendingProofs' => SubscriptionPayment::query()->withoutGlobalScope('tenant')
                ->where('status', PaymentProofStatus::Pending->value)->count(),
            'recentClinics' => Tenant::withoutGlobalScope('tenant')
                ->with('subscription')->latest()->take(10)->get(),
        ]);
    }
}
