<?php
if (defined('WP_CLI')) {
    $_SERVER['HTTP_HOST'] = 'localhost';
}

if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') {
    $_SERVER['HTTPS'] = 'on';
}

if (isset($_SERVER['HTTP_HOST'])) {
    $httpProtocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http';
    define('WP_HOME', $httpProtocol . '://' . $_SERVER['HTTP_HOST']);
    define('WP_SITEURL', $httpProtocol . '://' . $_SERVER['HTTP_HOST']);
}

function required_env(string $key): string {
    $value = getenv($key);
    if ($value === false || $value === null || $value === '') {
        throw new RuntimeException("Required environment variable {$key} is not set.");
    }

    return $value;
}

define('DB_NAME', required_env('DB_NAME'));
define('DB_USER', required_env('DB_USER'));
define('DB_PASSWORD', required_env('DB_PASS'));
define('DB_HOST', getenv('DB_HOST') ?: '127.0.0.1');
define('DB_CHARSET', 'utf8mb4');
define('DB_COLLATE', '');

define('AUTH_KEY', required_env('WP_AUTH_KEY'));
define('SECURE_AUTH_KEY', required_env('WP_SECURE_AUTH_KEY'));
define('LOGGED_IN_KEY', required_env('WP_LOGGED_IN_KEY'));
define('NONCE_KEY', required_env('WP_NONCE_KEY'));
define('AUTH_SALT', required_env('WP_AUTH_SALT'));
define('SECURE_AUTH_SALT', required_env('WP_SECURE_AUTH_SALT'));
define('LOGGED_IN_SALT', required_env('WP_LOGGED_IN_SALT'));
define('NONCE_SALT', required_env('WP_NONCE_SALT'));

$table_prefix = 'wp_';

define('WP_DEBUG', false);

if (!defined('ABSPATH')) {
    define('ABSPATH', __DIR__ . '/');
}

require_once ABSPATH . 'wp-settings.php';
