<?php

use App\Models\OCRRecord;
use App\Services\OCRService;
use Illuminate\Support\Facades\Log;

require __DIR__.'/vendor/autoload.php';

$app = require_once __DIR__.'/bootstrap/app.php';
$kernel = $app->make(Illuminate\Contracts\Console\Kernel::class);
$kernel->bootstrap();

$recordId = 6; // The ID mentioned by the user
$record = OCRRecord::find($recordId);

if (!$record) {
    echo "Record $recordId not found.\n";
    exit(1);
}

echo "Found record: {$record->id}, Status: {$record->status}\n";

$service = new OCRService();

try {
    echo "Reprocessing...\n";
    $service->reprocess($record);
    
    $record->refresh();
    echo "New Status: {$record->status}\n";
    echo "Error Message: {$record->error_message}\n";
    
    $questions = $record->questions;
    echo "Questions found: " . $questions->count() . "\n";
    
    foreach ($questions as $q) {
        echo "Q{$q->question_number}: {$q->question_content} (Confidence: {$q->confidence})\n";
    }

} catch (\Exception $e) {
    echo "Exception: " . $e->getMessage() . "\n";
    echo $e->getTraceAsString();
}
