// Small UI primitives: icons, buttons, chips, mesh art

const Icon = ({name, size=16, stroke=1.8}) => {
  const s = size;
  const p = { width:s, height:s, viewBox:"0 0 24 24", fill:"none", stroke:"currentColor", strokeWidth:stroke, strokeLinecap:"round", strokeLinejoin:"round" };
  switch(name){
    case "search": return <svg {...p}><circle cx="11" cy="11" r="7"/><path d="m20 20-3.5-3.5"/></svg>;
    case "bell": return <svg {...p}><path d="M6 8a6 6 0 1 1 12 0c0 7 3 7 3 9H3c0-2 3-2 3-9"/><path d="M10 21a2 2 0 0 0 4 0"/></svg>;
    case "arrow-left": return <svg {...p}><path d="M19 12H5"/><path d="m12 19-7-7 7-7"/></svg>;
    case "arrow-right": return <svg {...p}><path d="M5 12h14"/><path d="m12 5 7 7-7 7"/></svg>;
    case "play": return <svg {...p} fill="currentColor" stroke="none"><path d="M8 5v14l11-7z"/></svg>;
    case "mic": return <svg {...p}><rect x="9" y="2" width="6" height="12" rx="3"/><path d="M5 10a7 7 0 0 0 14 0"/><path d="M12 17v5"/></svg>;
    case "send": return <svg {...p}><path d="M22 2 11 13"/><path d="M22 2 15 22l-4-9-9-4 20-7z"/></svg>;
    case "sparkle": return <svg {...p}><path d="M12 3v4M12 17v4M3 12h4M17 12h4M5.6 5.6l2.8 2.8M15.6 15.6l2.8 2.8M5.6 18.4l2.8-2.8M15.6 8.4l2.8-2.8"/></svg>;
    case "check": return <svg {...p}><path d="M20 6 9 17l-5-5"/></svg>;
    case "x": return <svg {...p}><path d="M18 6 6 18M6 6l12 12"/></svg>;
    case "clock": return <svg {...p}><circle cx="12" cy="12" r="9"/><path d="M12 7v5l3 2"/></svg>;
    case "chevron-down": return <svg {...p}><path d="m6 9 6 6 6-6"/></svg>;
    case "chevron-right": return <svg {...p}><path d="m9 6 6 6-6 6"/></svg>;
    case "filter": return <svg {...p}><path d="M3 5h18M6 12h12M10 19h4"/></svg>;
    case "plus": return <svg {...p}><path d="M12 5v14M5 12h14"/></svg>;
    case "bookmark": return <svg {...p}><path d="M19 21 12 16l-7 5V5a2 2 0 0 1 2-2h10a2 2 0 0 1 2 2z"/></svg>;
    case "grid": return <svg {...p}><rect x="3" y="3" width="7" height="7" rx="1.5"/><rect x="14" y="3" width="7" height="7" rx="1.5"/><rect x="3" y="14" width="7" height="7" rx="1.5"/><rect x="14" y="14" width="7" height="7" rx="1.5"/></svg>;
    case "user": return <svg {...p}><circle cx="12" cy="8" r="4"/><path d="M4 21c1.5-4 4.5-6 8-6s6.5 2 8 6"/></svg>;
    case "users": return <svg {...p}><circle cx="9" cy="8" r="3.5"/><path d="M3 20c1-3 3.2-4.5 6-4.5s5 1.5 6 4.5"/><circle cx="17" cy="9" r="2.5"/><path d="M15 15c3 0 5 1.5 6 4"/></svg>;
    case "layers": return <svg {...p}><path d="m12 3 9 5-9 5-9-5 9-5z"/><path d="m3 13 9 5 9-5"/><path d="m3 18 9 5 9-5"/></svg>;
    case "flag": return <svg {...p}><path d="M4 21V4"/><path d="M4 4h12l-2 4 2 4H4"/></svg>;
    case "calendar": return <svg {...p}><rect x="3" y="5" width="18" height="16" rx="2"/><path d="M3 10h18M8 3v4M16 3v4"/></svg>;
    case "trend-up": return <svg {...p}><path d="m3 17 6-6 4 4 8-8"/><path d="M14 7h7v7"/></svg>;
    case "trend-down": return <svg {...p}><path d="m3 7 6 6 4-4 8 8"/><path d="M14 17h7v-7"/></svg>;
    case "quote": return <svg {...p}><path d="M7 11V7a3 3 0 0 1 3-3M13 11V7a3 3 0 0 1 3-3M4 17v-6h6v6H4zM14 17v-6h6v6h-6z" fill="currentColor" fillOpacity=".3"/></svg>;
    case "pause": return <svg {...p} fill="currentColor" stroke="none"><rect x="6" y="5" width="4" height="14" rx="1"/><rect x="14" y="5" width="4" height="14" rx="1"/></svg>;
    case "sliders": return <svg {...p}><path d="M4 6h10M18 6h2M4 12h2M10 12h10M4 18h14M20 18h0"/><circle cx="16" cy="6" r="2"/><circle cx="8" cy="12" r="2"/><circle cx="18" cy="18" r="2" fill="currentColor"/></svg>;
    case "book": return <svg {...p}><path d="M4 19.5v-15A2.5 2.5 0 0 1 6.5 2H20v20H6.5a2.5 2.5 0 0 1 0-5H20"/></svg>;
    case "bot": return <svg {...p}><rect x="4" y="7" width="16" height="12" rx="3"/><path d="M12 2v5M8 13h.01M16 13h.01M9 17h6"/></svg>;
    case "info": return <svg {...p}><circle cx="12" cy="12" r="9"/><path d="M12 16v-5M12 8h.01"/></svg>;
    case "save": return <svg {...p}><path d="M19 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h11l5 5v11a2 2 0 0 1-2 2z"/><path d="M7 3v6h9V3M7 21v-7h10v7"/></svg>;
    case "globe": return <svg {...p}><circle cx="12" cy="12" r="9"/><path d="M3 12h18M12 3c3 3.5 3 14.5 0 18M12 3c-3 3.5-3 14.5 0 18"/></svg>;
    case "upload": return <svg {...p}><path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"/><path d="m17 8-5-5-5 5"/><path d="M12 3v13"/></svg>;
    case "link": return <svg {...p}><path d="M10 13a5 5 0 0 0 7.1 0l3-3a5 5 0 0 0-7.1-7.1l-1.7 1.7"/><path d="M14 11a5 5 0 0 0-7.1 0l-3 3a5 5 0 0 0 7.1 7.1l1.7-1.7"/></svg>;
    default: return <svg {...p}><circle cx="12" cy="12" r="9"/></svg>;
  }
};

