<?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');
$layout = file_get_contents($root . '/app/Views/layouts/main.php');
$repo = file_get_contents($root . '/app/Repositories/DoctorsRepository.php');
$controller = file_get_contents($root . '/app/Controllers/DoctorsController.php');

$formClose = strpos($view, '</form>');
$noticePosition = strpos($view, 'doctors712-fair-comparison');

$checks = [
    'version' => ($version['version'] ?? '') === '11.0.11.77',
    'phase' => ($version['phase'] ?? '') === 'H11.0-P11.1-R45-P7.1.2',
    'card_class' => strpos($view, 'doctors712-controls-card') !== false,
    'filter_class' => strpos($view, 'doctors712-filter') !== false,
    'notice_class' => strpos($view, 'doctors712-fair-comparison') !== false,
    'notice_after_form' => $formClose !== false && $noticePosition !== false && $noticePosition > $formClose,
    'grid_areas' => strpos($css, '"context filters"') !== false
        && strpos($css, '"notice notice"') !== false,
    'top_alignment' => strpos($css, 'align-items: start') !== false,
    'notice_grid_area' => strpos($css, 'grid-area: notice') !== false,
    'notice_full_width' => strpos($css, 'width: 100%') !== false
        && strpos($css, 'max-width: none') !== false,
    'cache_identity_updated' => strpos(
        $layout,
        'executive-ui-core.css?v=11.0.11.77-r45-p7-1-2-doctors-layout'
    ) !== false,
    'tablet_contract' => strpos($css, '@media (max-width: 1180px)') !== false,
    'mobile_contract' => 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_2']['oracle_writes'] ?? '') === 'NONE',
];

$failed = [];
foreach ($checks as $name => $ok) {
    if (!$ok) $failed[] = $name;
}

echo json_encode([
    'schema' => 'R45_P7_1_2_QA',
    'checks' => $checks,
    'passed' => count($checks) - count($failed),
    'failed' => $failed,
], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES), PHP_EOL;

exit(count($failed) === 0 ? 0 : 1);
