<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Ammo Chart — OnlyScavs</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <script src="https://cdn.jsdelivr.net/npm/chart.js@4.4.3/dist/chart.umd.min.js"></script>
  <style>
    :root {
      --bg: #0e0e0e;
      --panel: #161616;
      --panel2: #1c1c1c;
      --text: #e8e8e8;
      --muted: #777;
      --muted2: #555;
      --border: #262626;
      --accent: #9ccfff;
      --accent2: #ffd580;
    }
    * { box-sizing: border-box; margin: 0; padding: 0; }

    body {
      font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
      background: var(--bg);
      color: var(--text);
      min-height: 100vh;
    }

    /* ── NAV ─────────────────────────────────────────────────── */
    .site-nav {
      position: fixed;
      top: 0; left: 0; right: 0;
      z-index: 100;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 0 32px;
      height: 52px;
      background: rgba(14,14,14,0.88);
      backdrop-filter: blur(10px);
      -webkit-backdrop-filter: blur(10px);
      border-bottom: 1px solid rgba(255,255,255,0.06);
    }
    .nav-brand {
      font-size: 0.95rem;
      font-weight: 700;
      letter-spacing: 0.08em;
      text-transform: uppercase;
      color: var(--accent);
      text-decoration: none;
    }
    .nav-links { display: flex; gap: 4px; }
    .nav-links a {
      color: var(--muted);
      text-decoration: none;
      font-size: 0.82rem;
      padding: 6px 12px;
      border-radius: 6px;
      transition: color 0.15s, background 0.15s;
    }
    .nav-links a:hover, .nav-links a.active {
      color: var(--text);
      background: rgba(255,255,255,0.06);
    }

    /* ── LAYOUT ───────────────────────────────────────────────── */
    .page {
      max-width: 1280px;
      margin: 0 auto;
      padding: 80px 32px 60px;
    }

    .page-header {
      margin-bottom: 28px;
    }
    .page-header h1 {
      font-size: 1.6rem;
      font-weight: 800;
      letter-spacing: -0.01em;
      color: #fff;
    }
    .page-header p {
      font-size: 0.85rem;
      color: var(--muted);
      margin-top: 4px;
    }

    /* ── CONTROLS ─────────────────────────────────────────────── */
    .controls {
      display: flex;
      gap: 10px;
      flex-wrap: wrap;
      margin-bottom: 20px;
      align-items: center;
    }
    .controls input, .controls select {
      background: var(--panel);
      border: 1px solid var(--border);
      color: var(--text);
      padding: 7px 12px;
      border-radius: 7px;
      font-size: 0.83rem;
      outline: none;
      transition: border-color 0.15s;
    }
    .controls input:focus, .controls select:focus {
      border-color: var(--accent);
    }
    .controls input { width: 200px; }
    .controls select { cursor: pointer; }
    .controls label {
      font-size: 0.8rem;
      color: var(--muted);
      display: flex;
      align-items: center;
      gap: 6px;
      cursor: pointer;
    }
    .controls label input[type=checkbox] {
      width: auto;
      padding: 0;
      accent-color: var(--accent);
      cursor: pointer;
    }
    .count-badge {
      font-size: 0.75rem;
      color: var(--muted2);
      margin-left: auto;
    }

    /* ── CHART ────────────────────────────────────────────────── */
    .chart-wrap {
      background: var(--panel);
      border: 1px solid var(--border);
      border-radius: 12px;
      padding: 24px;
      position: relative;
      margin-bottom: 28px;
    }
    .chart-wrap canvas {
      display: block;
    }

    /* ── LEGEND ───────────────────────────────────────────────── */
    .legend-wrap {
      display: flex;
      flex-wrap: wrap;
      gap: 8px;
      margin-bottom: 24px;
    }
    .legend-chip {
      display: flex;
      align-items: center;
      gap: 6px;
      font-size: 0.75rem;
      color: var(--muted);
      padding: 4px 10px;
      border-radius: 20px;
      border: 1px solid var(--border);
      cursor: pointer;
      transition: border-color 0.15s, color 0.15s, opacity 0.15s;
    }
    .legend-chip:hover { border-color: #444; color: var(--text); }
    .legend-chip.hidden { opacity: 0.35; }
    .legend-dot {
      width: 9px; height: 9px;
      border-radius: 50%;
      flex-shrink: 0;
    }

    /* ── TABLE ────────────────────────────────────────────────── */
    .table-wrap {
      background: var(--panel);
      border: 1px solid var(--border);
      border-radius: 12px;
      overflow: auto;
    }
    table {
      width: 100%;
      border-collapse: collapse;
      font-size: 0.82rem;
    }
    thead th {
      background: var(--panel2);
      color: var(--muted);
      font-weight: 600;
      font-size: 0.72rem;
      letter-spacing: 0.06em;
      text-transform: uppercase;
      padding: 10px 14px;
      text-align: left;
      white-space: nowrap;
      position: sticky;
      top: 0;
      cursor: pointer;
      user-select: none;
    }
    thead th:hover { color: var(--text); }
    thead th.sort-asc::after  { content: " ↑"; color: var(--accent); }
    thead th.sort-desc::after { content: " ↓"; color: var(--accent); }
    tbody tr {
      border-top: 1px solid var(--border);
      transition: background 0.1s;
    }
    tbody tr:hover { background: var(--panel2); }
    tbody td { padding: 9px 14px; vertical-align: middle; white-space: nowrap; }
    td.num { text-align: right; font-variant-numeric: tabular-nums; }

    .tier-bar {
      display: inline-block;
      height: 6px;
      border-radius: 3px;
      min-width: 2px;
      vertical-align: middle;
      margin-right: 6px;
    }

    .tag {
      display: inline-block;
      font-size: 0.68rem;
      font-weight: 600;
      padding: 2px 6px;
      border-radius: 4px;
      background: rgba(156,207,255,0.12);
      color: var(--accent);
    }
    .tag.tracer {
      background: rgba(255,213,128,0.12);
      color: var(--accent2);
    }

    /* ── EMPTY ────────────────────────────────────────────────── */
    .empty {
      text-align: center;
      padding: 80px 20px;
      color: var(--muted2);
    }
    .empty strong { display: block; font-size: 1.1rem; margin-bottom: 8px; color: var(--muted); }

    /* ── TOOLTIP OVERRIDE ─────────────────────────────────────── */
    #chartjs-tooltip {
      background: #1a1a1a;
      border: 1px solid var(--border);
      border-radius: 8px;
      padding: 10px 14px;
      color: var(--text);
      font-size: 0.78rem;
      pointer-events: none;
      position: fixed;
      transition: opacity 0.15s;
      max-width: 220px;
      line-height: 1.6;
      z-index: 9999;
    }
    #chartjs-tooltip .tt-name { font-weight: 700; margin-bottom: 4px; color: #fff; }
    #chartjs-tooltip .tt-row { color: var(--muted); }
    #chartjs-tooltip .tt-row span { color: var(--text); }
  </style>
