<?php
declare(strict_types=1);

// ⟦WARRANT⟧
// webster warrant: V3.3
// WARRANT-SUBJECT: HALDEN.PHP.WRAPPER.BEGIN_CANDIDATE_UPLOAD.PRIVATE_CORE_BRIDGE
// WARRANT-ROLE: INTERNAL_ENDPOINT_WRAPPER
// CONTRACT-VERSION: V1.0 LOCKED
// PURPOSE:
// - Bridge beginCandidateUpload to the current private HALDEN core runtime without replacing the public API library.
// - Preserve watcher upload availability while proving the private-core inline declaration behavior.
// MAY:
// - Resolve private_ww/halden/core_lib.php from known public-root and specs-subdomain layouts.
// - Use apiGetConfig_ and haldenHandleOperation_ from the current private core.
// MUST:
// - Return controlled JSON if the private core or required current function names are unavailable.
// - Invoke only the beginCandidateUpload operation.
// - Emit marker-tagged entry/error evidence without printing secrets or raw request bodies.
// MUST NOT:
// - Load the stale public _lib.php.
// - Call obsolete apiLoadConfig_ or apiHandleOperation_ names.
// - Print secrets, config values, request bodies, upload tickets, or stack traces.
// VIOLATION:
// - Watcher uploads blank-500 or bypass the private-core beginCandidateUpload contract.
// WARRANT-HASH: DEFERRED
// ⟦/WARRANT⟧

header('Content-Type: application/json; charset=utf-8');
ini_set('display_errors', '0');
error_reporting(E_ALL);

function haldenBeginCandidateUploadBridgeSend_(int $statusCode, array $payload): void {
    http_response_code($statusCode);
    header('Content-Type: application/json; charset=utf-8');
    echo json_encode($payload, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
    exit;
}

function haldenBeginCandidateUploadBridgeCorePath_(): ?string {
    $candidates = [
        dirname(__DIR__, 4) . '/private_ww/halden/core_lib.php',
        dirname(__DIR__, 5) . '/private_ww/halden/core_lib.php',
        dirname(__DIR__, 6) . '/private_ww/halden/core_lib.php',
    ];

    $seen = [];
    foreach ($candidates as $candidate) {
        if (!is_string($candidate) || $candidate === '') {
            continue;
        }
        if (isset($seen[$candidate])) {
            continue;
        }
        $seen[$candidate] = true;
        if (is_file($candidate)) {
            return $candidate;
        }
    }

    return null;
}

try {
    error_log('DEBUG_LOG: HALDEN_BEGIN_CANDIDATE_UPLOAD_PRIVATE_CORE_BRIDGE entry');

    $corePath = haldenBeginCandidateUploadBridgeCorePath_();
    if (!is_string($corePath) || $corePath === '') {
        error_log('TRACE_LOG: HALDEN_BEGIN_CANDIDATE_UPLOAD_PRIVATE_CORE_BRIDGE core_missing');
        haldenBeginCandidateUploadBridgeSend_(500, [
            'ok' => false,
            'error' => 'HALDEN_PRIVATE_CORE_NOT_FOUND'
        ]);
    }

    require_once $corePath;

    if (!function_exists('apiGetConfig_')) {
        error_log('TRACE_LOG: HALDEN_BEGIN_CANDIDATE_UPLOAD_PRIVATE_CORE_BRIDGE apiGetConfig_missing');
        haldenBeginCandidateUploadBridgeSend_(500, [
            'ok' => false,
            'error' => 'HALDEN_CONFIG_FUNCTION_NOT_FOUND'
        ]);
    }

    if (!function_exists('haldenHandleOperation_')) {
        error_log('TRACE_LOG: HALDEN_BEGIN_CANDIDATE_UPLOAD_PRIVATE_CORE_BRIDGE haldenHandleOperation_missing');
        haldenBeginCandidateUploadBridgeSend_(500, [
            'ok' => false,
            'error' => 'HALDEN_HANDLER_FUNCTION_NOT_FOUND'
        ]);
    }

    $cfg = apiGetConfig_();
    if (!is_array($cfg)) {
        error_log('TRACE_LOG: HALDEN_BEGIN_CANDIDATE_UPLOAD_PRIVATE_CORE_BRIDGE config_not_array');
        haldenBeginCandidateUploadBridgeSend_(500, [
            'ok' => false,
            'error' => 'SERVER_MISCONFIG'
        ]);
    }

    haldenHandleOperation_('beginCandidateUpload', $cfg);
} catch (Throwable $e) {
    error_log('TRACE_LOG: HALDEN_BEGIN_CANDIDATE_UPLOAD_PRIVATE_CORE_BRIDGE unhandled class=' . get_class($e));
    haldenBeginCandidateUploadBridgeSend_(500, [
        'ok' => false,
        'error' => 'HALDEN_BEGIN_CANDIDATE_UPLOAD_BRIDGE_FAILED'
    ]);
}

// beginCandidateUpload private-core bridge ends
