<?php
declare(strict_types=1);

$root = dirname(__DIR__);
$repo = file_get_contents($root . '/app/Repositories/VisitsRepository.php');
$controller = file_get_contents($root . '/app/Controllers/OperationsController.php');

$patterns = [
    'extract_year_predicates' => preg_match_all('/EXTRACT\s*\(\s*YEAR\s+FROM/iu', $repo, $m1),
    'trunc_date_predicates' => preg_match_all('/TRUNC\s*\(\s*r\.expecteddonedate\s*\)/iu', $repo, $m2),
    'database_fetch_all_calls' => preg_match_all('/Database::fetchAll\s*\(/u', $repo, $m3),
    'metadata_all_tab_columns' => preg_match_all('/all_tab_columns/iu', $repo, $m4),
    'metadata_all_tables' => preg_match_all('/all_tables/iu', $repo, $m5),
    'safe_dataset_calls' => preg_match_all('/safeDataset\s*\(/u', $controller, $m6),
];

$result = [
    'schema' => 'DDB_VISITS_SQL_STATIC_AUDIT_V1',
    'captured_at' => date(DATE_ATOM),
    'patterns' => $patterns,
    'observations' => [
        'The Visits page uses one consolidated monthly OPD/Emergency bundle.',
        'Year-mode filters use direct half-open date ranges; EXTRACT is not used in WHERE predicates.',
        'Daily trend uses a direct date-column range and keeps TRUNC only for grouping/labels.',
        'Metadata discovery queries ALL_TAB_COLUMNS and ALL_TABLES.',
        'The controller loads all Visits datasets before rendering.',
    ],
    'source_changes' => 'NONE',
    'oracle_writes' => 'NONE',
];

echo json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), PHP_EOL;
