<?php
declare(strict_types=1);

/**
 * Called by the n8n Case Ingest workflow after a document is OCR'd + indexed.
 * Auth: HMAC-SHA256 (same as award-survey-credits, shared INTERSITE_HMAC_SECRET).
 *
 * Body: { doc_id, status, page_count, doc_type, detected_date, parties, error_msg }
 */

require_once __DIR__ . '/../../includes/bootstrap.php';

dbnToolsRequireMethod('POST');

$secret = dbnToolsIntersiteSecret();
if ($secret === '') {
    dbnToolsError('Intersite secret not configured.', 500, 'misconfigured');
}

$ts = (int)($_SERVER['HTTP_X_INTERSITE_TIMESTAMP'] ?? 0);
$sig = (string)($_SERVER['HTTP_X_INTERSITE_SIGNATURE'] ?? '');
if ($ts === 0 || $sig === '' || abs(time() - $ts) > 300) {
    dbnToolsError('Bad intersite auth.', 401, 'bad_auth');
}

$raw = file_get_contents('php://input');
if (!is_string($raw) || $raw === '') {
    dbnToolsError('Empty body.', 400, 'empty');
}
if (!hash_equals(hash_hmac('sha256', $ts . '.' . $raw, $secret), $sig)) {
    dbnToolsError('Bad signature.', 401, 'bad_sig');
}

$data = json_decode($raw, true);
if (!is_array($data)) {
    dbnToolsError('Invalid JSON.', 400, 'invalid_json');
}

$docId = (int)($data['doc_id'] ?? 0);
$status = (string)($data['status'] ?? 'failed');
$pageCount = isset($data['page_count']) ? (int)$data['page_count'] : null;
$docType = isset($data['doc_type']) ? (string)$data['doc_type'] : null;
$detectedDate = isset($data['detected_date']) ? (string)$data['detected_date'] : null;
$parties = $data['parties'] ?? null;
$errorMsg = isset($data['error_msg']) ? (string)$data['error_msg'] : null;

if ($docId <= 0) {
    dbnToolsError('doc_id required.', 400, 'bad_input');
}

$db = dbnmDb();
$db->prepare(
    'UPDATE case_documents
     SET ocr_status = ?,
         page_count = COALESCE(?, page_count),
         doc_type = COALESCE(?, doc_type),
         detected_date = COALESCE(?, detected_date),
         parties = COALESCE(?, parties),
         ocr_error = COALESCE(?, ocr_error),
         indexed_at = CASE WHEN ? = "ready" THEN NOW() ELSE indexed_at END
     WHERE id = ?'
)->execute([
    $status,
    $pageCount,
    $docType,
    $detectedDate,
    $parties !== null ? json_encode($parties, JSON_UNESCAPED_UNICODE) : null,
    $errorMsg,
    $status,
    $docId,
]);

dbnToolsRespond(['ok' => true]);
