<?php
declare(strict_types=1);

// ── Load shared platform bootstrap first ────────────────────────────────────
define('SAVIT_ROOT', dirname(__DIR__));
require_once SAVIT_ROOT . '/Common/bootstrap.php';

// ── Analytics-specific constants ─────────────────────────────────────────────
define('ANALYTICS_ROOT',  __DIR__);
define('ANALYTICS_VIEWS', ANALYTICS_ROOT . '/views');

// ROOT_PATH must point to Audit/ because Crawl\Lib\Database uses it for config lookup
if (!defined('ROOT_PATH')) {
    define('ROOT_PATH', SAVIT_ROOT . '/Audit');
}

// ── Analytics autoloader ──────────────────────────────────────────────────────
spl_autoload_register(function (string $class): void {
    $prefix = 'Analytics\\';
    if (strpos($class, $prefix) !== 0) return;
    $rel  = str_replace('\\', '/', substr($class, strlen($prefix)));
    $file = ANALYTICS_ROOT . '/' . $rel . '.php';
    if (file_exists($file)) require_once $file;
});

// ── Re-use Audit Engine classes (GA4Service, GSCService, GoogleOAuthService) ─
spl_autoload_register(function (string $class): void {
    $prefix = 'Crawl\\';
    if (strpos($class, $prefix) !== 0) return;
    $file = SAVIT_ROOT . '/Audit/' . str_replace('\\', '/', substr($class, strlen($prefix))) . '.php';
    if (file_exists($file)) require_once $file;
});

// ── Analytics vendor (PhpSpreadsheet) ────────────────────────────────────────
if (file_exists(ANALYTICS_ROOT . '/vendor/autoload.php')) {
    require_once ANALYTICS_ROOT . '/vendor/autoload.php';
}
