<?php
require __DIR__ . '/../config/bootstrap.php';

use App\Core\Database;

$db = Database::getInstance();

$conferenceId = (int)($argv[1] ?? 1);

$stmt = $db->prepare("SELECT id, org_id, short_name FROM conferences WHERE id=? LIMIT 1");
$stmt->execute([$conferenceId]);
$c = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$c) {
    exit("Conference not found\n");
}

$sql = "
SELECT
    r.code,
    mi.url,
    rp.is_allowed,
    rp.can_view,
    rp.can_create,
    rp.can_update,
    rp.can_delete
FROM rbac_role_menu_permissions rp
JOIN roles r ON r.id=rp.role_id
JOIN rbac_menu_items mi ON mi.id=rp.menu_item_id
WHERE rp.conference_id=?
  AND rp.org_id=?
  AND mi.url IN (
    '/easychair/admin/branding',
    '/easychair/admin/conference-home',
    '/easychair/admin/committee-images',
    '/easychair/admin/reviewer-assignments',
    '/easychair/admin/submission-stage-rules',
    '/easychair/admin/test-email',
    '/easychair/admin/org-smtp',
    '/easychair/admin/conference-role-permissions',
    '/easychair/admin/conference-business-permissions',
    '/easychair/admin/custom-rights',
    '/easychair/admin/rbac-scope',
    '/easychair/admin/rbac-matrix'
  )
ORDER BY mi.url, r.code
";

$stmt = $db->prepare($sql);
$stmt->execute([(int)$c['id'], (int)$c['org_id']]);

echo "Conference {$c['id']} {$c['short_name']} | Org {$c['org_id']}\n";
echo str_repeat("=", 90) . "\n";

foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $r) {
    printf(
        "%-18s %-48s allow=%d view=%d create=%d update=%d delete=%d\n",
        $r['code'],
        $r['url'],
        $r['is_allowed'],
        $r['can_view'],
        $r['can_create'],
        $r['can_update'],
        $r['can_delete']
    );
}