// Placeholder portrait for NPC — mesh gradient + monogram, no fake photo
const NpcPortrait = ({name="Marek Kowalski", hue="default", size=360}) => {
  const initials = name.split(" ").map(s=>s[0]).slice(0,2).join("");
  return (
    <div style={{width:size,aspectRatio:"3/4",borderRadius:16,position:"relative",overflow:"hidden",boxShadow:"0 30px 60px -20px rgba(0,0,0,.6)"}}>
      <div className={`mesh ${hue}`} style={{position:"absolute",inset:0}}/>
      <div style={{position:"absolute",inset:0,display:"grid",placeItems:"center"}}>
        <div style={{
          fontFamily:"var(--serif)",fontSize:size*0.42,color:"#fff",
          textShadow:"0 4px 30px rgba(0,0,0,.35)",letterSpacing:"-.04em",
        }}>{initials}</div>
      </div>
      <div style={{position:"absolute",left:12,bottom:12,right:12,padding:"10px 12px",background:"rgba(10,10,16,.6)",backdropFilter:"blur(12px)",borderRadius:10,border:"1px solid rgba(255,255,255,.08)"}}>
        <div style={{fontFamily:"var(--mono)",fontSize:10,letterSpacing:".15em",textTransform:"uppercase",color:"var(--fg-3)"}}>NPC · wygenerowana postać</div>
        <div style={{fontFamily:"var(--serif)",fontSize:22,letterSpacing:"-.01em",marginTop:2}}>{name}</div>
      </div>
    </div>
  );
};

// Mesh thumbnail for scenario card
const ScenarioThumb = ({title, level, hue, eyebrow}) => (
  <div className="thumb">
    <div className={`mesh ${hue||""}`} style={{position:"absolute",inset:0}}/>
    <div className="meta">
      {eyebrow && <span className="chip mono" style={{background:"rgba(0,0,0,.4)",color:"#fff",backdropFilter:"blur(8px)"}}>{eyebrow}</span>}
      {level && <span className="chip mono" style={{background:"rgba(0,0,0,.4)",color:"#fff",backdropFilter:"blur(8px)"}}>{level}</span>}
    </div>
    <div className="label">{title}</div>
  </div>
);

// Turn beads indicator
const TurnBeads = ({total, current}) => (
  <div className="turns">
    <span>tura</span>
    <strong style={{color:"var(--fg)",fontFamily:"var(--serif)",fontSize:18}}>{current}</strong>
    <span>/ {total}</span>
    <div style={{display:"flex",gap:3,marginLeft:6}}>
      {Array.from({length:total}).map((_,i)=> (
        <div key={i} className={`bead ${i<current-1?"done":i===current-1?"cur":""}`} />
      ))}
    </div>
  </div>
);

// Score ring
const ScoreRing = ({value=74, label="ogólny wynik"}) => {
  const r = 72, c = 2*Math.PI*r;
  const dash = c * (value/100);
  return (
    <div className="score-ring">
      <svg width="160" height="160">
        <circle cx="80" cy="80" r={r} stroke="var(--surface-3)" strokeWidth="10" fill="none"/>
        <circle cx="80" cy="80" r={r} stroke="url(#grad)" strokeWidth="10" fill="none"
          strokeDasharray={`${dash} ${c}`} strokeLinecap="round" style={{transition:"stroke-dasharray 1s"}}/>
        <defs>
          <linearGradient id="grad" x1="0" y1="0" x2="1" y2="1">
            <stop offset="0" stopColor="oklch(0.72 0.18 295)"/>
            <stop offset="1" stopColor="oklch(0.82 0.12 295)"/>
          </linearGradient>
        </defs>
      </svg>
      <div className="v"><div>{value}<small>{label}</small></div></div>
    </div>
  );
};

