<?php

declare(strict_types=1);

require_once dirname(__DIR__) . '/app/ai/combined_tutor_review_diagnostics.php';

$failures = 0;

function diagAssert(bool $ok, string $label): void
{
    global $failures;
    if ($ok) {
        echo "OK: {$label}\n";

        return;
    }
    echo "FAIL: {$label}\n";
    $failures++;
}

$timeout = combinedTutorReviewFailureFromOpenAiFetch(
    ['ok' => false, 'error' => 'OpenAI request timed out.', 'timed_out' => true, 'http_status' => 0, 'raw' => ''],
    'q1'
);
diagAssert($timeout['error_code'] === 'timeout', 'timeout classified');
diagAssert(str_contains($timeout['tutor_message'], 'timed out'), 'timeout tutor message');

$invalidJson = combinedTutorReviewFailureFromOpenAiFetch(
    ['ok' => false, 'error' => 'AI did not return valid JSON.', 'timed_out' => false, 'http_status' => 200, 'raw' => 'not json', 'response_len' => 8],
    'q2'
);
diagAssert($invalidJson['error_code'] === 'incomplete_response', 'invalid JSON classified');

$missingSummary = combinedTutorReviewFailureFromNormalize('Reviewer JSON missing summary.', 'q3');
diagAssert($missingSummary['error_code'] === 'incomplete_response', 'missing summary classified');

$inconsistent = combinedTutorReviewFailureFromNormalize('Suggested correct answer was incomplete.', 'q4');
diagAssert($inconsistent['error_code'] === 'suggestions_inconsistent', 'inconsistent suggestions classified');

if ($failures > 0) {
    echo "\n{$failures} test(s) failed.\n";
    exit(1);
}

echo "\nAll combined tutor review diagnostics tests passed.\n";
