/* ═══════════════════════════════════════════════════════════
   OpAgent Inside — shared UI primitives
   ═══════════════════════════════════════════════════════════ */
const { useState, useEffect, useRef } = React;

/* ── canonical OpAgent target mark (brand identity logo) ── */
function Reticle({ size = 30, color = "#163D2B", dot = "#C4973E" }){
  return (
    <svg className="reticle" width={size} height={size} viewBox="0 0 40 40" fill="none" aria-label="OpAgent" style={{display:"block",flexShrink:0}}>
      <circle cx="20" cy="20" r="17" stroke={color} strokeWidth="0.5" opacity="0.35"/>
      <g stroke={color} strokeWidth="0.8" opacity="0.55">
        <line x1="20" y1="2" x2="20" y2="4.5"/><line x1="20" y1="35.5" x2="20" y2="38"/>
        <line x1="2" y1="20" x2="4.5" y2="20"/><line x1="35.5" y1="20" x2="38" y2="20"/>
        <line x1="7.3" y1="7.3" x2="9.1" y2="9.1"/><line x1="30.9" y1="30.9" x2="32.7" y2="32.7"/>
        <line x1="7.3" y1="32.7" x2="9.1" y2="30.9"/><line x1="30.9" y1="9.1" x2="32.7" y2="7.3"/>
      </g>
      <circle cx="20" cy="20" r="13.5" stroke={color} strokeWidth="1.6"/>
      <g stroke={color} strokeWidth="1.6" strokeLinecap="square">
        <line x1="20" y1="3.5" x2="20" y2="12"/><line x1="20" y1="28" x2="20" y2="36.5"/>
        <line x1="3.5" y1="20" x2="12" y2="20"/><line x1="28" y1="20" x2="36.5" y2="20"/>
      </g>
      <circle cx="20" cy="20" r="4.6" fill={dot}/>
      <circle cx="20" cy="20" r="1.6" fill={color}/>
    </svg>
  );
}

/* ── Q+i monogram — used only as a subtle corner watermark ── */
function QiWatermark({ onDark, inAside }){
  const qFill = onDark ? "rgba(253,252,249,0.06)" : "rgba(22,61,43,0.05)";
  const iFill = onDark ? "rgba(196,151,62,0.42)" : "rgba(196,151,62,0.12)";
  return (
    <svg className={"qi-watermark"+(inAside?" in-aside":"")} viewBox="0 0 100 100" aria-hidden="true">
      <text x="50" y="88" textAnchor="middle" fontFamily="Georgia,'Times New Roman',serif" fontWeight="700" fontSize="100" fill={qFill}>Q</text>
      <text x="50" y="59" textAnchor="middle" fontFamily="Georgia,'Times New Roman',serif" fontSize="27" fill={iFill}>i</text>
    </svg>
  );
}

function Wordmark({ color="#163D2B" }){
  return <span className="wm" style={{color}}>OpAgent<span className="ai">.ai</span></span>;
}

/* ── REF tag ── */
function Ref(){ return <span className="ref">REF</span>; }

/* ── chips ── */
function Chip({ kind, children, dot, dotColor }){
  return <span className={"chip "+kind}>{dot && <span className="cd" style={{background:dotColor}}></span>}{children}</span>;
}
function EngineChip({ name }){ return <Chip kind="engine">{name}</Chip>; }
function OwnerChip({ name }){ return <Chip kind="owner">◦ {name}</Chip>; }
function ClockChip({ when, urgent }){ return <span className={"chip clock"+(urgent?" urgent":"")}>◷ {when}</span>; }
function PayerChip({ name }){ return <Chip kind="payer">{name}</Chip>; }
function CptChip({ code }){ return <Chip kind="cpt">{code}</Chip>; }

/* ── dollar ── */
function Dollar({ amount, lost, refTag, sm }){
  if(!amount) return null;
  return <span className={"dollar"+(lost?" lost":"")+(sm?" sm":"")}>{amount}{refTag && <span className="dref">REF</span>}</span>;
}

/* ── deep-link (propose-only; stages, human commits) ── */
function DeepLink({ label = "Deep-link to DrChrono", onStage }){
  const [staged, setStaged] = useState(false);
  return (
    <div style={{display:"flex", flexDirection:"column", alignItems:"flex-end"}}>
      <button className={"deeplink"+(staged?" staged":"")} onClick={()=>{ if(!staged){ setStaged(true); onStage && onStage(); } }}>
        {staged
          ? <><span className="dk-ic">✓</span> Staged — open in DrChrono to commit</>
          : <><span className="dk-ic">⤴</span> {label} <span className="arrow">↗</span></>}
      </button>
      <span className="propose-note">{staged ? "awaiting human commit" : "proposes · never writes"}</span>
    </div>
  );
}

/* ── hero tile ── */
function Tile({ label, value, sub, kind }){
  const cls = kind==="gilt" ? "gilt" : kind==="deny" ? "deny" : kind==="rej" ? "rej" : "";
  const vcls = kind==="gilt" ? "gilt" : kind==="money" ? "money" : "";
  return (
    <div className={"tile "+cls}>
      <div className="tl">{label}</div>
      <div className={"tv "+vcls}>{value}</div>
      <div className="tsub">{sub}</div>
    </div>
  );
}

/* ── section head ── */
function SHead({ title, count }){
  return <div className="shead"><h3>{title}</h3>{count && <span className="sc">{count}</span>}<span className="ln"></span></div>;
}

/* ── toast ── */
function Toast({ msg, sub, show }){
  return (
    <div className={"toast"+(show?" show":"")}>
      <span className="tk">⤴</span>
      <span className="tx"><b>{msg}</b>{sub && <span className="tsub">{sub}</span>}</span>
    </div>
  );
}

Object.assign(window, {
  Reticle, QiWatermark, Wordmark, Ref, Chip, EngineChip, OwnerChip, ClockChip, PayerChip, CptChip,
  Dollar, DeepLink, Tile, SHead, Toast
});
