<?php
declare(strict_types=1);

namespace App\Controllers;

use App\Core\Controller;
use App\Core\Database;

final class AdminAuditTenantController extends Controller
{
    public function index(): void
    {
        require_org_admin(); // org_admin أو superadmin
        $db=Database::getInstance();
        $orgId=(int)current_org_id();

        $isSA = function_exists('is_superadmin') && is_superadmin();
        $all = (int)($_GET['all'] ?? 0);

        $sql = "SELECT * FROM audit_logs ";
        $params = [];

        if (!($isSA && $all===1)) {
            $sql .= " WHERE org_id=:o ";
            $params[':o']=$orgId;
        }

        $sql .= " ORDER BY id DESC LIMIT 400";
        $st=$db->prepare($sql);
        $st->execute($params);
        $rows=$st->fetchAll()?:[];

        $this->view('admin/audit/index', ['rows'=>$rows,'showAll'=>($isSA && $all===1)]);
    }
}
