// DRIVR — DA scorecard, dark theme. Phone reproduction of the light-app scorecard.
// Driving Safety & Delivery Quality sections with green→yellow→red heat bars.

function Tri() {
  return (
    <svg width="22" height="11" viewBox="0 0 22 11" style={{ display: "block" }} aria-hidden="true">
      <path d="M11 0 L18 9 L4 9 Z" fill="var(--paper-3)" />
    </svg>
  );
}

// One metric row. heat: "" | "good" | "bad" | "none". w = fill width %.
function Row({ label, sub, value, unit, heat = "none", w = 46 }) {
  const hasHeat = heat !== "none";
  return (
    <div className={"metric-row" + (hasHeat ? " has-heat" : "")}>
      {hasHeat && (
        <span
          className="heat-fill"
          data-kind={heat === "good" || heat === "bad" ? heat : ""}
          style={{ width: w + "%" }}
        />
      )}
      <span className={sub ? "m-sub" : "m-label"}>{label}</span>
      <span className="m-val">
        {value}
        {unit && <span style={{ color: hasHeat ? "rgba(11,12,14,0.55)" : "var(--paper-3)", fontWeight: 500 }}>{unit}</span>}
      </span>
    </div>
  );
}

function EventsLabel() {
  return (
    <div style={{ display: "flex", alignItems: "baseline", gap: 7, padding: "14px 12px 4px" }}>
      <span style={{ fontFamily: "var(--font-body)", fontWeight: 700, fontSize: 11, letterSpacing: "0.1em", textTransform: "uppercase", color: "var(--paper-3)" }}>Events</span>
      <span style={{ fontFamily: "var(--font-body)", fontSize: 12, color: "var(--paper-4)" }}>per 100 deliveries</span>
    </div>
  );
}

// Podium + star — "standings / rank".
// ── Builder modules (slide 5: "build what nobody sells") ──────────────
// Operator-built tools woven into the scorecard. Coded blue (--accent) to
// read as "yours", vs the teal of standard DRIVR scorecard data.
function ToolHeader({ title, icon }) {
  return (
    <div style={{ display: "flex", alignItems: "center", gap: 10, padding: "10px 15px", background: "var(--accent)" }}>
      {icon && <Icon name={icon} size={17} color="#fff" />}
      <span style={{ fontFamily: "var(--font-display)", fontWeight: 800, fontSize: 16, letterSpacing: "-0.01em", color: "#fff" }}>{title}</span>
      <span style={{ marginLeft: "auto", fontFamily: "var(--font-body)", fontWeight: 700, fontSize: 9.5, letterSpacing: "0.13em", textTransform: "uppercase", color: "rgba(255,255,255,0.92)" }}>Your tool</span>
    </div>
  );
}

function BuilderCard({ title, icon, children }) {
  return (
    <div style={{ borderRadius: "var(--r-md)", overflow: "hidden", background: "var(--ink-800)", border: "1px solid var(--accent-dim)", boxShadow: "0 0 0 1px rgba(61,123,236,0.10)" }}>
      <ToolHeader title={title} icon={icon} />
      <div style={{ padding: "15px 16px 17px" }}>{children}</div>
    </div>
  );
}

function Track({ pct, color = "var(--accent-bright)", marks = [] }) {
  return (
    <div style={{ position: "relative", height: 9, borderRadius: 6, background: "var(--ink-700)" }}>
      <div style={{ position: "absolute", inset: 0, borderRadius: 6, overflow: "hidden" }}>
        <div style={{ height: "100%", width: pct + "%", background: color, borderRadius: 6 }} />
      </div>
      {marks.map((m, i) => (
        <span key={i} style={{ position: "absolute", left: m + "%", top: -2, width: 2, height: 13, background: "var(--ink-900)", transform: "translateX(-1px)" }} />
      ))}
    </div>
  );
}

