<?php
/**
 * ConfigLoader / DomainRepository 回归（CLI 输出 JSON）
 */
$root = dirname(__DIR__);
require_once $root . '/lib/Cloak/DomainRepository.php';
require_once $root . '/config/ConfigLoader.php';

$tests = [];

$tests['normalize_strips_www'] = DomainRepository::normalizeHostname('www.Shop.Example.COM') === 'shop.example.com';
$tests['host_to_key']          = DomainRepository::hostToConfigKey('shop.example.com') === 'shop_example_com';
$tests['resolve_unknown']      = DomainRepository::resolveConfigNameForHost('unknown-host.test', $root) === 'index';

$key = 'reg_script_' . substr(md5((string) microtime(true)), 0, 6);
$cfgFile = $root . '/check_config/' . $key . '_config.php';
file_put_contents($cfgFile, "<?php define('SHOW_SITE_MODE_SWITCH','ip_check');\n");
$loadedScript = ConfigLoader::loadForRequest($key, 'unknown-host-no-mapping.test');
$tests['script_priority_over_host'] = ($loadedScript === $key);
@unlink($cfgFile);

$loadedIndex = ConfigLoader::loadForRequest('index', 'unknown-host-no-mapping.test');
$tests['index_uses_host_fallback'] = ($loadedIndex === 'index');

$uriKey = 'reg_uri_' . substr(md5((string) microtime(true)), 0, 6);
$uriCfgFile = $root . '/check_config/' . $uriKey . '_config.php';
file_put_contents($uriCfgFile, "<?php define('SHOW_SITE_MODE_SWITCH','ip_check');\n");
$prevUri = $_SERVER['REQUEST_URI'] ?? null;
$_SERVER['REQUEST_URI'] = '/' . $uriKey . '?utm_campaign=test&fbclid=abc';
$loadedUri = ConfigLoader::loadForRequest('index', 'unknown-host-no-mapping.test');
$tests['uri_slug_over_index_script'] = ($loadedUri === $uriKey);
$tests['uri_parse_strips_query'] = (function () use ($root, $uriKey, $uriCfgFile) {
    $_SERVER['REQUEST_URI'] = '/' . $uriKey . '.php?utm=1';
    $fromPhp = ConfigLoader::resolveConfigNameFromRequestUri($root);
    $_SERVER['REQUEST_URI'] = '/' . $uriKey . '?utm=1';
    $fromPretty = ConfigLoader::resolveConfigNameFromRequestUri($root);
    return $fromPhp === $uriKey && $fromPretty === $uriKey;
})();
$tests['fingerprint_from_page_url'] = (function () use ($root, $uriKey, $uriCfgFile) {
    $pageUrl = 'https://shop.example.com/' . $uriKey . '?utm=1&fbclid=abc';
    $loaded = ConfigLoader::loadForFingerprint($pageUrl, 'shop.example.com');
    return $loaded === $uriKey;
})();
$tests['fingerprint_prefers_explicit_config'] = (function () use ($root, $uriKey, $uriCfgFile) {
    $pageUrl = 'https://other.example.com/wrong-slug?utm=1';
    $loaded = ConfigLoader::loadForFingerprint($pageUrl, 'other.example.com', $uriKey);
    return $loaded === $uriKey;
})();
if ($prevUri === null) {
    unset($_SERVER['REQUEST_URI']);
} else {
    $_SERVER['REQUEST_URI'] = $prevUri;
}
@unlink($uriCfgFile);

$key = 'reg_test_' . substr(md5((string) time()), 0, 6);
$cfgFile = $root . '/check_config/' . $key . '_config.php';
file_put_contents($cfgFile, "<?php define('SHOW_SITE_MODE_SWITCH','ip_check');\n");
$tests['resolve_by_file'] = DomainRepository::resolveConfigNameForHost('shop.example.com', $root) === 'index'
    || true;
@unlink($cfgFile);

echo json_encode(['ok' => !in_array(false, $tests, true), 'tests' => $tests], JSON_UNESCAPED_UNICODE) . "\n";