// Learning curve — z osiami, gridlines, tooltipami per punkt
const Curve = ({data=[45,52,61,58,68,71,74,82], width=380, height=170, threshold=70}) => {
  // Fixed Y scale 0–100 (to % always czytelne), X = numer próby 1..N
  const padL = 34, padR = 12, padT = 14, padB = 24;
  const w = width - padL - padR, h = height - padT - padB;
  const yFor = (v) => padT + (1 - v/100)*h;
  const xFor = (i) => padL + (i/(data.length-1))*w;
  const pts = data.map((v,i)=>[xFor(i), yFor(v), v, i+1]);
  const line = pts.map((p,i)=> (i===0?"M":"L")+p[0].toFixed(1)+" "+p[1].toFixed(1)).join(" ");
  const area = line + ` L ${padL+w} ${padT+h} L ${padL} ${padT+h} Z`;
  const yTicks = [0, 25, 50, 75, 100];
  const trend = data[data.length-1] - data[0];
  return (
    <div style={{fontFamily:"var(--sans)"}}>
      <div className="flex between" style={{marginBottom:6,alignItems:"baseline"}}>
        <div style={{fontSize:11,fontFamily:"var(--mono)",letterSpacing:".04em",textTransform:"uppercase",color:"var(--fg-3)"}}>Krzywa uczenia</div>
        <div style={{fontSize:11,fontFamily:"var(--mono)",color:trend>=0?"var(--ok)":"var(--bad)"}}>
          {trend>=0?"▲":"▼"} {Math.abs(trend)} pkt · {data.length} prób
        </div>
      </div>
      <svg className="curve" width={width} height={height} viewBox={`0 0 ${width} ${height}`} style={{display:"block",width:"100%",height:"auto"}}>
        <defs>
          <linearGradient id="curveG" x1="0" y1="0" x2="0" y2="1">
            <stop offset="0" stopColor="oklch(0.72 0.18 295)" stopOpacity="0.35"/>
            <stop offset="1" stopColor="oklch(0.72 0.18 295)" stopOpacity="0"/>
          </linearGradient>
        </defs>
        {/* Y gridlines + labels */}
        {yTicks.map(t=>(
          <g key={t}>
            <line x1={padL} y1={yFor(t)} x2={padL+w} y2={yFor(t)} stroke="var(--line)" strokeDasharray={t===0?"":"2 3"} strokeWidth="1"/>
            <text x={padL-6} y={yFor(t)+3} textAnchor="end" fontSize="10" fontFamily="var(--mono)" fill="var(--fg-4)">{t}%</text>
          </g>
        ))}
        {/* Threshold */}
        <line x1={padL} y1={yFor(threshold)} x2={padL+w} y2={yFor(threshold)} stroke="var(--warn)" strokeWidth="1" strokeDasharray="3 3" opacity="0.7"/>
        <text x={padL+w-2} y={yFor(threshold)-3} textAnchor="end" fontSize="9" fontFamily="var(--mono)" fill="var(--warn)">próg {threshold}%</text>
        {/* Area + line */}
        <path d={area} fill="url(#curveG)"/>
        <path d={line} fill="none" stroke="oklch(0.82 0.12 295)" strokeWidth="2"/>
        {/* Points with tooltips */}
        {pts.map(([x,y,v,n],i)=>(
          <g key={i}>
            <circle cx={x} cy={y} r={i===pts.length-1?4:2.8} fill={i===pts.length-1?"var(--accent-3)":"var(--accent)"}/>
            <circle cx={x} cy={y} r="10" fill="transparent" style={{cursor:"help"}}>
              <title>Próba {n} · {v}% {v>=threshold?"· ✓ powyżej progu":""}</title>
            </circle>
          </g>
        ))}
        {/* X axis labels — pokazuj pierwszą, ostatnią i co 2 środkowe */}
        {pts.map(([x,,,n],i)=>{
          const show = i===0 || i===pts.length-1 || (data.length<=8 ? i%2===1 : i%Math.ceil(data.length/5)===0);
          return show ? <text key={i} x={x} y={height-padB+14} textAnchor="middle" fontSize="10" fontFamily="var(--mono)" fill="var(--fg-4)">{n}</text> : null;
        })}
        <text x={padL+w/2} y={height-2} textAnchor="middle" fontSize="10" fontFamily="var(--mono)" fill="var(--fg-4)" letterSpacing=".04em">numer próby</text>
      </svg>
    </div>
  );
};

Object.assign(window, { Icon, NpcPortrait, ScenarioThumb, TurnBeads, ScoreRing, Curve });