</head>
<body>

<nav class="site-nav">
  <a class="nav-brand" href="/">OnlyScavs</a>
  <div class="nav-links">
    <a href="/keys">Keys</a>
    <a href="/collector">Collector</a>
    <a href="/quests">Quests</a>
    <a href="/loadout">Loadout</a>
    <a href="/meds">Injectors</a>
    <a href="/barters">Barters</a>
    <a href="/ammo" class="active">Ammo</a>
  </div>
</nav>

<div class="page">
  <div class="page-header">
    <h1>Ammo Chart</h1>
    <p>Damage vs. Armor Penetration — scatter plot by caliber</p>
  </div>

  {% if not rounds %}
    <div class="empty">
      <strong>No ammo data yet.</strong>
      Run <code>python import_ammo.py</code> to import from tarkov.dev.
    </div>
  {% else %}

  <div class="controls">
    <input type="text" id="search" placeholder="Search ammo…" value="{{ q }}">
    <select id="caliberFilter">
      <option value="">All calibers</option>
      {% for c in calibers %}
        <option value="{{ c }}" {% if c == caliber %}selected{% endif %}>{{ c }}</option>
      {% endfor %}
    </select>
    <label>
      <input type="checkbox" id="tracerOnly"> Tracer only
    </label>
    <span class="count-badge" id="countBadge"></span>
  </div>

  <div class="chart-wrap">
    <canvas id="ammoChart" height="460"></canvas>
  </div>

  <div class="legend-wrap" id="legendWrap"></div>

  <div class="table-wrap">
    <table id="ammoTable">
      <thead>
        <tr>
          <th data-col="name">Name</th>
          <th data-col="caliber">Caliber</th>
          <th data-col="damage" class="sort-desc">Damage</th>
          <th data-col="penetration_power">Pen</th>
          <th data-col="armor_damage">Armor Dmg</th>
          <th data-col="fragmentation_chance">Frag %</th>
          <th data-col="initial_speed">Velocity</th>
          <th data-col="projectile_count">Pellets</th>
          <th data-col="tracer">Tracer</th>
        </tr>
      </thead>
      <tbody id="tableBody"></tbody>
    </table>
  </div>

  {% endif %}
</div>

<div id="chartjs-tooltip" style="opacity:0;"></div>

<script>
const ALL_ROUNDS = {{ rounds | tojson }};

