/**
 * auditor-toast-migration-batch1 smoke — guards 2026-06-01 T3I migration.
 *
 * Auditor finding: ~30 native alert()/confirm() calls bypassed the
 * existing Toast / ConfirmDialog systems, producing OS-modal popups
 * that block the JS thread and look out-of-place against the rest
 * of the dashboard chrome. Auditor flagged this as polish-tier.
 *
 * BATCH 1 (5 high-visibility files):
 *   - components/settings/GeneralSettings.tsx
 *   - components/settings/CountriesManager.tsx
 *   - components/settings/UserManagement.tsx
 *   - components/maintenance/MaintenanceList.tsx
 *   - components/profile/UserProfileHeader.tsx
 *
 * BATCH 2 (the remaining 16 files — landed in this PR):
 *   - components/settings/ApiTokenSettings.tsx
 *   - components/settings/ExecutiveTokensManager.tsx
 *   - components/settings/RulesList.tsx
 *   - components/settings/SystemStatus.tsx
 *   - components/settings/TemplateSettings.tsx
 *   - components/settings/TicketStatsSettings.tsx
 *   - components/settings/RegionsManager.tsx
 *   - components/profile/NotificationChannels.tsx
 *   - components/profile/SystemChannels.tsx
 *   - components/audit/StatusHistoryTable.tsx
 *   - components/chat/ChatWidget.tsx
 *   - components/incidents/IncidentDetailView.tsx
 *   - components/monitors/AddMonitorModal.tsx
 *   - app/audit-logs/page-content.tsx
 *   - app/dashboard/incidents/page.tsx
 *   - hooks/useMonitors.ts
 *
 * The file list below now covers all known sites; each must import
 * useToast and/or useConfirm and contain no bare alert()/confirm('string') calls.
 */
import './_lib';
import { ok, fail } from './_lib';
import { readFileSync } from 'fs';
import path from 'path';

const ROOT = path.resolve(__dirname, '../..');
const read = (rel: string): string => readFileSync(path.join(ROOT, rel), 'utf8');

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

const BATCH_ALL = [
    // Batch 1
    'src/components/settings/GeneralSettings.tsx',
    'src/components/settings/CountriesManager.tsx',
    'src/components/settings/UserManagement.tsx',
    'src/components/maintenance/MaintenanceList.tsx',
    'src/components/profile/UserProfileHeader.tsx',
    // Batch 2
    'src/components/settings/ApiTokenSettings.tsx',
    'src/components/settings/ExecutiveTokensManager.tsx',
    'src/components/settings/RulesList.tsx',
    'src/components/settings/SystemStatus.tsx',
    'src/components/settings/TemplateSettings.tsx',
    'src/components/settings/TicketStatsSettings.tsx',
    'src/components/settings/RegionsManager.tsx',
    'src/components/profile/NotificationChannels.tsx',
    'src/components/profile/SystemChannels.tsx',
    'src/components/audit/StatusHistoryTable.tsx',
    'src/components/chat/ChatWidget.tsx',
    'src/components/incidents/IncidentDetailView.tsx',
    'src/components/monitors/AddMonitorModal.tsx',
    'src/app/audit-logs/page-content.tsx',
    'src/app/dashboard/incidents/page.tsx',
    'src/hooks/useMonitors.ts',
];

function main(): void {
    for (const file of BATCH_ALL) {
        const src = read(file);

        // Each migrated file imports useToast or useConfirm (or both).
        const usesToast = /import.*useToast.*from.*['"]@\/components\/ui\/Toast['"]/.test(src);
        const usesConfirm = /import.*useConfirm.*from.*['"]@\/components\/ui\/ConfirmDialogProvider['"]/.test(src);
        assert(`${file}: imports useToast or useConfirm`,
            usesToast || usesConfirm,
            `${file} doesn't import the Toast/ConfirmDialog hooks — toast migration regressed.`);

        // No bare `alert(` calls remain (excluding AlertCircle, AlertTriangle, etc).
        const bareAlert = /(?<![A-Za-z_])alert\(/.test(src);
        assert(`${file}: no bare alert() calls`,
            !bareAlert,
            `${file} still contains alert() — toast migration regression.`);

        // No bare `confirm(` calls (must use `await confirm({...})` from useConfirm).
        // Heuristic: bare `confirm('...')` with a string-literal first arg is the
        // pattern we want gone. Object-literal calls are the new form.
        const bareConfirmStringArg = /(?<![A-Za-z_.])confirm\(\s*['"]/.test(src);
        assert(`${file}: no bare confirm('string') calls`,
            !bareConfirmStringArg,
            `${file} still contains a native confirm('string') call — should use await confirm({title, description}).`);
    }

    ok('auditor-toast-migration-batch1', `${BATCH_ALL.length} files migrated to Toast/ConfirmDialog`);
}

main();
