<?php

/**
 * Lightweight bootstrap for unit tests against pure AdOps helpers.
 *
 * Intentionally does NOT boot CodeIgniter — the tests under tests/unit/AdOps
 * only exercise static functions on CampaignCsv, CreativeCsv, ListsService,
 * EventLogService (recursiveDiff), and ImportExportService (isApiSuccess).
 * A full CI4 boot would add seconds per run for zero benefit.
 */

// PSR-4 autoloader: App\... → app/...
spl_autoload_register(static function (string $class): void {
    $prefix   = 'App\\';
    $baseDir  = dirname(__DIR__) . '/app/';

    if (strpos($class, $prefix) !== 0) {
        return;
    }

    $relative = substr($class, strlen($prefix));
    $file     = $baseDir . str_replace('\\', '/', $relative) . '.php';

    if (is_file($file)) {
        require_once $file;
    }
});

// CampaignCsv::encodeRow gates the Hearst rollup columns on this constant,
// which normally comes from app/Config/Constants.php. Without it the guard in
// extraColumnsFor() short-circuits to false for every advertiser, which would
// make any assertion about the rollup columns pass vacuously. Mirror the real
// production value so both branches are reachable under test.
defined('HEARST_ADV_IDS') || define('HEARST_ADV_IDS', [13421, 20934]);

// CampaignCsv::decodeRow calls session('mngtuser') when decoding a
// single-publisher row. Stub the helper so tests don't need a CI4 session.
if (!function_exists('session')) {
    function session(string $key = null)
    {
        $store = $GLOBALS['__test_session__'] ?? [];
        if ($key === null) {
            return $store;
        }
        return $store[$key] ?? null;
    }
}

// log_message is invoked by ApiClient/services on error paths. Tests don't
// touch those paths today, but stub it so future accidental calls don't fail.
if (!function_exists('log_message')) {
    function log_message(string $level, string $message): void
    {
        // swallow
    }
}
