/**
 * PR-27 smoke — PagerDuty + Teams channel exports + outbox routing.
 *
 * The full behaviour (config validation, payload shape, error
 * surfaces) is covered by the jest suites:
 *   src/lib/notification-system/channels/__tests__/pagerduty.channel.test.ts
 *   src/lib/notification-system/channels/__tests__/teams.channel.test.ts
 *
 * This smoke is a coarser export contract check: the two new
 * channel types are routable through the outbox dispatcher and
 * the type unions are extended.
 *
 * Run: npx tsx scripts/smoke/pr27-pagerduty-teams.ts
 */
import './_lib';
import { ok, fail } from './_lib';
import { PagerDutyChannel } from '../../src/lib/notification-system/channels/pagerduty.channel';
import { TeamsChannel } from '../../src/lib/notification-system/channels/teams.channel';
import { defaultDelivery } from '../../src/lib/notification-system/outbox/outbox.dispatcher';
import type { OutboxEntry } from '../../src/lib/notification-system/outbox/types';

async function main(): Promise<void> {
    // 1. PagerDuty channel exports the right shape.
    const pd = new PagerDutyChannel();
    if (pd.type !== 'pagerduty') fail('PR-27 pd-type', `expected type='pagerduty', got '${pd.type}'`);
    if (typeof pd.sendOrThrow !== 'function') fail('PR-27 pd-impl', 'sendOrThrow missing');
    ok('PagerDutyChannel exports type=pagerduty + sendOrThrow');

    // 2. Teams channel exports the right shape.
    const t = new TeamsChannel();
    if (t.type !== 'teams') fail('PR-27 teams-type', `expected type='teams', got '${t.type}'`);
    if (typeof t.sendOrThrow !== 'function') fail('PR-27 teams-impl', 'sendOrThrow missing');
    ok('TeamsChannel exports type=teams + sendOrThrow');

    // 3. Outbox dispatcher routes a malformed payload (no exception, returns ok:false).
    //    This proves the new channelType values are in the dispatcher's switch.
    const badEntry: OutboxEntry = {
        id: 1,
        eventKey: 'k',
        eventType: 'monitor_down',
        channelType: 'pagerduty' as never,
        channelId: 1,
        payload: 'not-json',
        attempts: 0,
        maxAttempts: 3,
        lastError: null,
        nextAttemptAt: new Date(),
        deliveredAt: null,
        failedAt: null,
        createdAt: new Date(),
    };
    const res = await defaultDelivery(badEntry);
    if (res.ok) fail('PR-27 dispatcher-bad-payload', 'malformed payload should fail');
    if (!('error' in res) || !/Invalid outbox payload/i.test(res.error)) {
        fail('PR-27 dispatcher-bad-payload', `wrong error path: ${'error' in res ? res.error : 'no-error'}`);
    }
    ok('outbox dispatcher rejects malformed payloads for pagerduty channel type');

    // 4. Same check for teams channelType.
    const badTeamsEntry = { ...badEntry, channelType: 'teams' as never };
    const res2 = await defaultDelivery(badTeamsEntry);
    if (res2.ok) fail('PR-27 dispatcher-bad-teams', 'malformed payload should fail');
    ok('outbox dispatcher recognizes teams channel type');

    ok('PR-27 channels smoke', 'PagerDuty + Teams exports + outbox routing OK');
}

main().catch((e) => fail('PR-27 crashed', String(e)));
