<?php
declare(strict_types=1);

/**
 * Lightweight bootstrap for Dev Portal (no controller scan).
 * Loads config + DB + WP/ACL compat needed for auth and backups.
 */
if (!defined('ACC_BASE_PATH')) {
    define('ACC_BASE_PATH', dirname(__DIR__));
}

require_once ACC_BASE_PATH . '/standalone/config.php';

if (!defined('ABSPATH')) {
    define('ABSPATH', ACC_BASE_PATH . DIRECTORY_SEPARATOR);
}
if (!defined('ACC_PATH')) {
    define('ACC_PATH', ACC_BASE_PATH . DIRECTORY_SEPARATOR);
}
if (!defined('ACC_URL')) {
    define('ACC_URL', '/acc-engine/');
}
if (!defined('ARRAY_A')) {
    define('ARRAY_A', 'ARRAY_A');
}
if (!defined('ARRAY_N')) {
    define('ARRAY_N', 'ARRAY_N');
}

date_default_timezone_set('Asia/Tehran');

require_once ACC_BASE_PATH . '/standalone/wp_compat.php';

try {
    $pdo = new PDO(
        ACC_DB_DSN,
        ACC_DB_USER,
        ACC_DB_PASS,
        [
            PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
            PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
            PDO::ATTR_EMULATE_PREPARES   => false,
            PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES ' . ACC_DB_CHARSET,
        ]
    );
} catch (Throwable $e) {
    http_response_code(500);
    header('Content-Type: text/plain; charset=utf-8');
    echo 'Database connection failed';
    if (defined('ACC_DEBUG') && ACC_DEBUG) {
        echo ': ' . $e->getMessage();
    }
    exit;
}

require_once ACC_BASE_PATH . '/standalone/wpdb_adapter.php';
$GLOBALS['wpdb'] = new ACC_WPDB_Adapter($pdo, ACC_DB_PREFIX);

require_once ACC_BASE_PATH . '/includes/helpers/acl-scope-registry.php';
require_once ACC_BASE_PATH . '/includes/helpers/acl-scope-enforce.php';
require_once ACC_BASE_PATH . '/includes/helpers/acl-functions.php';
require_once ACC_BASE_PATH . '/standalone/acl_compat.php';
require_once ACC_BASE_PATH . '/includes/helpers/dev-portal.php';
