/* global React, Icon, Button, Chip, ScreenHeader, Panel, SectionLabel, AgentNote, FitBadge, TONES, TRACKER_STAGES, TRACKER_JOBS, TRACKER_INSIGHTS, NET_PROBES */
const { useState: useTrk } = React;

function ScrTracker({ onPrep, onPaths }) {
  const [view, setView] = useTrk("board");
  return (
    <div style={{ maxWidth: 1180, margin: "0 auto", padding: "8px 0 40px" }}>
      <ScreenHeader
        eyebrow="Track"
        title="Job tracker"
        lede="Every application, its stage, and what's next. Offerroom logs each one automatically and asks you to confirm it actually went out."
        actions={<div style={{ display: "inline-flex", background: "var(--paper-sunken)", border: "1px solid var(--ink-100)", borderRadius: 8, padding: 3, gap: 2 }}>
          {[["board", "grid"], ["list", "list"]].map(([k, ic]) => (
            <button key={k} onClick={() => setView(k)} style={{ width: 34, height: 30, borderRadius: 6, border: "none", cursor: "pointer", display: "grid", placeItems: "center",
              background: view === k ? "#fff" : "transparent", color: view === k ? "var(--fg-1)" : "var(--fg-2)", boxShadow: view === k ? "0 1px 2px rgba(31,27,22,0.06)" : "none" }}>
              <Icon name={ic} size={15}/>
            </button>
          ))}
        </div>}
      />

      {/* Insights */}
      <Panel pad={0} style={{ marginBottom: 16 }}>
        <div style={{ display: "flex", alignItems: "stretch" }}>
          {TRACKER_INSIGHTS.map((s, i) => (
            <div key={i} style={{ flex: 1, padding: "16px 20px", borderRight: i < TRACKER_INSIGHTS.length - 1 ? "1px solid var(--ink-50)" : "none" }}>
              <div style={{ fontFamily: "var(--font-display)", fontSize: 28, fontWeight: 500, letterSpacing: "-0.015em", color: s.tone, lineHeight: 1 }}>{s.n}</div>
              <div style={{ fontSize: 11, color: "var(--fg-2)", letterSpacing: "0.04em", textTransform: "uppercase", marginTop: 5 }}>{s.label}</div>
            </div>
          ))}
        </div>
        <div style={{ padding: "11px 20px", borderTop: "1px solid var(--ink-100)", display: "flex", alignItems: "center", gap: 9, fontSize: 12.5, color: "var(--fg-2)" }}>
          <Icon name="barChart" size={14} style={{ color: "var(--cardinal)" }}/>
          <span><b style={{ color: "var(--fg-1)", fontWeight: 600 }}>This week:</b> your reply rate climbed 9 points. The roles where you led with a number got 3x the responses, I'm weighting your tailoring that way.</span>
        </div>
      </Panel>

      {view === "board" ? <Board onPrep={onPrep} onPaths={onPaths}/> : <ListView onPrep={onPrep} onPaths={onPaths}/>}
    </div>
  );
}

function Board({ onPrep, onPaths }) {
  return (
    <div style={{ display: "grid", gridTemplateColumns: `repeat(${TRACKER_STAGES.length}, 1fr)`, gap: 12, overflowX: "auto", paddingBottom: 4,
      background: "radial-gradient(circle at 1px 1px, rgba(31,27,22,0.08) 1px, transparent 0) 0 0 / 24px 24px", borderRadius: 12, padding: 14 }}>
      {TRACKER_STAGES.map(stage => {
        const jobs = TRACKER_JOBS.filter(j => j.stage === stage);
        return (
          <div key={stage} style={{ minWidth: 168 }}>
            <div style={{ display: "flex", alignItems: "center", justifyContent: "space-between", padding: "2px 4px 10px" }}>
              <span style={{ fontSize: 11.5, fontWeight: 600, letterSpacing: "0.04em", textTransform: "uppercase", color: "var(--fg-2)" }}>{stage}</span>
              <span style={{ fontSize: 11, color: "var(--fg-3)", fontFamily: "var(--font-mono)" }}>{jobs.length}</span>
            </div>
            <div style={{ display: "flex", flexDirection: "column", gap: 8 }}>
              {jobs.map(j => <BoardCard key={j.id} j={j} onPrep={onPrep} onPaths={onPaths}/>)}
              {jobs.length === 0 && <div style={{ height: 60, borderRadius: 10, border: "1.5px dashed var(--ink-100)" }}/>}
            </div>
          </div>
        );
      })}
    </div>
  );
}

const SRC_ICON = { "Pasted JD": "fileText", "Internal board": "grid", "Chrome extension": "puzzle", "Referral": "contacts" };

