<?php

namespace App\Controllers\Admin;

use App\Models\Control\AdminActivityLogModel;
use App\Models\Control\InvoiceModel;
use App\Models\Control\SubscriptionModel;
use App\Models\Control\TenantRecordModel;

class DashboardController extends BaseAdminController
{
    public function index()
    {
        $tenants       = new TenantRecordModel();
        $subscriptions = new SubscriptionModel();
        $invoices      = new InvoiceModel();

        return view('admin/dashboard/index', [
            'pageTitle'         => 'Dashboard',
            'activeNav'         => 'dashboard',
            'totalHotels'       => $tenants->countAllResults(),
            'activeHotels'      => $tenants->countByStatus('active'),
            'suspendedHotels'   => $tenants->countByStatus('suspended'),
            'activeSubs'        => $subscriptions->countByStatus('active'),
            'trialSubs'         => $subscriptions->countByStatus('trial'),
            'pastDueSubs'       => $subscriptions->countByStatus('past_due'),
            'mrr'               => $subscriptions->estimatedMrr(),
            'totalCollected'    => $invoices->totalCollected(),
            'totalOutstanding'  => $invoices->totalOutstanding(),
            'overdueCount'      => $invoices->countByStatus('overdue'),
            'recentHotels'      => $tenants->orderBy('created_at', 'desc')->limit(5)->find(),
            'recentActivity'    => (new AdminActivityLogModel())->recent(8),
        ]);
    }
}