// ── Color palette per caliber ──────────────────────────────────────────────
const PALETTE = [
  "#9ccfff","#ffd580","#7ee8a2","#ff9eb5","#c9b1ff",
  "#ffb347","#80deea","#f48fb1","#a5d6a7","#ffe082",
  "#90caf9","#ef9a9a","#ce93d8","#80cbc4","#ffcc02",
  "#ff7043","#66bb6a","#ab47bc","#26c6da","#d4e157",
];

function caliberColor(caliber, idx) {
  return PALETTE[idx % PALETTE.length];
}

// ── State ──────────────────────────────────────────────────────────────────
let sortCol = "damage";
let sortAsc  = false;
let hiddenCalibers = new Set();

// ── Filter rounds ──────────────────────────────────────────────────────────
function filteredRounds() {
  const q      = document.getElementById("search").value.trim().toLowerCase();
  const cal    = document.getElementById("caliberFilter").value;
  const tracer = document.getElementById("tracerOnly").checked;

  return ALL_ROUNDS.filter(r => {
    if (cal    && r.caliber !== cal) return false;
    if (tracer && !r.tracer)         return false;
    if (q && !r.name.toLowerCase().includes(q) &&
             !(r.short_name || "").toLowerCase().includes(q)) return false;
    return true;
  });
}

// ── Caliber list ───────────────────────────────────────────────────────────
function getCalibers(rounds) {
  const seen = new Map();
  rounds.forEach(r => { if (r.caliber && !seen.has(r.caliber)) seen.set(r.caliber, seen.size); });
  return seen;
}

// ── Chart ──────────────────────────────────────────────────────────────────
const ctx = document.getElementById("ammoChart");
let chart = null;

function buildDatasets(rounds) {
  const calibers = getCalibers(rounds);
  const groups = {};
  calibers.forEach((_, cal) => { groups[cal] = []; });

  rounds.forEach(r => {
    if (r.damage == null || r.penetration_power == null) return;
    if (hiddenCalibers.has(r.caliber)) return;
    const key = r.caliber || "Unknown";
    if (!groups[key]) groups[key] = [];
    groups[key].push({ x: r.damage, y: r.penetration_power, r: r });
  });

  const caliberList = [...calibers.keys()];
  return caliberList.map((cal, idx) => ({
    label: cal,
    data: (groups[cal] || []).map(p => ({ x: p.x, y: p.y, r: p.r })),
    backgroundColor: caliberColor(cal, idx) + "cc",
    borderColor:     caliberColor(cal, idx),
    borderWidth: 1,
    pointRadius: 6,
    pointHoverRadius: 9,
    _calIdx: idx,
  }));
}

function renderChart(rounds) {
  const datasets = buildDatasets(rounds);

  if (chart) {
    chart.data.datasets = datasets;
    chart.update("none");
    return;
  }

  const tooltip = document.getElementById("chartjs-tooltip");

  chart = new Chart(ctx, {
    type: "scatter",
    data: { datasets },
    options: {
      responsive: true,
      maintainAspectRatio: false,
      animation: { duration: 200 },
      plugins: {
        legend: { display: false },
        tooltip: {
          enabled: false,
          external(context) {
            const { chart, tooltip: tt } = context;
            if (tt.opacity === 0) { tooltip.style.opacity = 0; return; }

            const point = tt.dataPoints?.[0];
            if (!point) return;
            const r = point.raw.r;

            tooltip.innerHTML = `
              <div class="tt-name">${r.name}</div>
              <div class="tt-row">Caliber: <span>${r.caliber || "—"}</span></div>
              <div class="tt-row">Damage: <span>${r.damage ?? "—"}</span></div>
              <div class="tt-row">Pen: <span>${r.penetration_power ?? "—"}</span></div>
              <div class="tt-row">Armor Dmg: <span>${r.armor_damage ?? "—"}</span></div>
              <div class="tt-row">Frag: <span>${r.fragmentation_chance != null ? (r.fragmentation_chance * 100).toFixed(0) + "%" : "—"}</span></div>
              ${r.tracer ? '<div class="tt-row"><span style="color:var(--accent2)">Tracer</span></div>' : ""}
            `;

            const pos = chart.canvas.getBoundingClientRect();
            const x = pos.left + tt.caretX + 12;
            const y = pos.top  + tt.caretY - 10;
            tooltip.style.left    = x + "px";
            tooltip.style.top     = y + "px";
            tooltip.style.opacity = 1;
          }
        },
      },
      scales: {
        x: {
          title: { display: true, text: "Damage", color: "#777", font: { size: 12 } },
          grid:  { color: "rgba(255,255,255,0.05)" },
          ticks: { color: "#666" },
          min: 0,
        },
        y: {
          title: { display: true, text: "Armor Penetration", color: "#777", font: { size: 12 } },
          grid:  { color: "rgba(255,255,255,0.05)" },
          ticks: { color: "#666" },
          min: 0,
        },
      },
    },
  });
}

