<?php
declare(strict_types=1);

namespace App\Controllers\Web;

use App\Middleware\AuthSession;
use App\Middleware\TenantScope;
use App\Models\Sds;
use App\Models\Training;
use Core\Controller;
use Core\Session;

/**
 * HSE Library landing page — a hub that shows KPI tiles for both the SDS
 * catalogue and the training records, and links out to each section.
 */
final class HseController extends Controller
{
    public function middleware(): array
    {
        return [AuthSession::class, TenantScope::class];
    }

    private function tenantId(): int { return (int) ($_SESSION['tenant_id'] ?? 0); }

    public function index(): void
    {
        $tenantId = $this->tenantId();
        $trainingCounts = Training::countsForTenant($tenantId);
        $sdsCounts      = Sds::countsForTenant($tenantId);
        $expiring       = Training::recordsForTenant($tenantId, ['status' => 'expiring_soon']);
        $expired        = Training::recordsForTenant($tenantId, ['status' => 'expired']);
        $reviewSoon     = Sds::listForTenant($tenantId, ['due_soon' => 1]);
        $this->render('hse/index', [
            'trainingCounts' => $trainingCounts,
            'sdsCounts'      => $sdsCounts,
            'expiring'       => $expiring,
            'expired'        => $expired,
            'reviewSoon'     => $reviewSoon,
            'flash'          => Session::flash('hse_msg'),
        ]);
    }
}
