<?php

namespace App\Controllers\Client;

use App\Controllers\BaseController;
use App\Models\InvoiceModel;
use App\Models\ServiceModel;

class DashboardController extends BaseController
{
    public function index()
    {
        $clientId = session()->get('client_id');

        $invoiceModel = new InvoiceModel();
        $serviceModel = new ServiceModel();

        $invoices = $invoiceModel->where('client_id', $clientId)->orderBy('created_at', 'DESC')->findAll(5);
        $services = $serviceModel->where('client_id', $clientId)->orderBy('renewal_date', 'ASC')->findAll(5);
        $unpaidTotal = $invoiceModel->where('client_id', $clientId)->whereIn('status', ['sent', 'overdue'])
            ->selectSum('amount')->first()['amount'] ?? 0;

        return view('client/dashboard', compact('invoices', 'services', 'unpaidTotal'));
    }
}
