/**
 * auditor-dup-incidents-route-gone smoke — guards T3J (2026-06-01).
 *
 * The /incidents and /dashboard/incidents routes were duplicates —
 * separate page files, both fetching /api/incidents, no redirect
 * between them. The sidebar nav and search index already pointed at
 * /dashboard/incidents as the canonical URL; /incidents was an
 * orphan that confused operators and split testing surface.
 *
 * Fix: deleted src/app/incidents/* and migrated the remaining
 * references (SystemStatus.tsx Links, the playwright e2e spec) to
 * /dashboard/incidents.
 */
import './_lib';
import { ok, fail } from './_lib';
import { existsSync, readFileSync } from 'fs';
import path from 'path';

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

function assert(label: string, cond: boolean, detail: string): void {
    if (!cond) fail(label, detail);
    ok(label);
}

function read(rel: string): string {
    return readFileSync(path.join(ROOT, rel), 'utf8');
}

function main(): void {
    // The duplicate route is gone.
    for (const f of ['src/app/incidents/page.tsx', 'src/app/incidents/page-content.tsx', 'src/app/incidents/[id]/page.tsx']) {
        assert(`duplicate file removed: ${f}`,
            !existsSync(path.join(ROOT, f)),
            `${f} still exists — auditor T3J regressed.`);
    }

    // The canonical /dashboard/incidents route is still in place.
    assert('canonical /dashboard/incidents/page.tsx present',
        existsSync(path.join(ROOT, 'src/app/dashboard/incidents/page.tsx')),
        'src/app/dashboard/incidents/page.tsx missing — canonical route disappeared.');
    assert('canonical /dashboard/incidents/[id]/page.tsx present',
        existsSync(path.join(ROOT, 'src/app/dashboard/incidents/[id]/page.tsx')),
        'src/app/dashboard/incidents/[id]/page.tsx missing.');

    // SystemStatus link rewrites landed.
    const sys = read('src/components/settings/SystemStatus.tsx');
    assert('SystemStatus.tsx links to /dashboard/incidents/<id>',
        /href=\{`\/dashboard\/incidents\/\$\{i\.id\}`\}/.test(sys),
        'SystemStatus.tsx links no longer point at /dashboard/incidents/<id>.');
    assert('SystemStatus.tsx no longer links to bare /incidents/<id>',
        !/href=\{`\/incidents\/\$\{i\.id\}`\}/.test(sys),
        'SystemStatus.tsx still has stale /incidents/<id> hrefs.');

    // e2e spec updated.
    const e2e = read('src/e2e/e2e/incident-reporting.spec.ts');
    assert('e2e incident-reporting goes to /dashboard/incidents',
        /page\.goto\('\/dashboard\/incidents'\)/.test(e2e),
        'e2e spec no longer navigates to /dashboard/incidents.');

    ok('auditor-dup-incidents-route-gone', 'duplicate /incidents route deleted; refs migrated');
}

main();
