// P7 AI smoke: proposal-only. Auto-CLASSIFY → AI flag → routing REVIEW/DENY never
// ALLOW (KG-05); SUMMARIZE cites evidence; EXTRACT stays DISCUSSION (Scenario 7);
// fail-closed; budget degrade (KG-12); replay-once; accept. No real network.
import 'dotenv/config';
import crypto from 'node:crypto';
import jwt from 'jsonwebtoken';

const SERVICE = process.env.SMOKE_URL ?? 'http://localhost:3200';
const APP_ID = 'portal-demo';
const APP_SECRET = JSON.parse(process.env.APP_SECRETS ?? '{"portal-demo":"dev-secret"}')[APP_ID];
const ADAPTER_SECRET = (() => {
  try { return JSON.parse(process.env.ADAPTER_SECRETS ?? '{}').WEBHOOK ?? 'dev-adapter-secret'; } catch { return 'dev-adapter-secret'; }
})();

const token = jwt.sign({ sub: 'ai-studio', appId: APP_ID, orgId: `org_${APP_ID}` }, APP_SECRET, { algorithm: 'HS256', expiresIn: '1h' });
const sign = (body) => 'sha256=' + crypto.createHmac('sha256', ADAPTER_SECRET).update(body).digest('hex');
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
const assert = (c, m) => { if (!c) { console.error('✗', m); process.exit(1); } console.log('✓', m); };

async function req(path, method = 'GET', body) {
  const r = await fetch(`${SERVICE}${path}`, {
    method,
    headers: { 'content-type': 'application/json', authorization: `Bearer ${token}` },
    body: body ? JSON.stringify(body) : undefined,
  });
  if (!r.ok) throw new Error(`${method} ${path} ${r.status}: ${await r.text()}`);
  return r.json();
}
async function pollUntil(fn, ms = 8000) {
  const end = Date.now() + ms;
  while (Date.now() < end) { const v = await fn(); if (v) return v; await sleep(300); }
  return null;
}

async function ingest(text) {
  const payload = { eventId: `ai-sm-${crypto.randomUUID()}`, from: 'sim@member', text, threadRef: `sm-${Date.now()}` };
  const body = JSON.stringify(payload);
  const wh = await fetch(`${SERVICE}/v1/adapters/WEBHOOK/webhook`, {
    method: 'POST',
    headers: { 'content-type': 'application/json', 'x-iios-signature': sign(body) },
    body,
  });
  if (wh.status !== 202) throw new Error(`webhook ${wh.status}`);
  const e = await pollUntil(async () => {
    const list = await req('/v1/adapters/inbound');
    const hit = list.find((x) => x.externalEventId === payload.eventId);
    return hit && hit.interactionId ? hit : null;
  });
  if (!e) throw new Error('interaction never normalized');
  return e.interactionId;
}

// ── KG-05: inbound "party at 9 PM" is auto-classified; AI flag routes to REVIEW/DENY, never ALLOW ──
const interactionId = await ingest('There is a party at 9 PM tonight!');

// The AiProjector auto-runs CLASSIFY on interaction.normalized — wait for the AI flag.
const flagged = await pollUntil(async () => {
  const arts = await req(`/v1/ai/artifacts?interactionId=${interactionId}`);
  return arts.find((a) => a.artifactType === 'CLASSIFICATION') ?? null;
}, 10000);
assert(flagged, 'inbound message auto-classified by the AiProjector (proposal)');
assert(flagged.claims.some((c) => c.claimType === 'MODERATION_FLAG'), 'CLASSIFICATION proposes a MODERATION_FLAG claim');

// Route it to an unrestricted auto-ALLOW binding + a CHILD binding.
const adults = await req('/v1/routes/bindings', 'POST', { originChannelType: 'WEBHOOK', originRef: 'webhook', destinationChannelType: 'PORTAL', destinationRef: 'adults', requiresReview: false, enabled: true });
const children = await req('/v1/routes/bindings', 'POST', { originChannelType: 'WEBHOOK', originRef: 'webhook', destinationChannelType: 'PORTAL', destinationRef: 'children', restrictionProfile: 'CHILD', requiresReview: false, enabled: true });
const before = (await req('/v1/adapters/outbound')).length;
const { decisions } = await req('/v1/routes/simulate', 'POST', { interactionId, originChannelType: 'WEBHOOK', originRef: 'webhook' });
const state = (id) => decisions.find((d) => d.routeBindingId === id)?.decisionState;
assert(state(adults.id) === 'REVIEW', 'AI flag forces adults → REVIEW (never ALLOW) — KG-05');
assert(state(children.id) === 'DENY', 'AI flag on CHILD → DENY (deny-by-default)');
assert((await req('/v1/adapters/outbound')).length === before, 'AI proposal + simulate sent nothing');

// ── SUMMARIZE cites evidence ──
const sum = await req('/v1/ai/jobs', 'POST', { interactionId, jobType: 'SUMMARIZE' });
assert(sum.artifact.artifactType === 'SUMMARY' && String((sum.artifact.contentRef ?? {}).text).includes('[ai]'), 'SUMMARIZE → [ai] summary artifact');
assert((sum.artifact.evidence ?? []).some((e) => e.sourceId === interactionId), 'summary cites the source interaction');

// ── EXTRACT stays DISCUSSION (Scenario 7) ──
const ext = await req('/v1/ai/jobs', 'POST', { interactionId, jobType: 'EXTRACT' });
const eventClaim = (ext.artifact.claims ?? []).find((c) => c.claimType === 'EVENT');
assert(eventClaim && eventClaim.claimJson.certainty === 'DISCUSSION', 'EXTRACT event certainty=DISCUSSION (never CONFIRMED) — Scenario 7');

// ── replay a job twice → one artifact ──
const safe = await ingest('The committee met today. Budget discussed.');
await req('/v1/ai/jobs', 'POST', { interactionId: safe, jobType: 'SUMMARIZE' });
const a1 = await req(`/v1/ai/artifacts?interactionId=${safe}`);
await req('/v1/ai/jobs', 'POST', { interactionId: safe, jobType: 'SUMMARIZE' });
const a2 = await req(`/v1/ai/artifacts?interactionId=${safe}`);
assert(a1.filter((a) => a.artifactType === 'SUMMARY').length === 1 && a2.filter((a) => a.artifactType === 'SUMMARY').length === 1, 'replaying a job is idempotent (one artifact)');

// ── accept records human feedback ──
const accepted = await req(`/v1/ai/artifacts/${sum.artifact.id}/accept`, 'POST');
assert(accepted.status === 'ACCEPTED', 'accept → status ACCEPTED (human feedback, not a send)');

console.log('\nP7 AI smoke: PASS');
process.exit(0);
