<?php
$root = dirname(__DIR__);
$version = json_decode(file_get_contents($root . '/DDB_VERSION.json'), true);
$view = file_get_contents($root . '/app/Views/doctors/index.php');
$css = file_get_contents($root . '/public/assets/css/executive-ui-core.css');
$repo = file_get_contents($root . '/app/Repositories/DoctorsRepository.php');
$controller = file_get_contents($root . '/app/Controllers/DoctorsController.php');

$checks = [
    'version' => ($version['version'] ?? '') === '11.0.11.76',
    'phase' => ($version['phase'] ?? '') === 'H11.0-P11.1-R45-P7.1.1',
    'card_class' => strpos($view, 'doctors711-controls-card') !== false,
    'context_class' => strpos($view, 'doctors711-controls-context') !== false,
    'filter_class' => strpos($view, 'doctors711-filter') !== false,
    'fair_notice_class' => strpos($view, 'doctors711-fair-comparison') !== false,
    'desktop_two_column' => strpos($css, 'grid-template-columns: minmax(260px, .9fr) minmax(0, 2.1fr)') !== false,
    'field_row' => strpos($css, 'minmax(220px, 1.25fr)') !== false,
    'fair_notice_full_width' => strpos($css, '.doctors711-fair-comparison') !== false
        && strpos($css, 'grid-column: 1 / -1') !== false,
    'tablet_layout' => strpos($css, '@media (max-width: 1180px)') !== false,
    'mobile_layout' => strpos($css, '@media (max-width: 720px)') !== false,
    'fair_policy_preserved' => strpos($controller, 'FairYearComparisonPolicy') !== false,
    'month_filter_preserved' => strpos($repo, 'MONTHID BETWEEN :start_month AND :end_month') !== false,
    'oracle_writes_none' => ($version['r45_p7_1_1']['oracle_writes'] ?? '') === 'NONE',
];

$failed = [];
foreach ($checks as $name => $ok) {
    if (!$ok) $failed[] = $name;
}
echo json_encode([
    'schema' => 'R45_P7_1_1_QA',
    'checks' => $checks,
    'passed' => count($checks) - count($failed),
    'failed' => $failed,
], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES), PHP_EOL;
exit(count($failed) === 0 ? 0 : 1);
