<?php

use hyax\Env;
use yii\base\InvalidConfigException;

require __DIR__ . '/../vendor/autoload.php';

ini_set('max_input_vars', '4000');

// Debug enabled only on staging and local envs
$ip = $_SERVER['REMOTE_ADDR'];
$debugCookie = isset($_COOKIE['X-Debug']);



if (!Env::get()->isProduction() || $ip == '172.56.208.40' || $ip == '68.108.46.239' || $ip == '24.16.8.119' || $ip == '184.186.118.254' || $ip == '172.56.208.212' || $ip == '222.106.153.190' || $ip == '222.106.88.179' || $ip == '182.208.246.188' || $ip == '24.234.103.187' || $ip == '172.56.208.72' || $ip == '162.218.234.148' || $ip == '24.234.228.22' || $ip == '184.186.118.254' || $ip == '120.29.78.95' || $ip == '172.56.209.202' || $debugCookie) {
    defined('YII_DEBUG') or define('YII_DEBUG', true);
    defined('YII_ENV') or define('YII_ENV', 'dev');
}
/*

defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');
*/

const DOTENV_PATH = __DIR__ . '/..';
const DOTENV_OVERLOAD = false;
define('DOTENV_FILE', sprintf('.env.%s', Env::get()->isProduction() ? 'prod' : 'dev'));

// Caddy forward_auth decision endpoint. Caddy calls this over 127.0.0.1 to
// decide whether to reverse-proxy a hy.page/links.page (or custom-domain)
// request to hyax3 at the edge or fall through to growthforms. Responds with
// the X-Hyax3-Route header and exits before Yii boots. See Caddyfile.
if (($_SERVER['REQUEST_URI'] ?? '') === \hyax\Hyax3Proxy::DECISION_PATH) {
    \hyax\Hyax3Proxy::respondRouteDecision();
}

// hy.page / links.page reverse-proxy interceptor. Returns immediately
// for any request we don't proxy; otherwise streams to hyax3 and exits
// before Yii bootstraps. Kept as a fallback now that Caddy proxies migrated
// HTML at the edge: migrated requests are short-circuited by Caddy and never
// reach here, and non-migrated requests return immediately. See Hyax3Proxy.
\hyax\Hyax3Proxy::handleIfApplicable();

// hyax.com reverse-proxy interceptor (separate setup). Proxies the
// marketing homepage + Nuxt assets to platform.hyax.com and the blog +
// blog asset/api/sitemap prefixes to hyax.hyax.app; exits before Yii
// bootstraps. See src/hyax/BlogProxy.php.
\hyax\BlogProxy::handleIfApplicable();

require __DIR__ . '/../vendor/yiisoft/yii2/Yii.php';
$config = require __DIR__ . '/../config/web.php';
try {
    Yii::setAlias('@dektrium/user/controllers', 'src/dektrium/user/controllers');
    (new yii\web\Application($config))->run();
} catch (InvalidConfigException $e) {
    if (YII_ENV) {
        print_r($e->getTraceAsString());
    }
    die('OOPS:' . $e->getMessage());
}
