const { Badge, Banner, Transcript, Prose, ArtifactFrame, KeyValueList, Button } =
  window.EquestrianismDesignSystem_6b57f9;

const EVENTS = [
  { type: 'session', body: 'Started from routine weekly-shop (0 7 * * 1).', time: '07:00:02' },
  { type: 'tool call', body: 'browser.open\n{ "url": "https://groceries.example.co.uk" }', time: '07:00:11' },
  { type: 'output', body: 'Basket restored from last week — 34 items, £86.20 before substitutions.', time: '07:01:48' },
  { type: 'warning', tone: 'warning', body: 'Two items unavailable: semi-skimmed 2L, sourdough. Proposing substitutions.', time: '07:02:03' },
  { type: 'question', body: 'The sourdough substitute is £1.40 dearer and a different bakery. Accept, or drop the item?', time: '07:02:04' },
];

function SessionDetail({ go }) {
  const [events, setEvents] = React.useState(EVENTS);
  React.useEffect(() => {
    const t = setInterval(() => {
      setEvents((ev) => ev.length < 8 ? [...ev, {
        type: 'poll', body: `Still waiting on the substitution question (${ev.length - 4}m).`,
        time: `07:0${Math.min(ev.length, 9)}:00`,
      }] : ev);
    }, 5000);
    return () => clearInterval(t);
  }, []);
  return (
    <div className="stack">
      <window.PageHead title="Weekly shop"
        sub={<span>session <code style={{ fontFamily: 'var(--font-mono)' }}>s-4189</code> · started 07:00 · $0.41 so far</span>}
        end={<Badge state="needs-input">Needs input</Badge>} />
      <Banner state="needs-input" action={<Button size="sm" onClick={() => {}}>Answer</Button>}>
        The agent asked a question 22 minutes ago; the run is paused until someone answers.
      </Banner>
      <div className="two-col" style={{ gridTemplateColumns: '3fr 2fr' }}>
        <div className="stack" style={{ gap: 12 }}>
          <p className="panel-lbl" style={{ margin: 0 }}>Transcript</p>
          <Transcript events={events} truncated connection="live" />
        </div>
        <div className="stack" style={{ gap: 12 }}>
          <div className="panel">
            <p className="panel-lbl">Details</p>
            <KeyValueList items={[
              { label: 'Routine', value: 'weekly-shop' },
              { label: 'Model', value: 'claude-sonnet-4-5' },
              { label: 'Budget cap', value: '$2.00' },
              { label: 'Spent so far', value: '$0.41' },
            ]} />
          </div>
          <div className="panel">
            <p className="panel-lbl">Artifact — substitution notes</p>
            <Prose>
              <h2>Proposed substitutions</h2>
              <ul>
                <li><strong>Semi-skimmed 2L</strong> → 2 × 1L, same brand (+£0.15)</li>
                <li><strong>Sourdough</strong> → awaiting your answer</li>
              </ul>
            </Prose>
          </div>
          <ArtifactFrame height={140} label="Sandboxed" note="Receipt preview, rendered as authored."
            srcDoc={'<body style="font-family:Georgia;padding:14px;background:#fff;color:#222"><b>groceries.example.co.uk</b><p style="margin:6px 0 0;font-size:14px">34 items · £86.20<br>Delivery: Tue 07:00–08:00</p></body>'} />
        </div>
      </div>
    </div>
  );
}
window.SessionDetail = SessionDetail;
