'use client';
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import { useState } from 'react';
import { Button } from '../ui/button.js';
import { Badge } from '../ui/badge.js';
import { Drawer, DrawerContent, DrawerDescription, DrawerHeader, DrawerTitle, DrawerTrigger, } from '../ui/drawer.js';
import { SupervisorVerdict } from '../../../../../packages/core/src/domain/generated/output.js';
const VERDICT_VARIANT = {
    [SupervisorVerdict.approve]: 'default',
    [SupervisorVerdict.reject]: 'destructive',
    [SupervisorVerdict.escalate]: 'secondary',
    [SupervisorVerdict.advise]: 'secondary',
};
function formatTimestamp(iso) {
    try {
        return new Date(iso).toLocaleString();
    }
    catch {
        return iso;
    }
}
export function SupervisorDecisionWhyDrawer({ decisions, triggerLabel = 'Why?', defaultOpen = false, }) {
    const [open, setOpen] = useState(defaultOpen);
    if (decisions.length === 0)
        return null;
    // Decisions are ordered newest-first when supplied by the SSE stream; render
    // chronological so the audit trail reads top-to-bottom.
    const ordered = [...decisions].sort((a, b) => new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime());
    const latest = ordered[ordered.length - 1];
    return (_jsxs(Drawer, { open: open, onOpenChange: setOpen, direction: "right", children: [_jsx(DrawerTrigger, { asChild: true, children: _jsx(Button, { type: "button", variant: "ghost", size: "sm", "data-testid": "supervisor-why-trigger", children: triggerLabel }) }), _jsxs(DrawerContent, { className: "max-w-md", "data-testid": "supervisor-why-drawer", direction: "right", children: [_jsxs(DrawerHeader, { children: [_jsx(DrawerTitle, { children: "Supervisor decision" }), _jsx(DrawerDescription, { children: ordered.length === 1
                                    ? 'Why the supervisor reached this verdict.'
                                    : `Audit trail for ${ordered.length} decisions.` })] }), _jsx("div", { className: "flex flex-1 flex-col gap-4 overflow-y-auto px-4 pb-6", "data-testid": "supervisor-why-content", children: ordered.map((decision) => (_jsxs("article", { className: "flex flex-col gap-2 rounded border p-3", "data-testid": `decision-row-${decision.decisionId}`, children: [_jsxs("header", { className: "flex items-center justify-between gap-2", children: [_jsx(Badge, { variant: VERDICT_VARIANT[decision.verdict] ?? 'secondary', "data-testid": `decision-verdict-${decision.decisionId}`, className: "capitalize", children: decision.verdict }), _jsx("time", { className: "text-muted-foreground text-xs", children: formatTimestamp(decision.createdAt) })] }), _jsx("p", { className: "text-sm whitespace-pre-wrap", "data-testid": `decision-rationale-${decision.decisionId}`, children: decision.rationale }), _jsxs("dl", { className: "text-muted-foreground grid grid-cols-2 gap-x-4 gap-y-1 text-xs", children: [_jsx("dt", { children: "Model" }), _jsx("dd", { "data-testid": `decision-model-${decision.decisionId}`, children: decision.modelId }), _jsx("dt", { children: "Prompt version" }), _jsx("dd", { "data-testid": `decision-prompt-${decision.decisionId}`, children: decision.promptVersion }), decision.ruleRef ? (_jsxs(_Fragment, { children: [_jsx("dt", { children: "Rule" }), _jsx("dd", { "data-testid": `decision-rule-${decision.decisionId}`, children: decision.ruleRef })] })) : null, typeof decision.confidence === 'number' ? (_jsxs(_Fragment, { children: [_jsx("dt", { children: "Confidence" }), _jsxs("dd", { "data-testid": `decision-confidence-${decision.decisionId}`, children: [Math.round(decision.confidence * 100), "%"] })] })) : null, _jsx("dt", { children: "Source" }), _jsxs("dd", { children: [decision.sourceEventKind, " \u00B7 ", decision.sourceEventId] })] })] }, decision.decisionId))) }), _jsxs("div", { className: "text-muted-foreground border-t p-4 text-xs", children: ["Latest verdict: ", _jsx("span", { className: "font-medium", children: latest.verdict })] })] })] }));
}
