// ui.jsx — shared UI atoms: Logo, Icons, CarPhoto, etc.

const Logo = ({ size = 28 }) => (
  <img
    src="/assets/logo.png"
    alt="OttoKlar"
    style={{ height: size, width: "auto", display: "block" }}
  />
);

// Iconography — single-stroke, 1.6px
const Ic = {
  arrow: (p) => (<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round" {...p}><path d="M5 12h14M13 6l6 6-6 6"/></svg>),
  arrowDown: (p) => (<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round" {...p}><path d="M12 5v14M6 13l6 6 6-6"/></svg>),
  check: (p) => (<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" {...p}><path d="M5 12.5l4.5 4.5L19 7"/></svg>),
  camera: (p) => (<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round" {...p}><path d="M3 8.5A2.5 2.5 0 015.5 6h2L9 4h6l1.5 2h2A2.5 2.5 0 0121 8.5v9A2.5 2.5 0 0118.5 20h-13A2.5 2.5 0 013 17.5v-9z"/><circle cx="12" cy="13" r="3.5"/></svg>),
  upload: (p) => (<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round" {...p}><path d="M12 16V4M7 9l5-5 5 5"/><path d="M5 20h14"/></svg>),
  pin: (p) => (<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round" {...p}><path d="M12 22s7-7.2 7-13a7 7 0 10-14 0c0 5.8 7 13 7 13z"/><circle cx="12" cy="9" r="2.5"/></svg>),
  star: (p) => (<svg viewBox="0 0 24 24" fill="currentColor" {...p}><path d="M12 2l2.9 6.6 7.1.7-5.4 4.8 1.7 7-6.3-3.7-6.3 3.7 1.7-7L2 9.3l7.1-.7L12 2z"/></svg>),
  shield: (p) => (<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round" {...p}><path d="M12 3l8 3v6c0 5-3.5 8.4-8 9-4.5-.6-8-4-8-9V6l8-3z"/><path d="M9 12l2 2 4-4"/></svg>),
  bolt: (p) => (<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round" {...p}><path d="M13 3L4 14h7l-1 7 9-11h-7l1-7z"/></svg>),
  euro: (p) => (<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round" {...p}><path d="M18 6.5A7 7 0 008 8a7 7 0 000 8 7 7 0 0010 1.5"/><path d="M4 10h9M4 14h8"/></svg>),
  wrench: (p) => (<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round" {...p}><path d="M15 4a4.5 4.5 0 014.5 5.5L21 11l-2 2-1.5-1.5A4.5 4.5 0 0112 9l-7 7 2 2 7-7"/></svg>),
  wrench2: (p) => (<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round" {...p}><path d="M14.7 6.3a1 1 0 000 1.4l1.6 1.6a1 1 0 001.4 0l3.77-3.77a6 6 0 01-7.94 7.94l-6.91 6.91a2.12 2.12 0 01-3-3l6.91-6.91a6 6 0 017.94-7.94l-3.76 3.76z"/></svg>),
  tools: (p) => (<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.4" strokeLinecap="round" strokeLinejoin="round" {...p}><g transform="translate(12 12) scale(0.8) translate(-12 -12)"><path d="M14.7 6.3a1 1 0 000 1.4l1.6 1.6a1 1 0 001.4 0l3.77-3.77a6 6 0 01-7.94 7.94l-6.91 6.91a2.12 2.12 0 01-3-3l6.91-6.91a6 6 0 017.94-7.94l-3.76 3.76z"/></g><g transform="translate(24 0) scale(-1 1)"><g transform="translate(12 12) scale(0.8) translate(-12 -12)"><path d="m15 12-8.373 8.373a1 1 0 11-3-3L12 9"/><path d="m18 15 4-4"/><path d="m21.5 11.5-1.914-1.914A2 2 0 0119 8.172V7l-2.26-2.26a6 6 0 00-4.202-1.756L9 2.96l.92.82A6.18 6.18 0 0112 8.4V10l2 2h1.172a2 2 0 011.414.586L18.5 14.5"/></g></g></svg>),
  clock: (p) => (<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round" {...p}><circle cx="12" cy="12" r="9"/><path d="M12 7v5l3 2"/></svg>),
  sparkle: (p) => (<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round" {...p}><path d="M12 3l1.7 5.3L19 10l-5.3 1.7L12 17l-1.7-5.3L5 10l5.3-1.7L12 3zM19 16l.8 2.2 2.2.8-2.2.8-.8 2.2-.8-2.2L16 19l2.2-.8L19 16z"/></svg>),
  x: (p) => (<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round" {...p}><path d="M6 6l12 12M18 6L6 18"/></svg>),
  more: (p) => (<svg viewBox="0 0 24 24" fill="currentColor" {...p}><circle cx="6" cy="12" r="1.5"/><circle cx="12" cy="12" r="1.5"/><circle cx="18" cy="12" r="1.5"/></svg>),
  phone: (p) => (<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round" {...p}><path d="M5 4h4l1.5 4-2 1.5a11 11 0 006 6L16 13.5l4 1.5v4a2 2 0 01-2 2A16 16 0 013 6a2 2 0 012-2z"/></svg>),
  calendar: (p) => (<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round" {...p}><rect x="3.5" y="5.5" width="17" height="15" rx="2"/><path d="M8 3v4M16 3v4M3.5 10h17"/></svg>),
  chevron: (p) => (<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" {...p}><path d="M9 6l6 6-6 6"/></svg>),
};

// Header bar — used on landing AND app screens
const Header = ({ onLaunch, view, onHome, onPartnerLogin }) => (
  <header className="hdr">
    <div className="wrap hdr-row">
      <a href="#" onClick={(e) => { e.preventDefault(); onHome && onHome(); }} aria-label="OttoKlar home">
        <Logo />
      </a>
      <div style={{ display: "flex", gap: 8, alignItems: "center" }}>
        <button
          className="btn btn-sm"
          onClick={() => onPartnerLogin && onPartnerLogin()}
          style={{ background: "rgba(250,250,247,.78)", border: "1px solid var(--rule)", color: "var(--mute)" }}>
          {t("Header.partnerLogin")}
        </button>
      </div>
    </div>
  </header>
);

// CarPhoto — synthetic damaged-car "photo" with severity-aware annotations.
// Severity: 'minor' | 'moderate' | 'severe'
const CarPhoto = ({ severity = "moderate", showOverlay = true, scanning = false, theme }) => {
  const accent = "#10A171";
  // Damage configurations per severity
  const damages = {
    minor: [
      { x: 56, y: 64, w: 18, h: 9, label: t("CarPhoto.damageMinorBumper"), conf: "97%" },
    ],
    moderate: [
      { x: 30, y: 56, w: 22, h: 14, label: t("CarPhoto.damageModerateDoor"), conf: "96%" },
      { x: 58, y: 64, w: 16, h: 8, label: t("CarPhoto.damageModeratePaint"), conf: "92%" },
    ],
    severe: [
      { x: 28, y: 50, w: 28, h: 22, label: t("CarPhoto.damageSeverePanel"), conf: "98%" },
      { x: 56, y: 62, w: 22, h: 13, label: t("CarPhoto.damageSevereBumper"), conf: "97%" },
      { x: 12, y: 42, w: 14, h: 12, label: t("CarPhoto.damageSevereHeadlight"), conf: "89%" },
    ],
  }[severity];

  return (
    <div style={{
      position: "relative", width: "100%", aspectRatio: "4 / 3",
      borderRadius: 12, overflow: "hidden",
      background: "linear-gradient(180deg, #d7dbe1 0%, #c2c7cf 60%, #aeb3bd 100%)",
      boxShadow: "inset 0 0 0 1px rgba(0,0,0,.06)",
    }}>
      {/* Asphalt ground */}
      <div style={{
        position: "absolute", inset: "auto 0 0 0", height: "32%",
        background: "linear-gradient(180deg, #6c6f76 0%, #4a4d53 100%)",
      }} />
      {/* Car body — stylized 3/4 view of rear-door + bumper */}
      <svg viewBox="0 0 400 300" style={{ position: "absolute", inset: 0, width: "100%", height: "100%" }}>
        <defs>
          <linearGradient id="bodyGrad" x1="0" y1="0" x2="0" y2="1">
            <stop offset="0" stopColor="#1a3550" />
            <stop offset="0.5" stopColor="#0e2236" />
            <stop offset="1" stopColor="#0a1828" />
          </linearGradient>
          <linearGradient id="windowGrad" x1="0" y1="0" x2="0" y2="1">
            <stop offset="0" stopColor="#3b4a5e" />
            <stop offset="1" stopColor="#1a2433" />
          </linearGradient>
        </defs>
        {/* Main body */}
        <path d="M20,200 Q40,110 130,95 L320,95 Q370,100 385,160 L385,220 L370,235 L40,235 L20,220 Z" fill="url(#bodyGrad)" />
        {/* Window strip */}
        <path d="M70,150 Q90,115 140,110 L310,110 Q345,118 355,160 L70,160 Z" fill="url(#windowGrad)" opacity="0.85" />
        {/* Door split lines */}
        <line x1="155" y1="100" x2="155" y2="230" stroke="rgba(0,0,0,.4)" strokeWidth="1.2" />
        <line x1="265" y1="95" x2="265" y2="232" stroke="rgba(0,0,0,.4)" strokeWidth="1.2" />
        {/* Door handles */}
        <rect x="180" y="172" width="22" height="5" rx="2" fill="rgba(0,0,0,.5)" />
        <rect x="290" y="172" width="22" height="5" rx="2" fill="rgba(0,0,0,.5)" />
        {/* Wheel arches */}
        <ellipse cx="100" cy="230" rx="48" ry="28" fill="#0a1828" />
        <ellipse cx="330" cy="230" rx="48" ry="28" fill="#0a1828" />
        {/* Wheels */}
        <circle cx="100" cy="232" r="26" fill="#1a1d22" />
        <circle cx="100" cy="232" r="14" fill="#3a3d44" />
        <circle cx="330" cy="232" r="26" fill="#1a1d22" />
        <circle cx="330" cy="232" r="14" fill="#3a3d44" />
        {/* Highlight on body */}
        <path d="M70,170 Q200,150 360,168 L360,178 Q200,160 70,180 Z" fill="rgba(255,255,255,0.08)" />

        {/* DAMAGE marks — drawn on the body underneath the AI overlay */}
        {severity === "minor" && (
          <>
            <path d="M230,200 Q245,196 260,202 Q255,205 240,206 Z" fill="rgba(180,150,120,.55)" />
            <line x1="232" y1="205" x2="262" y2="203" stroke="rgba(160,120,80,.7)" strokeWidth="1.2" />
          </>
        )}
        {severity === "moderate" && (
          <>
            {/* Door dent */}
            <ellipse cx="195" cy="180" rx="32" ry="14" fill="rgba(0,0,0,.35)" />
            <ellipse cx="195" cy="178" rx="22" ry="9" fill="rgba(0,0,0,.4)" />
            <path d="M170,178 Q195,170 220,178" stroke="rgba(255,255,255,.18)" strokeWidth="1" fill="none" />
            {/* Paint chip on bumper */}
            <path d="M250,205 Q265,200 280,206 L278,212 L252,212 Z" fill="rgba(200,170,140,.7)" />
            <circle cx="265" cy="208" r="2" fill="rgba(120,90,60,.9)" />
          </>
        )}
        {severity === "severe" && (
          <>
            {/* Big panel deformation */}
            <ellipse cx="190" cy="175" rx="44" ry="22" fill="rgba(0,0,0,.45)" />
            <ellipse cx="190" cy="173" rx="32" ry="14" fill="rgba(0,0,0,.55)" />
            <path d="M160,175 Q190,160 220,176" stroke="rgba(255,255,255,.22)" strokeWidth="1.2" fill="none" />
            <path d="M165,185 Q190,196 220,185" stroke="rgba(255,255,255,.12)" strokeWidth="1" fill="none" />
            {/* Cracked bumper */}
            <path d="M240,200 L260,196 L275,204 L290,200" stroke="rgba(0,0,0,.7)" strokeWidth="2.2" fill="none" />
            <path d="M248,210 L268,205 L285,210" stroke="rgba(180,150,120,.6)" strokeWidth="3" fill="none" />
            {/* Headlight crack */}
            <path d="M55,150 L75,158 L70,168" stroke="rgba(255,255,255,.4)" strokeWidth="1.4" fill="none" />
          </>
        )}
      </svg>

      {/* Scanning line overlay */}
      {scanning && (
        <>
          <div style={{
            position: "absolute", left: 0, right: 0, top: 0, height: "30%",
            background: `linear-gradient(180deg, ${accent}00 0%, ${accent}22 50%, ${accent}55 95%, ${accent}00 100%)`,
            animation: "scanLine 1.6s ease-in-out infinite alternate",
            mixBlendMode: "screen",
          }} />
          <div style={{
            position: "absolute", left: 12, top: 12,
            color: "#fff", fontSize: 11, fontFamily: "var(--mono)", letterSpacing: ".06em",
            background: "rgba(0,0,0,.55)", padding: "4px 8px", borderRadius: 6,
            display: "flex", alignItems: "center", gap: 6,
          }}>
            <span className="dot" style={{ background: accent, animation: "pulseSoft 1s infinite" }} /> {t("CarPhoto.scanning")}
          </div>
        </>
      )}

      {/* AI overlay — bounding boxes & labels */}
      {showOverlay && !scanning && damages.map((d, i) => (
        <div key={i} style={{
          position: "absolute",
          left: `${d.x}%`, top: `${d.y}%`,
          width: `${d.w}%`, height: `${d.h}%`,
          border: `1.5px solid ${accent}`,
          borderRadius: 4,
          boxShadow: `0 0 0 1px rgba(0,0,0,.25), 0 0 18px ${accent}55`,
          animation: `fadeUp .5s ease ${i * 0.15}s both`,
        }}>
          <div style={{
            position: "absolute", left: -1.5, top: -22,
            background: accent, color: "#fff",
            padding: "3px 7px", borderRadius: "4px 4px 4px 0",
            fontSize: 10.5, fontFamily: "var(--mono)", letterSpacing: ".02em",
            whiteSpace: "nowrap",
          }}>
            {d.label} · {d.conf}
          </div>
        </div>
      ))}

      {/* Camera HUD corners */}
      {showOverlay && [["tl",10,10],["tr",10,"auto"],["bl","auto",10],["br","auto","auto"]].map(([pos, top, left], i) => {
        const corner = pos;
        const style = { position: "absolute", width: 14, height: 14, opacity: 0.65 };
        if (corner === "tl") Object.assign(style, { top: 10, left: 10, borderTop: "1.5px solid #fff", borderLeft: "1.5px solid #fff" });
        if (corner === "tr") Object.assign(style, { top: 10, right: 10, borderTop: "1.5px solid #fff", borderRight: "1.5px solid #fff" });
        if (corner === "bl") Object.assign(style, { bottom: 10, left: 10, borderBottom: "1.5px solid #fff", borderLeft: "1.5px solid #fff" });
        if (corner === "br") Object.assign(style, { bottom: 10, right: 10, borderBottom: "1.5px solid #fff", borderRight: "1.5px solid #fff" });
        return <div key={i} style={style} />;
      })}
    </div>
  );
};

// Step number ring
const StepNum = ({ n }) => (
  <div style={{
    width: 32, height: 32, borderRadius: 50, display: "grid", placeItems: "center",
    background: "var(--ink)", color: "#fff", fontSize: 13, fontFamily: "var(--mono)",
    fontWeight: 500,
  }}>{String(n).padStart(2, "0")}</div>
);

// Avatar bubble
const Avatar = ({ initials, color = "#10A171" }) => (
  <div style={{
    width: 28, height: 28, borderRadius: 50, background: color, color: "#fff",
    display: "grid", placeItems: "center", fontSize: 11, fontWeight: 600,
    letterSpacing: ".02em",
  }}>{initials}</div>
);

Object.assign(window, { Logo, Ic, Header, CarPhoto, StepNum, Avatar });