const BoardCard = ({ j, onPrep, onPaths }) => {
  const t = TONES[j.tone] || TONES.neutral;
  const probe = NET_PROBES[j.co];
  return (
    <div style={{ background: "#fff", border: "1px solid rgba(31,27,22,0.10)", borderRadius: 10, padding: "11px 12px", boxShadow: "0 1px 2px rgba(31,27,22,0.04)", position: "relative", cursor: "grab" }}>
      <span style={{ position: "absolute", left: 0, top: 11, bottom: 11, width: 2, background: t.color, borderRadius: 2 }}/>
      <div style={{ display: "flex", justifyContent: "space-between", alignItems: "flex-start", gap: 6 }}>
        <div style={{ fontSize: 13, fontWeight: 600, lineHeight: 1.25 }}>{j.title}</div>
        <span style={{ fontSize: 10.5, color: "var(--fg-2)", fontFamily: "var(--font-mono)", flexShrink: 0 }}>{j.fit.toFixed(2)}</span>
      </div>
      <div style={{ fontSize: 11.5, color: "var(--fg-2)", marginTop: 2 }}>{j.co}</div>
      <div style={{ fontSize: 11, color: "var(--fg-1)", marginTop: 8, display: "flex", alignItems: "center", gap: 5 }}>
        <Icon name="clock" size={11} style={{ color: "var(--fg-3)" }}/>{j.next}
      </div>
      <div style={{ display: "flex", alignItems: "center", justifyContent: "space-between", marginTop: 9 }}>
        <span style={{ display: "inline-flex", alignItems: "center", gap: 4, fontSize: 10, color: "var(--fg-3)" }}>
          <Icon name={SRC_ICON[j.source] || "fileText"} size={11}/>{j.source}
        </span>
        <div style={{ display: "flex", alignItems: "center", gap: 6 }}>
          {probe && probe.hasPaths && <button onClick={(e) => { e.stopPropagation(); onPaths && onPaths(j.co); }} title={`${probe.count} warm paths into ${j.co}`} style={{ display: "inline-flex", alignItems: "center", gap: 4, fontSize: 11, fontWeight: 600, color: "var(--slate-text)", background: "var(--slate-tint)", border: "none", borderRadius: 999, padding: "3px 8px", cursor: "pointer" }}><Icon name="route" size={11}/>{probe.count}</button>}
          {j.prep && <button onClick={onPrep} style={{ display: "inline-flex", alignItems: "center", gap: 4, fontSize: 11, fontWeight: 600, color: "var(--cardinal)", background: "var(--cardinal-50)", border: "none", borderRadius: 999, padding: "3px 9px", cursor: "pointer" }}><Icon name="graduation" size={11}/>Prep</button>}
        </div>
      </div>
    </div>
  );
};

function ListView({ onPrep, onPaths }) {
  return (
    <Panel pad={0}>
      <div style={{ display: "grid", gridTemplateColumns: "1.4fr 110px 1fr 110px 90px", padding: "11px 18px", borderBottom: "1px solid var(--ink-100)", fontSize: 11, fontWeight: 600, letterSpacing: "0.04em", textTransform: "uppercase", color: "var(--fg-2)" }}>
        <span>Role</span><span>Stage</span><span>Next step</span><span>Source</span><span style={{ textAlign: "right" }}>Fit</span>
      </div>
      {TRACKER_JOBS.map((j, i) => (
        <div key={j.id} style={{ display: "grid", gridTemplateColumns: "1.4fr 110px 1fr 110px 90px", padding: "13px 18px", borderBottom: i < TRACKER_JOBS.length - 1 ? "1px solid var(--ink-50)" : "none", alignItems: "center", fontSize: 13 }}>
          <div style={{ display: "flex", alignItems: "center", gap: 10 }}>
            <span style={{ width: 2, height: 22, background: (TONES[j.tone] || TONES.neutral).color, borderRadius: 2 }}/>
            <div><div style={{ fontWeight: 600 }}>{j.title}</div><div style={{ fontSize: 12, color: "var(--fg-2)" }}>{j.co}</div></div>
          </div>
          <Chip tone={j.tone === "neutral" ? "neutral" : j.tone}>{j.stage}</Chip>
          <span style={{ fontSize: 12.5, color: "var(--fg-1)", display: "flex", alignItems: "center", gap: 8 }}>
            {j.next}
            {NET_PROBES[j.co] && NET_PROBES[j.co].hasPaths && <button onClick={() => onPaths && onPaths(j.co)} style={{ display: "inline-flex", alignItems: "center", gap: 4, fontSize: 11, fontWeight: 600, color: "var(--slate-text)", background: "var(--slate-tint)", border: "none", borderRadius: 999, padding: "2px 8px", cursor: "pointer" }}><Icon name="route" size={11}/>Paths</button>}
            {j.prep && <button onClick={onPrep} style={{ fontSize: 11, fontWeight: 600, color: "var(--cardinal)", background: "var(--cardinal-50)", border: "none", borderRadius: 999, padding: "2px 8px", cursor: "pointer" }}>Prep</button>}
          </span>
          <span style={{ fontSize: 11.5, color: "var(--fg-2)", display: "inline-flex", alignItems: "center", gap: 5 }}><Icon name={SRC_ICON[j.source] || "fileText"} size={12}/>{j.source}</span>
          <span style={{ textAlign: "right", fontFamily: "var(--font-mono)", fontSize: 12, color: "var(--fg-2)" }}>{j.fit.toFixed(2)}</span>
        </div>
      ))}
    </Panel>
  );
}

window.ScrTracker = ScrTracker;
