/**
 * Theme C smoke — maintenance-window evaluator.
 *
 * Validates the in-memory predicate end-to-end without hitting the DB.
 * Run: npx tsx scripts/smoke/c-maintenance-window.ts
 */
import './_lib';
import { ok, fail } from './_lib';
import { isWithinMaintenanceWindow } from '../../src/lib/services/maintenance-window';
import type { MaintenanceWindow } from '@prisma/client';

function w(overrides: Partial<MaintenanceWindow>): MaintenanceWindow {
    return {
        id: 1,
        monitorId: null,
        startsAt: new Date('2026-06-01T10:00:00Z'),
        endsAt: new Date('2026-06-01T11:00:00Z'),
        reason: 'maintenance',
        recurrence: null,
        createdAt: new Date('2026-05-01T00:00:00Z'),
        createdById: 1,
        deletedAt: null,
        deletedById: null,
        ...overrides,
    } as MaintenanceWindow;
}

function main(): void {
    const now = new Date('2026-06-01T10:30:00Z');

    // One-shot, scoped to monitor 5
    const oneShot = w({ monitorId: 5 });
    if (!isWithinMaintenanceWindow(now, 5, [oneShot])) fail('C.one-shot', 'should match');
    ok('one-shot window matches inside its range');

    if (isWithinMaintenanceWindow(now, 6, [oneShot])) fail('C.scoping', 'monitor mismatch should reject');
    ok('one-shot window respects monitor scoping');

    // Operator-wide
    const wide = w({ monitorId: null });
    if (!isWithinMaintenanceWindow(now, 99, [wide])) fail('C.wide', 'null monitorId should match all');
    ok('operator-wide (monitorId=null) matches every monitor');

    // Soft-deleted ignored
    const deleted = w({ monitorId: 5, deletedAt: new Date('2026-05-30T00:00:00Z') });
    if (isWithinMaintenanceWindow(now, 5, [deleted])) fail('C.soft-delete', 'should ignore');
    ok('soft-deleted windows are ignored');

    // Recurrence
    const wednesday = new Date('2026-06-03T10:30:00Z');
    const recurring = w({ monitorId: 5, recurrence: 'MON,WED' });
    if (!isWithinMaintenanceWindow(wednesday, 5, [recurring])) fail('C.recurrence', 'WED should match');
    ok('weekly recurrence matches on listed day');

    const tuesday = new Date('2026-06-02T10:30:00Z');
    if (isWithinMaintenanceWindow(tuesday, 5, [recurring])) fail('C.recurrence', 'TUE should NOT match');
    ok('weekly recurrence rejects non-listed day');

    ok('Theme C maintenance-window smoke', 'predicate contracts pass');
}

main();