function SafetyStreak() {
  return (
    <BuilderCard title="Safety Streak" icon="flag">
      <div style={{ display: "flex", alignItems: "baseline", gap: 9, marginBottom: 14 }}>
        <span style={{ fontFamily: "var(--font-mono)", fontWeight: 600, fontSize: 40, lineHeight: 1, color: "var(--paper)", letterSpacing: "-0.02em" }}>34</span>
        <span style={{ fontFamily: "var(--font-body)", fontWeight: 600, fontSize: 14, color: "var(--paper-3)" }}>clean days in a row</span>
        <span style={{ marginLeft: "auto", fontFamily: "var(--font-body)", fontWeight: 700, fontSize: 11, letterSpacing: "0.08em", textTransform: "uppercase", color: "var(--ok)" }}>On streak</span>
      </div>
      <Track pct={57} marks={[50, 100]} />
      <div style={{ display: "flex", justifyContent: "space-between", marginTop: 7, fontFamily: "var(--font-mono)", fontSize: 11, color: "var(--paper-4)" }}>
        <span>0</span><span style={{ color: "var(--ok)" }}>30 ✓</span><span>60</span>
      </div>
      <div style={{ marginTop: 13, display: "flex", flexDirection: "column", gap: 8 }}>
        <div style={{ display: "flex", alignItems: "center", gap: 9, fontFamily: "var(--font-body)", fontSize: 13.5, color: "var(--paper-2)" }}>
          <span style={{ width: 18, height: 18, borderRadius: 999, background: "var(--ok-dim)", color: "var(--ok)", display: "flex", alignItems: "center", justifyContent: "center", flex: "none" }}><Icon name="check" size={11} /></span>
          <span><b style={{ color: "var(--paper)", fontFamily: "var(--font-mono)", fontWeight: 700 }}>$5</b> gift card texted at day 30 — sent.</span>
        </div>
        <div style={{ display: "flex", alignItems: "center", gap: 9, fontFamily: "var(--font-body)", fontSize: 13.5, color: "var(--paper-3)" }}>
          <span style={{ width: 18, height: 18, borderRadius: 999, background: "var(--accent-ghost)", color: "var(--accent-bright)", display: "flex", alignItems: "center", justifyContent: "center", flex: "none" }}><Icon name="chevron-right" size={12} /></span>
          <span>Next: <b style={{ color: "var(--paper)", fontFamily: "var(--font-mono)", fontWeight: 700 }}>$10</b> at 60 days · 26 to go.</span>
        </div>
      </div>
    </BuilderCard>
  );
}

function PodBonus() {
  const drivers = [1, 1, 1, 1, 1, 1, 0, 0]; // 6 of 8 cleared
  return (
    <BuilderCard title="Team POD Bonus" icon="team">
      <div style={{ display: "flex", alignItems: "baseline", gap: 9, marginBottom: 12 }}>
        <span style={{ fontFamily: "var(--font-mono)", fontWeight: 600, fontSize: 40, lineHeight: 1, color: "var(--green-bright)", letterSpacing: "-0.02em" }}>$18</span>
        <span style={{ fontFamily: "var(--font-body)", fontWeight: 600, fontSize: 14, color: "var(--paper-3)" }}>your bonus this week</span>
      </div>
      <div style={{ fontFamily: "var(--font-body)", fontSize: 13.5, lineHeight: 1.45, color: "var(--paper-2)", marginBottom: 14 }}>
        Your <b style={{ color: "var(--paper)", fontFamily: "var(--font-mono)", fontWeight: 700 }}>99.3%</b> POD acceptance cleared the bar. The pool pays off <b style={{ color: "var(--paper)" }}>everyone's</b> POD numbers.
      </div>
      <div style={{ display: "flex", alignItems: "center", justifyContent: "space-between", marginBottom: 8 }}>
        <span style={{ fontFamily: "var(--font-body)", fontWeight: 700, fontSize: 10.5, letterSpacing: "0.1em", textTransform: "uppercase", color: "var(--paper-3)" }}>Team above 99% POD</span>
        <span style={{ fontFamily: "var(--font-mono)", fontWeight: 700, fontSize: 13, color: "var(--green-bright)" }}>6 / 8</span>
      </div>
      <div style={{ display: "flex", gap: 5 }}>
        {drivers.map((d, i) => (
          <span key={i} style={{ flex: 1, height: 8, borderRadius: 3, background: d ? "var(--green)" : "var(--ink-700)" }} />
        ))}
      </div>
      <div style={{ marginTop: 11, fontFamily: "var(--font-body)", fontSize: 12.5, color: "var(--paper-4)" }}>
        Hit <b style={{ color: "var(--paper-2)", fontFamily: "var(--font-mono)" }}>8 / 8</b> and the pool doubles next week.
      </div>
    </BuilderCard>
  );
}

