<?php
/**
 * Oracle module front-controller / router.
 *
 * All requests matching /oracle/* are rewritten here by Apache mod_rewrite.
 * The router parses the path, sets the appropriate $_GET variables,
 * then includes the relevant module file.
 *
 * URL scheme:
 *   GET  /oracle                          → dashboard
 *   GET  /oracle/admin/db                 → database list (all)
 *   GET  /oracle/admin/db/{group}         → list filtered by group name
 *   GET  /oracle/admin/db/{group}/{sub}   → list filtered by group + subgroup
 *   GET  /oracle/admin/db/new             → create form
 *   POST /oracle/admin/db/new             → save new record
 *   GET  /oracle/admin/db/{id}/edit       → edit form  (numeric id)
 *   POST /oracle/admin/db/{id}/edit       → save edit
 *   POST /oracle/admin/db                 → delete (action=delete in POST body)
 *   GET  /oracle/admin/groups             → group management
 *   POST /oracle/admin/groups             → group actions
 *   GET  /oracle/admin/access             → access management
 *   POST /oracle/admin/access             → access actions
 *   GET  /oracle/admin/roles              → role list / create
 *   POST /oracle/admin/roles              → role actions
 *   GET  /oracle/admin/roles/{id}/edit    → edit role (perms + grants)
 *   POST /oracle/admin/roles/{id}/edit    → save role
 *   GET  /oracle/admin/export             → export (download)
 *   GET  /oracle/admin/import             → import page
 *   POST /oracle/admin/import             → process import
 *   POST /oracle/connect                  → select a database (store in session)
 *   GET  /oracle/disconnect               → deselect current database
 *   GET  /oracle/report                   → report overview (requires selected DB)
 *   GET  /oracle/report/config            → configuration reports
 *   GET  /oracle/report/realtime          → real-time reports
 *   GET  /oracle/report/history           → history reports
 *   POST /oracle/api/test-connection      → AJAX connection test
 */

// The module files all resolve bootstrap relative to their own __DIR__,
// so no bootstrap call needed here — each included file handles it.

$_OCI_MODULE = dirname(__DIR__) . '/modules/oracle';

// ── Parse path ────────────────────────────────────────────────────────────────
$_uri     = parse_url($_SERVER['REQUEST_URI'] ?? '/', PHP_URL_PATH);
$_subPath = ltrim(substr($_uri, strlen('/oracle')), '/');
$_segs    = $_subPath !== '' ? explode('/', $_subPath) : [];

$_s0 = $_segs[0] ?? '';
$_s1 = $_segs[1] ?? '';
$_s2 = rawurldecode($_segs[2] ?? '');
$_s3 = rawurldecode($_segs[3] ?? '');
unset($_uri, $_subPath, $_segs);

// ── Dispatch ──────────────────────────────────────────────────────────────────
switch (true) {

    // /oracle  (dashboard)
    case $_s0 === '':
        require $_OCI_MODULE . '/index.php';
        break;

    // /oracle/admin/db[/...]
    case $_s0 === 'admin' && $_s1 === 'db':
        if ($_s2 === 'new') {
            // /oracle/admin/db/new
            if (!isset($_GET['action'])) $_GET['action'] = 'create';
        } elseif ($_s2 !== '' && $_s3 === 'edit' && ctype_digit($_s2)) {
            // /oracle/admin/db/{id}/edit
            if (!isset($_GET['action'])) $_GET['action'] = 'edit';
            if (!isset($_GET['id']))     $_GET['id']     = (int)$_s2;
        } elseif ($_s2 !== '') {
            // /oracle/admin/db/{group}[/{subgroup}]
            $_GET['group_name'] = $_s2;
            if ($_s3 !== '') $_GET['subgroup'] = $_s3;
        }
        require $_OCI_MODULE . '/databases.php';
        break;

    // /oracle/admin/groups
    case $_s0 === 'admin' && $_s1 === 'groups':
        require $_OCI_MODULE . '/groups.php';
        break;

    // /oracle/admin/access
    case $_s0 === 'admin' && $_s1 === 'access':
        require $_OCI_MODULE . '/access.php';
        break;

    // /oracle/admin/roles[/{id}/edit]
    case $_s0 === 'admin' && $_s1 === 'roles':
        if ($_s2 !== '' && ctype_digit($_s2) && $_s3 === 'edit') {
            if (!isset($_GET['id'])) $_GET['id'] = (int)$_s2;
        }
        require $_OCI_MODULE . '/roles.php';
        break;

    // /oracle/admin/export
    case $_s0 === 'admin' && $_s1 === 'export':
        require $_OCI_MODULE . '/export.php';
        break;

    // /oracle/admin/import
    case $_s0 === 'admin' && $_s1 === 'import':
        require $_OCI_MODULE . '/import.php';
        break;

    // /oracle/connect  (POST: select a DB)
    case $_s0 === 'connect':
        require $_OCI_MODULE . '/connect.php';
        break;

    // /oracle/disconnect  (GET: deselect current DB)
    case $_s0 === 'disconnect':
        require $_OCI_MODULE . '/connect.php';
        break;

    // /oracle/report[/config[/tablespaces]|/realtime|/history]
    case $_s0 === 'report':
        if ($_s1 !== '') $_GET['report_section'] = $_s1;
        if ($_s2 !== '') $_GET['report_sub']     = $_s2;
        require $_OCI_MODULE . '/report.php';
        break;

    // /oracle/api/test-connection
    case $_s0 === 'api' && $_s1 === 'test-connection':
        require $_OCI_MODULE . '/test_connection.php';
        break;

    // Unknown oracle route
    default:
        http_response_code(404);
        // Bootstrap is needed for WEBROOT_PATH constant used by 404.php
        require dirname(__DIR__) . '/bootstrap.php';
        include WEBROOT_PATH . '/includes/404.php';
        break;
}
