<?php

require __DIR__ . '/../../config/app.php';

use App\Core\Database;

$db = Database::getInstance();

echo "\n==============================\n";
echo " Workflow Health Check\n";
echo "==============================\n\n";

$sql = "
SELECT
id,
submission_no,
status,
stage,
workflow_stage,
production_status,
decision
FROM submissions
ORDER BY id
";

$stmt = $db->query($sql);

$errors = 0;

while ($r = $stmt->fetch(PDO::FETCH_ASSOC)) {

    $expected = '';

    switch ($r['stage']) {

        case 'submission':
            $expected = 'submitted';
            break;

        case 'review':
            $expected = 'under_review';
            break;

        case 'revision':
            $expected = 'revision_required';
            break;

        case 'decision':
            $expected = 'decision';
            break;

        case 'production':
            $expected = 'production';
            break;

        case 'issue':
            $expected = 'issue';
            break;

        case 'published':
            $expected = 'published';
            break;
    }

    if ($expected !== '' && $expected !== $r['workflow_stage']) {

        echo "Mismatch: {$r['submission_no']}\n";
        echo "  stage     : {$r['stage']}\n";
        echo "  workflow  : {$r['workflow_stage']}\n";
        echo "  expected  : {$expected}\n\n";

        $errors++;
    }
}

echo "=================================\n";
echo "Total mismatches : {$errors}\n";
echo "=================================\n";
