/**
 * PR-35 / EA-4 smoke — Monitor.jsonPath + jsonExpectedValue are gone.
 *
 * Both columns existed but no HTTP probe strategy ever evaluated them.
 * Operators who set them in the UI got a confirmed-up monitor that
 * silently ignored the assertion. PR-35 deletes the columns, the
 * validation schema, the test file, and the jsonpath-plus dependency.
 *
 * Asserts no references in any source file (schema, validations,
 * test, code, package.json). Migration file and HARDENING_PLAN.md
 * are allowed to mention the legacy names — those are historical
 * records.
 */
import './_lib';
import { ok, fail } from './_lib';
import { readFileSync, existsSync } from 'fs';
import path from 'path';
import { execSync } from 'child_process';

const ROOT = path.resolve(__dirname, '../..');

function read(label: string, rel: string): string {
    const abs = path.join(ROOT, rel);
    if (!existsSync(abs)) fail(`ea-4: ${label} missing`, abs);
    return readFileSync(abs, 'utf8');
}

function main(): void {
    const schema = read('schema.prisma', 'prisma/schema.prisma');
    const validations = read('monitor.schema.ts', 'src/lib/validations/monitor.schema.ts');
    const pkg = read('package.json', 'package.json');
    const lock = read('package-lock.json', 'package-lock.json');

    // 1. Schema: no field declarations
    if (/^\s*jsonPath\s+String/m.test(schema)) fail('ea-4: schema.prisma still declares jsonPath column', 'fields not dropped');
    if (/^\s*jsonExpectedValue\s+String/m.test(schema)) fail('ea-4: schema.prisma still declares jsonExpectedValue column', 'fields not dropped');
    ok('ea-4: schema.prisma no longer declares the columns');

    // 2. Zod schema doesn't reference them
    if (/jsonPath\s*:/.test(validations) || /jsonExpectedValue\s*:/.test(validations)) {
        fail('ea-4: monitor.schema.ts still has jsonPath field', 'validation not cleaned');
    }
    // Negative-lookbehind on backtick so a comment that mentions the
    // dropped helper (e.g. ``jsonpath-plus dependency is no longer
    // needed``) doesn't trip the check.
    if (/(?<!`)isValidJsonPath/.test(validations)) {
        fail('ea-4: monitor.schema.ts still references isValidJsonPath', 'helper not removed');
    }
    if (/from\s+['"]jsonpath-plus['"]|require\(['"]jsonpath-plus['"]/.test(validations)) {
        fail('ea-4: monitor.schema.ts still imports jsonpath-plus', 'import not removed');
    }
    ok('ea-4: monitor.schema.ts has no jsonPath validation');

    // 3. Test file deleted
    const testFile = path.join(ROOT, 'src/lib/validations/__tests__/monitor.schema.test.ts');
    if (existsSync(testFile)) {
        fail('ea-4: monitor.schema.test.ts still exists', 'test file should be deleted');
    }
    ok('ea-4: monitor.schema.test.ts deleted');

    // 4. package.json no longer declares the dep
    if (/"jsonpath-plus"\s*:/.test(pkg)) {
        fail('ea-4: package.json still declares jsonpath-plus', 'dep not removed');
    }
    if (/"jsonpath-plus":/.test(lock)) {
        fail('ea-4: package-lock.json still has jsonpath-plus entry', 'npm install not run');
    }
    ok('ea-4: jsonpath-plus removed from package.json + lockfile');

    // 5. No stale source references (excluding migration files + docs)
    let hits: string;
    try {
        hits = execSync(
            'grep -rn "jsonPath\\|jsonExpectedValue" src/ 2>&1 || true',
            { encoding: 'utf8', cwd: ROOT },
        );
    } catch {
        hits = '';
    }
    const real = hits
        .split('\n')
        .filter((l) => l.trim() && !l.startsWith('Binary file'))
        .join('\n');
    if (real.trim()) {
        fail('ea-4: stale jsonPath references in src/', real.split('\n').slice(0, 5).join(' || '));
    }
    ok('ea-4: no remaining references in src/');

    ok('PR-35 jsonPath kill', 'columns dropped, code cleaned, dep removed');
}

main();
