<?php
declare(strict_types=1);
namespace App\Controllers;

use App\Core\Controller;
use App\Repositories\ReportRepository;
use App\Repositories\SettingRepository;

final class ReportController extends Controller {
    public function index(): void {
        require_role(['admin','reception','reports']);
        $from = trim((string)($_GET['from'] ?? date('Y-m-01')));
        $to = trim((string)($_GET['to'] ?? date('Y-m-d')));
        $repo = new ReportRepository();
        $settings = (new SettingRepository())->allAssoc();

        $metrics = $repo->dashboardMetrics();
        $topItems = $repo->topItems($from, $to);
        $futureBookings = $repo->futureBookings();
        $occupiedItems = $repo->occupiedItems();
        $invoices = $repo->invoicesList($from, $to);

        $queueSummary = $repo->queueSummary($from, $to);
        $queueDaily = $repo->queueDailyBreakdown($from, $to);
        $queuePrice = (float)($settings['queue_ticket_price'] ?? '0');
        $queueCurrency = (string)($settings['queue_currency'] ?? 'ر.ع');
        $queueCollected = ((int)($queueSummary['done_count'] ?? 0)) * $queuePrice;

        $this->view('reports/index', compact(
            'from', 'to', 'metrics', 'topItems', 'futureBookings', 'occupiedItems',
            'invoices', 'queueSummary', 'queueDaily', 'queuePrice', 'queueCurrency', 'queueCollected'
        ));
    }
}