// ── Legend ─────────────────────────────────────────────────────────────────
function renderLegend(rounds) {
  const calibers = getCalibers(ALL_ROUNDS); // always all calibers in legend
  const wrap = document.getElementById("legendWrap");
  wrap.innerHTML = "";
  calibers.forEach((idx, cal) => {
    const chip = document.createElement("div");
    chip.className = "legend-chip" + (hiddenCalibers.has(cal) ? " hidden" : "");
    chip.innerHTML = `<span class="legend-dot" style="background:${caliberColor(cal, idx)}"></span>${cal}`;
    chip.addEventListener("click", () => {
      if (hiddenCalibers.has(cal)) hiddenCalibers.delete(cal);
      else hiddenCalibers.add(cal);
      refresh();
    });
    wrap.appendChild(chip);
  });
}

// ── Table ──────────────────────────────────────────────────────────────────
const caliberColorMap = new Map();

function calForRound(r) {
  if (!caliberColorMap.has(r.caliber)) {
    const idx = caliberColorMap.size;
    caliberColorMap.set(r.caliber, PALETTE[idx % PALETTE.length]);
  }
  return caliberColorMap.get(r.caliber);
}

// Pre-populate color map from all rounds so it's stable
(function() {
  const seen = new Set();
  ALL_ROUNDS.forEach(r => {
    if (r.caliber && !seen.has(r.caliber)) {
      seen.add(r.caliber);
      caliberColorMap.set(r.caliber, PALETTE[caliberColorMap.size % PALETTE.length]);
    }
  });
})();

function renderTable(rounds) {
  const sorted = [...rounds].sort((a, b) => {
    const va = a[sortCol] ?? (typeof a[sortCol] === "number" ? -Infinity : "");
    const vb = b[sortCol] ?? (typeof b[sortCol] === "number" ? -Infinity : "");
    if (va < vb) return sortAsc ? -1 : 1;
    if (va > vb) return sortAsc ?  1 : -1;
    return 0;
  });

  const maxPen = Math.max(...ALL_ROUNDS.map(r => r.penetration_power || 0), 1);
  const tbody  = document.getElementById("tableBody");
  tbody.innerHTML = "";

  sorted.forEach(r => {
    const color   = calForRound(r);
    const penPct  = Math.round(((r.penetration_power || 0) / maxPen) * 100);
    const fragPct = r.fragmentation_chance != null
      ? (r.fragmentation_chance * 100).toFixed(0) + "%" : "—";

    const tr = document.createElement("tr");
    tr.innerHTML = `
      <td>${r.name}</td>
      <td><span class="tag">${r.caliber || "—"}</span></td>
      <td class="num">${r.damage ?? "—"}</td>
      <td class="num">
        <span class="tier-bar" style="width:${penPct}%;background:${color}"></span>${r.penetration_power ?? "—"}
      </td>
      <td class="num">${r.armor_damage ?? "—"}</td>
      <td class="num">${fragPct}</td>
      <td class="num">${r.initial_speed != null ? Math.round(r.initial_speed) + " m/s" : "—"}</td>
      <td class="num">${r.projectile_count && r.projectile_count > 1 ? r.projectile_count : "—"}</td>
      <td>${r.tracer ? '<span class="tag tracer">T</span>' : ""}</td>
    `;
    tbody.appendChild(tr);
  });

  document.getElementById("countBadge").textContent = `${sorted.length} rounds`;

  // Update sort header indicators
  document.querySelectorAll("thead th[data-col]").forEach(th => {
    th.classList.remove("sort-asc", "sort-desc");
    if (th.dataset.col === sortCol) {
      th.classList.add(sortAsc ? "sort-asc" : "sort-desc");
    }
  });
}

// ── Sort clicks ────────────────────────────────────────────────────────────
document.querySelectorAll("thead th[data-col]").forEach(th => {
  th.addEventListener("click", () => {
    if (sortCol === th.dataset.col) sortAsc = !sortAsc;
    else { sortCol = th.dataset.col; sortAsc = false; }
    renderTable(filteredRounds());
  });
});

// ── Refresh ────────────────────────────────────────────────────────────────
function refresh() {
  const rounds = filteredRounds();
  renderChart(rounds);
  renderLegend(rounds);
  renderTable(rounds);
}

// ── Init ───────────────────────────────────────────────────────────────────
document.getElementById("search").addEventListener("input", refresh);
document.getElementById("caliberFilter").addEventListener("change", refresh);
document.getElementById("tracerOnly").addEventListener("change", refresh);

// Set chart container height explicitly for Chart.js
ctx.style.height = "460px";

refresh();
</script>

</body>
</html>