function Wheel({ size = 17, color = "#fff" }) {
  return (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke={color} strokeWidth={2} aria-hidden="true" style={{ flex: "none" }}>
      <circle cx="12" cy="12" r="9" />
      <circle cx="12" cy="12" r="2.6" />
      <path d="M12 9.4V3.1M9.7 13.4 4.7 16.8M14.3 13.4 19.3 16.8" strokeLinecap="round" />
    </svg>
  );
}

function Section({ title, icon, iconNode, headerColor, children }) {
  return (
    <div className="surface" style={{ overflow: "hidden" }}>
      <div style={{ display: "flex", alignItems: "center", gap: 10, padding: "11px 15px", background: headerColor }}>
        {iconNode || <Icon name={icon} size={17} color="#fff" />}
        <span style={{ fontFamily: "var(--font-display)", fontWeight: 800, fontSize: 16, letterSpacing: "-0.01em", color: "#fff" }}>{title}</span>
      </div>
      <div style={{ padding: "7px 6px 9px" }}>{children}</div>
    </div>
  );
}

function Scorecard({ t }) {
  const head = t.headerColor;
  const d = window.VDATA || { name: "Marcus Reyes", id: "A1DRVRREYES0427" };
  const initials = d.name.split(" ").map((w) => w[0]).join("").slice(0, 2);
  return (
    <div style={{ display: "flex", flexDirection: "column", gap: 16, padding: "18px 14px 30px", background: "var(--ink-900)", minHeight: "100%" }}>

      {/* Driver identity */}
      <div style={{ display: "flex", alignItems: "center", gap: 12, paddingBottom: 4, borderBottom: "1px solid var(--line)" }}>
        <div style={{ width: 42, height: 42, borderRadius: 999, background: "var(--teal)", color: "#04222c", display: "flex", alignItems: "center", justifyContent: "center", fontFamily: "var(--font-display)", fontWeight: 800, fontSize: 17, flex: "none" }}>{initials}</div>
        <div style={{ minWidth: 0, marginBottom: 4 }}>
          <div style={{ fontFamily: "var(--font-display)", fontWeight: 800, fontSize: 18, letterSpacing: "-0.01em", color: "var(--paper)", lineHeight: 1.1 }}>{d.name}</div>
          <div style={{ fontFamily: "var(--font-mono)", fontSize: 11, color: "var(--paper-3)", marginTop: 2, whiteSpace: "nowrap" }}>{d.id} · DSPR @ DXC2</div>
        </div>
      </div>

      {/* Week header */}
      <div style={{ textAlign: "center", display: "flex", flexDirection: "column", alignItems: "center", gap: 12 }}>
        <div style={{ fontFamily: "var(--font-display)", fontWeight: 900, fontSize: 42, letterSpacing: "-0.025em", color: "var(--paper)", lineHeight: 1 }}>Week 28</div>
        <div style={{ display: "flex", alignItems: "center", gap: 0, width: "100%" }}>
          <span style={{ flex: 1, height: 1, background: "var(--line)" }} />
          <Tri />
          <span style={{ flex: 1, height: 1, background: "var(--line)" }} />
        </div>
        <div style={{ fontFamily: "var(--font-body)", fontSize: 15, color: "var(--paper-3)", whiteSpace: "nowrap" }}>
          Deliveries: <span style={{ fontFamily: "var(--font-mono)", fontWeight: 600, color: "var(--paper)" }}>1,254</span>
        </div>
      </div>

      {/* Tier + rank */}
      <div style={{ display: "flex", gap: 12 }}>
        {[
          { icon: "star", big: "Fantastic", small: "Overall tier", mono: false },
          { icon: "chart", big: <span style={{ whiteSpace: "nowrap" }}><span style={{ fontSize: 28 }}>12</span><span style={{ fontSize: 15, fontWeight: 500 }}>th</span> <span style={{ fontSize: 16, color: "rgba(255,255,255,0.7)" }}>/ 55</span></span>, small: "Rank", mono: true },
        ].map((c, i) => (
          <div key={i} style={{ flex: 1, background: head, borderRadius: "var(--r-md)", padding: "20px 14px 18px", display: "flex", flexDirection: "column", alignItems: "center", gap: 10, boxShadow: "0 1px 0 rgba(255,255,255,0.08) inset" }}>
            <Icon name={c.icon} size={28} color="#fff" />
            <span style={{ fontFamily: c.mono ? "var(--font-mono)" : "var(--font-display)", fontWeight: c.mono ? 600 : 800, fontSize: 26, letterSpacing: "-0.01em", color: "#fff", lineHeight: 1 }}>{c.big}</span>
            <span style={{ fontFamily: "var(--font-body)", fontSize: 13, color: "rgba(255,255,255,0.82)", whiteSpace: "nowrap" }}>{c.small}</span>
          </div>
        ))}
      </div>

      {/* Driving Safety */}
      <Section title="Driving Safety" iconNode={<Wheel />} headerColor={head}>
        <Row label="FICO Score" value="670" unit=" / 850" heat="" w={74} />
        <Row label="Proper-Park-Sequence Compliance" value="71.7" unit="%" />
        <Row label="Did Not Apply Parking Brake" sub value="60" unit=" / 251" />
        <Row label="Did Not Shift Gear to Park" sub value="11" unit=" / 251" />
        <EventsLabel />
        <Row label="Distractions" value="0.1" />
        <Row label="Speeding" value="0.8" heat="bad" w={66} />
        <Row label="Seatbelt Off" value="0.2" heat="good" w={34} />
        <Row label="Follow Distance" value="0.3" heat="" w={46} />
        <Row label="Sign/Signal Violations" value="0.1" />
      </Section>

      {t.builder && <SafetyStreak />}

      {/* Delivery Quality */}
      <Section title="Delivery Quality" icon="check" headerColor={head}>
        <Row label="Completion Rate" value="99.5" unit="%" />
        <Row label="Delivered, Not Received" value="2" unit=" / 1254" heat="" w={40} />
        <Row label="Photo-On-Delivery Acceptance" value="99.3" unit="%" />
        <Row label="Photo-On-Delivery Rejects" value="4" unit=" / 561" />
        <Row label="Blurry" sub value="2" />
        <Row label="Human in Photo" sub value="1" />
        <Row label="No Package Detected" sub value="1" />
        <Row label="Pickup Success Behaviors" value="500" heat="" w={70} />
        <Row label="Contact Compliance" value="83.3" unit="%" heat="good" w={62} />
        <Row label="Compliance Failures" sub value="1" unit=" / 12" />
      </Section>

      {t.builder && <PodBonus />}

      <div style={{ textAlign: "center", fontFamily: "var(--font-mono)", fontSize: 11, color: "var(--paper-4)", letterSpacing: "0.02em", paddingTop: 2 }}>
        DSPR @ DXC2 · Generated 12/30/24
      </div>
    </div>
  );
}

Object.assign(window, { Scorecard });
