/**
 * Admin Dashboard Corporate - KPIs, Tendências e Detalhes
 * Filtros por período: Este Mês, Mês Anterior, Últimos 2 Meses, ou período customizado
 */
const { buildHeader, buildFooter, buildHead, getChartJsScript } = require('./ui-template');

function buildAdminDashboardHTML(user) {
  // Support both admin and corporate roles
  const role = user.role || 'corporate';
  const pageScripts = getChartJsScript();

  // Calculate default dates (current month)
  const now = new Date();
  const firstDayOfMonth = new Date(now.getFullYear(), now.getMonth(), 1).toISOString().slice(0, 10);
  const today = now.toISOString().slice(0, 10);

  const pageCSS = `
  /* Trading Terminal - Live Rates */
  .trading-terminal {
    background: linear-gradient(135deg, #0F1923 0%, #1A2332 50%, #0D1B2A 100%);
    border-top: 1px solid rgba(0,255,136,0.15);
    border-bottom: 1px solid rgba(0,255,136,0.15);
    padding: 16px 40px;
    position: relative;
    overflow: hidden;
  }
  .trading-terminal::before {
    content: '';
    position: absolute; top: 0; left: 0; right: 0; height: 1px;
    background: linear-gradient(90deg, transparent, rgba(0,255,136,0.3), transparent);
  }
  .trading-terminal::after {
    content: '';
    position: absolute; bottom: 0; left: 0; right: 0; height: 1px;
    background: linear-gradient(90deg, transparent, rgba(0,255,136,0.3), transparent);
  }
  .live-rate-bar {
    display: flex; align-items: center; justify-content: center; gap: 20px;
    max-width: 1600px; margin: 0 auto;
  }
  .terminal-title {
    font-size: 10px; font-weight: 700; color: rgba(0,255,136,0.6);
    text-transform: uppercase; letter-spacing: 2px; margin-right: 8px;
    font-family: 'SF Mono', 'Fira Code', 'Consolas', monospace;
  }
  .live-rate-dot {
    width: 7px; height: 7px; border-radius: 50%; background: #00FF88;
    display: inline-block; animation: blink 1.5s infinite;
    box-shadow: 0 0 6px rgba(0,255,136,0.6);
  }
  @keyframes blink { 0%, 100% { opacity: 1; } 50% { opacity: 0.2; } }
  .rate-pair-group {
    display: flex; align-items: center; gap: 8px;
    background: rgba(255,255,255,0.03);
    border: 1px solid rgba(255,255,255,0.06);
    border-radius: 8px; padding: 8px 14px;
  }
  .rate-pair-label {
    font-size: 11px; font-weight: 800; color: rgba(255,255,255,0.35);
    text-transform: uppercase; letter-spacing: 1px; writing-mode: vertical-rl;
    text-orientation: mixed; font-family: 'SF Mono', 'Fira Code', 'Consolas', monospace;
  }
  .live-rate-btn {
    display: flex; flex-direction: column; align-items: center; gap: 1px;
    padding: 8px 18px; border-radius: 6px; border: 1px solid transparent;
    font-family: inherit; font-size: 14px; font-weight: 700; cursor: default;
    transition: all 0.25s; min-width: 150px; justify-content: center;
    position: relative;
  }
  .rate-flags { font-size: 9px; opacity: 0.5; letter-spacing: 1px; line-height: 1; }
  .live-rate-btn.compra {
    background: rgba(0,255,136,0.08); color: #00FF88;
    border-color: rgba(0,255,136,0.2);
  }
  .live-rate-btn.compra:hover { background: rgba(0,255,136,0.14); }
  .live-rate-btn.venda {
    background: rgba(255,68,68,0.08); color: #FF4444;
    border-color: rgba(255,68,68,0.2);
  }
  .live-rate-btn.venda:hover { background: rgba(255,68,68,0.14); }
  .live-rate-btn .rate-value {
    font-size: 22px; font-weight: 800; letter-spacing: -0.5px;
    font-family: 'SF Mono', 'Fira Code', 'Consolas', monospace;
    font-variant-numeric: tabular-nums;
    text-shadow: 0 0 12px currentColor;
  }
  .live-rate-btn .rate-type {
    font-size: 9px; text-transform: uppercase; letter-spacing: 1.5px;
    opacity: 0.5; font-family: 'SF Mono', 'Fira Code', 'Consolas', monospace;
  }
  .live-rate-btn.pulse .rate-value { animation: ratePulse 0.4s ease; }
  @keyframes ratePulse {
    0% { transform: scale(1); text-shadow: 0 0 12px currentColor; }
    50% { transform: scale(1.06); text-shadow: 0 0 24px currentColor, 0 0 48px currentColor; }
    100% { transform: scale(1); text-shadow: 0 0 12px currentColor; }
  }
  .live-rate-time {
    font-size: 10px; color: rgba(255,255,255,0.25);
    font-family: 'SF Mono', 'Fira Code', 'Consolas', monospace;
    letter-spacing: 0.5px;
  }
  @media (max-width: 768px) {
    .trading-terminal { padding: 12px 16px; }
    .live-rate-bar { flex-wrap: wrap; gap: 10px; justify-content: center; }
    .terminal-title { display: none; }
    .rate-pair-group { padding: 8px 12px; width: 100%; justify-content: center; }
    .live-rate-btn { min-width: 120px; padding: 8px 14px; flex: 1; }
    .live-rate-btn .rate-value { font-size: 18px; }
    .live-rate-time { width: 100%; text-align: center; }
  }
  @media (max-width: 480px) {
    .trading-terminal { padding: 10px 12px; }
    .live-rate-bar { gap: 8px; flex-direction: column; align-items: center; }
    .rate-pair-group { padding: 10px; gap: 6px; }
    .rate-pair-label { writing-mode: horizontal-tb; font-size: 10px; }
    .live-rate-btn { min-width: 110px; padding: 8px 12px; }
    .live-rate-btn .rate-value { font-size: 16px; }
    .live-rate-btn .rate-type { font-size: 8px; }
    .rate-flags { font-size: 9px; }
    .live-rate-time { font-size: 9px; }
  }

  /* Filter Bar */
  .filter-bar {
    background: var(--card);
    border-radius: 16px;
    padding: 20px 24px;
    border: 1px solid var(--border);
    box-shadow: 0 2px 8px rgba(0,0,0,0.04);
    margin-bottom: 24px;
    display: flex;
    align-items: center;
    gap: 16px;
    flex-wrap: wrap;
  }
  .filter-bar-label {
    font-size: 13px;
    font-weight: 600;
    color: var(--text-secondary);
  }
  .filter-presets {
    display: flex;
    gap: 8px;
  }
  .preset-btn {
    padding: 8px 16px;
    border: 1px solid var(--border);
    border-radius: 8px;
    font-size: 13px;
    font-weight: 500;
    font-family: inherit;
    background: white;
    color: var(--text);
    cursor: pointer;
    transition: all 0.15s;
  }
  .preset-btn:hover {
    border-color: var(--primary);
    color: var(--primary);
  }
  .preset-btn.active {
    background: var(--primary);
    border-color: var(--primary);
    color: white;
  }
  .filter-divider {
    width: 1px;
    height: 32px;
    background: var(--border);
    margin: 0 8px;
  }
  .date-inputs {
    display: flex;
    align-items: center;
    gap: 8px;
  }
  .date-inputs label {
    font-size: 12px;
    font-weight: 600;
    color: var(--text-secondary);
  }
  .date-inputs input {
    padding: 8px 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
    font-size: 13px;
    font-family: inherit;
  }
  .btn-apply {
    padding: 8px 20px;
    background: var(--primary);
    color: white;
    border: none;
    border-radius: 8px;
    font-size: 13px;
    font-weight: 600;
    font-family: inherit;
    cursor: pointer;
    transition: all 0.15s;
  }
  .btn-apply:hover {
    background: var(--primary-light);
  }
  .period-info {
    margin-left: auto;
    font-size: 12px;
    color: var(--text-muted);
    background: var(--bg);
    padding: 6px 12px;
    border-radius: 6px;
  }

  .dashboard-grid {
    display: grid;
    gap: 24px;
  }

  /* KPI Cards */
  .kpi-row {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 20px;
  }
  .kpi-card {
    background: var(--card);
    border-radius: 16px;
    padding: 24px;
    border: 1px solid var(--border);
    box-shadow: 0 2px 8px rgba(0,0,0,0.04);
    position: relative;
    min-height: 140px;
  }
  .kpi-card.total { border-left: 4px solid var(--primary); }
  .kpi-card.brl-usd { border-left: 4px solid var(--blue); }
  .kpi-card.usd-brl { border-left: 4px solid var(--green); }
  .kpi-card.usd-usd { border-left: 4px solid var(--purple); }
  .kpi-label {
    font-size: 12px;
    font-weight: 600;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.3px;
    margin-bottom: 12px;
  }
  .kpi-value {
    font-size: 32px;
    font-weight: 800;
    color: var(--text);
    margin-bottom: 4px;
  }
  .kpi-sub {
    font-size: 13px;
    color: var(--text-muted);
  }
  .kpi-detail {
    margin-top: 8px;
    padding-top: 8px;
    border-top: 1px solid var(--border);
    font-size: 12px;
    color: var(--text-secondary);
  }
  .kpi-detail span { font-weight: 600; color: var(--text); }

  /* Chart Cards */
  .charts-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 24px;
  }
  .chart-card {
    background: var(--card);
    border-radius: 16px;
    padding: 24px;
    border: 1px solid var(--border);
    box-shadow: 0 2px 8px rgba(0,0,0,0.04);
    min-height: 380px;
    position: relative;
  }
  .chart-card.full-width {
    grid-column: span 2;
  }
  .chart-card h3 {
    font-size: 14px;
    font-weight: 700;
    margin-bottom: 20px;
    color: var(--text);
  }
  .chart-wrap {
    height: 300px;
    position: relative;
  }

  /* Details Table */
  .details-card {
    background: var(--card);
    border-radius: 16px;
    padding: 24px;
    border: 1px solid var(--border);
    box-shadow: 0 2px 8px rgba(0,0,0,0.04);
  }
  .details-card h3 {
    font-size: 14px;
    font-weight: 700;
    margin-bottom: 16px;
    color: var(--text);
  }
  .details-table {
    width: 100%;
    border-collapse: collapse;
  }
  .details-table th {
    text-align: left;
    font-size: 11px;
    font-weight: 600;
    color: var(--text-secondary);
    text-transform: uppercase;
    padding: 12px 8px;
    border-bottom: 2px solid var(--border);
  }
  .details-table td {
    padding: 12px 8px;
    font-size: 13px;
    border-bottom: 1px solid #F3F4F6;
  }
  .details-table tr:hover { background: #FAFBFC; }
  .details-table .num { text-align: right; font-variant-numeric: tabular-nums; }

  /* Ranking Card */
  .ranking-card {
    background: var(--card);
    border-radius: 16px;
    padding: 24px;
    border: 1px solid var(--border);
    box-shadow: 0 2px 8px rgba(0,0,0,0.04);
    min-height: 300px;
  }
  .ranking-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
  }
  .ranking-header h3 {
    font-size: 14px;
    font-weight: 700;
    color: var(--text);
  }
  .ranking-table {
    width: 100%;
    border-collapse: collapse;
  }
  .ranking-table th {
    text-align: left;
    font-size: 11px;
    font-weight: 600;
    color: var(--text-secondary);
    text-transform: uppercase;
    padding: 12px 8px;
    border-bottom: 2px solid var(--border);
  }
  .ranking-table td {
    padding: 14px 8px;
    font-size: 14px;
    border-bottom: 1px solid #F3F4F6;
  }
  .ranking-table tr:last-child td { border-bottom: none; }
  .rank-num { width: 40px; font-weight: 800; color: var(--primary); }
  .rank-1 { color: #FFD700; }
  .rank-2 { color: #C0C0C0; }
  .rank-3 { color: #CD7F32; }

  /* Loading State */
  .loading {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
    min-height: 100px;
  }
  .spinner {
    width: 32px;
    height: 32px;
    border: 3px solid var(--border);
    border-top-color: var(--primary);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
  }
  @keyframes spin { to { transform: rotate(360deg); } }
  .loading-text {
    margin-left: 12px;
    font-size: 13px;
    color: var(--text-muted);
  }

  /* Responsive */
  @media (max-width: 1200px) {
    .kpi-row { grid-template-columns: repeat(2, 1fr); }
    .charts-row { grid-template-columns: 1fr; }
    .chart-card.full-width { grid-column: span 1; }
  }
  @media (max-width: 768px) {
    .kpi-row { grid-template-columns: 1fr; }
    .filter-bar {
      flex-direction: column;
      align-items: stretch;
      padding: 16px;
      gap: 12px;
    }
    .filter-presets { flex-wrap: wrap; justify-content: center; }
    .preset-btn { flex: 1; min-width: 90px; text-align: center; }
    .filter-divider { display: none; }
    .date-inputs {
      flex-direction: column;
      align-items: stretch;
      gap: 8px;
      width: 100%;
    }
    .date-inputs input { width: 100%; }
    .btn-apply { width: 100%; }
    .period-info { margin-left: 0; text-align: center; }

    .kpi-card { padding: 16px; min-height: 120px; }
    .kpi-value { font-size: 24px; }
    .kpi-label { font-size: 11px; }
    .kpi-sub { font-size: 12px; }
    .kpi-detail { font-size: 11px; }

    .chart-card { padding: 16px; min-height: 300px; }
    .chart-card h3 { font-size: 13px; margin-bottom: 12px; }
    .chart-wrap { height: 240px; }

    .details-card, .ranking-card { padding: 16px; }
    .details-table th, .details-table td { padding: 8px 6px; font-size: 11px; }
    .ranking-table th, .ranking-table td { padding: 10px 6px; font-size: 12px; }
  }
  @media (max-width: 480px) {
    .filter-bar { padding: 12px; }
    .preset-btn { font-size: 11px; padding: 6px 10px; }
    .date-inputs input { font-size: 12px; padding: 8px 10px; }
    .kpi-value { font-size: 20px; }
    .chart-wrap { height: 200px; }
    .details-table th, .details-table td { padding: 6px 4px; font-size: 10px; }
  }

  /* Dark Mode overrides */
  [data-theme="dark"] .preset-btn { background: var(--card); color: var(--text-secondary); border-color: var(--border); }
  [data-theme="dark"] .preset-btn:hover { border-color: var(--admin-accent); color: var(--green); }
  [data-theme="dark"] .date-inputs input { background: var(--card); color: var(--text); border-color: var(--border); }
  [data-theme="dark"] .details-table tr:hover { background: rgba(255,255,255,0.04); }
  [data-theme="dark"] .details-table thead th { background: var(--bg); }
  [data-theme="dark"] .details-table td { border-bottom-color: var(--border); }
  [data-theme="dark"] .loading-overlay { background: rgba(13,17,23,0.85); }
  `;

  return `<!DOCTYPE html>
<html lang="pt-BR">
<head>
${buildHead('Dashboard Corporate', pageCSS, pageScripts)}
</head>
<body>

${buildHeader({ role: role, userName: user.nome, activePage: 'dashboard', permissions: user.permissions || [] })}

<div class="trading-terminal">
  <div class="live-rate-bar">
    <span class="live-rate-dot"></span>
    <span class="terminal-title">Live Rates</span>

    <div class="rate-pair-group">
      <span class="rate-pair-label">USD</span>
      <button class="live-rate-btn compra">
        <span class="rate-flags">\uD83C\uDDFA\uD83C\uDDF8 \u2192 \uD83C\uDDE7\uD83C\uDDF7</span>
        <span class="rate-type">Compra</span>
        <span class="rate-value" id="rateUsdCompra">--</span>
      </button>
      <button class="live-rate-btn venda">
        <span class="rate-flags">\uD83C\uDDE7\uD83C\uDDF7 \u2192 \uD83C\uDDFA\uD83C\uDDF8</span>
        <span class="rate-type">Venda</span>
        <span class="rate-value" id="rateUsdVenda">--</span>
      </button>
    </div>

    <span class="live-rate-time" id="rateTime">--</span>
  </div>
</div>

<div class="app-container">
  <div class="filter-bar">
    <span class="filter-bar-label">Periodo:</span>
    <div class="filter-presets">
      <button class="preset-btn" data-preset="hoje">Hoje</button>
      <button class="preset-btn active" data-preset="thisMonth">Este Mes</button>
      <button class="preset-btn" data-preset="lastMonth">Mes Anterior</button>
      <button class="preset-btn" data-preset="last2Months">Ultimos 2 Meses</button>
    </div>
    <div class="filter-divider"></div>
    <div class="date-inputs">
      <label>De:</label>
      <input type="date" id="dateStart" value="${firstDayOfMonth}">
      <label>Ate:</label>
      <input type="date" id="dateEnd" value="${today}">
      <button class="btn-apply" onclick="applyCustomDates()">Aplicar</button>
    </div>
    <div class="period-info" id="periodInfo">Carregando...</div>
  </div>

  <div class="dashboard-grid">
    <div class="kpi-row" id="kpiRow">
      <div class="kpi-card total"><div class="loading"><div class="spinner"></div><span class="loading-text">Carregando...</span></div></div>
      <div class="kpi-card brl-usd"><div class="loading"><div class="spinner"></div></div></div>
      <div class="kpi-card usd-brl"><div class="loading"><div class="spinner"></div></div></div>
      <div class="kpi-card usd-usd"><div class="loading"><div class="spinner"></div></div></div>
    </div>

    <div class="charts-row">
      <div class="chart-card" id="chartVolume">
        <h3>Volume Diario (USD)</h3>
        <div class="chart-wrap"><div class="loading"><div class="spinner"></div><span class="loading-text">Carregando grafico...</span></div></div>
      </div>
      <div class="chart-card" id="chartOrdens">
        <h3>Quantidade de Ordens por Dia</h3>
        <div class="chart-wrap"><div class="loading"><div class="spinner"></div><span class="loading-text">Carregando grafico...</span></div></div>
      </div>
    </div>

    <div class="charts-row">
      <div class="chart-card full-width" id="chartFluxos">
        <h3>Volume por Fluxo (Comparativo)</h3>
        <div class="chart-wrap"><div class="loading"><div class="spinner"></div><span class="loading-text">Carregando grafico...</span></div></div>
      </div>
    </div>

    <div class="details-card" id="detailsCard">
      <h3>Resumo Diario do Periodo</h3>
      <div id="detailsContent"><div class="loading"><div class="spinner"></div><span class="loading-text">Carregando detalhes...</span></div></div>
    </div>

    <div class="ranking-card" id="rankingCard">
      <div class="ranking-header"><h3>Top 5 Agentes no Periodo</h3></div>
      <div id="rankingContent"><div class="loading"><div class="spinner"></div><span class="loading-text">Carregando ranking...</span></div></div>
    </div>
  </div>
</div>

${buildFooter()}

<script>
const formatUSD = (v) => '$' + Number(v).toLocaleString('en-US', { minimumFractionDigits: 0, maximumFractionDigits: 0 });
const formatNum = (v) => Number(v).toLocaleString('pt-BR');
const formatDate = (d) => d.split('-').reverse().join('/');

let currentPeriod = { inicio: '${firstDayOfMonth}', fim: '${today}' };
let trendData = null;
let charts = {};

function getPresetDates(preset) {
  const now = new Date();
  let inicio, fim;
  if (preset === 'hoje') {
    inicio = now;
    fim = now;
  } else if (preset === 'thisMonth') {
    inicio = new Date(now.getFullYear(), now.getMonth(), 1);
    fim = now;
  } else if (preset === 'lastMonth') {
    inicio = new Date(now.getFullYear(), now.getMonth() - 1, 1);
    fim = new Date(now.getFullYear(), now.getMonth(), 0);
  } else if (preset === 'last2Months') {
    inicio = new Date(now.getFullYear(), now.getMonth() - 1, 1);
    fim = now;
  }
  return { inicio: inicio.toISOString().slice(0, 10), fim: fim.toISOString().slice(0, 10) };
}

document.querySelectorAll('.preset-btn').forEach(btn => {
  btn.addEventListener('click', () => {
    document.querySelectorAll('.preset-btn').forEach(b => b.classList.remove('active'));
    btn.classList.add('active');
    const dates = getPresetDates(btn.dataset.preset);
    document.getElementById('dateStart').value = dates.inicio;
    document.getElementById('dateEnd').value = dates.fim;
    currentPeriod = dates;
    loadAllData();
  });
});

function applyCustomDates() {
  document.querySelectorAll('.preset-btn').forEach(b => b.classList.remove('active'));
  currentPeriod = { inicio: document.getElementById('dateStart').value, fim: document.getElementById('dateEnd').value };
  loadAllData();
}

function updatePeriodInfo() {
  const dias = Math.ceil((new Date(currentPeriod.fim) - new Date(currentPeriod.inicio)) / (1000 * 60 * 60 * 24)) + 1;
  document.getElementById('periodInfo').textContent = formatDate(currentPeriod.inicio) + ' - ' + formatDate(currentPeriod.fim) + ' (' + dias + ' dias)';
}

async function loadKPIs() {
  try {
    const res = await fetch('/corporate/api/kpis-period?inicio=' + currentPeriod.inicio + '&fim=' + currentPeriod.fim);
    const json = await res.json();
    if (!json.success) throw new Error(json.error);
    const d = json.data;
    const cards = document.querySelectorAll('#kpiRow .kpi-card');

    cards[0].innerHTML = '<div class="kpi-label">Total no Periodo</div><div class="kpi-value">' + formatNum(d.total.qtd) + '</div><div class="kpi-sub">ordens realizadas</div><div class="kpi-detail">Volume: <span>' + formatUSD(d.total.vol_usd) + '</span> | Ticket Medio: <span>' + formatUSD(d.total.ticket_medio) + '</span></div>';
    cards[1].innerHTML = '<div class="kpi-label">BRL &rarr; USD</div><div class="kpi-value">' + formatNum(d.brlUsd.qtd) + '</div><div class="kpi-sub">' + formatUSD(d.brlUsd.vol_usd) + '</div><div class="kpi-detail">Ticket Medio: <span>' + formatUSD(d.brlUsd.ticket_medio) + '</span></div>';
    cards[2].innerHTML = '<div class="kpi-label">USD &rarr; BRL</div><div class="kpi-value">' + formatNum(d.usdBrl.qtd) + '</div><div class="kpi-sub">' + formatUSD(d.usdBrl.vol_usd) + '</div><div class="kpi-detail">Ticket Medio: <span>' + formatUSD(d.usdBrl.ticket_medio) + '</span></div>';
    cards[3].innerHTML = '<div class="kpi-label">USD &rarr; USD</div><div class="kpi-value">' + formatNum(d.usdUsd.qtd) + '</div><div class="kpi-sub">' + formatUSD(d.usdUsd.vol_usd) + '</div><div class="kpi-detail">Ticket Medio: <span>' + formatUSD(d.usdUsd.ticket_medio) + '</span></div>';
  } catch (err) { console.error('KPIs error:', err); }
}

async function loadTrend() {
  try {
    const res = await fetch('/corporate/api/trend-period?inicio=' + currentPeriod.inicio + '&fim=' + currentPeriod.fim);
    const json = await res.json();
    if (!json.success) throw new Error(json.error);
    trendData = json.data;

    const allDates = new Set();
    trendData.brlUsd.forEach(r => allDates.add(r.dia));
    trendData.usdBrl.forEach(r => allDates.add(r.dia));
    trendData.usdUsd.forEach(r => allDates.add(r.dia));
    const dates = Array.from(allDates).sort();

    const getVal = (arr, dia, key) => arr.find(r => r.dia === dia)?.[key] || 0;

    if (charts.volume) charts.volume.destroy();
    if (charts.ordens) charts.ordens.destroy();
    if (charts.fluxos) charts.fluxos.destroy();

    document.querySelector('#chartVolume .chart-wrap').innerHTML = '<canvas id="canvasVolume"></canvas>';
    charts.volume = new Chart(document.getElementById('canvasVolume'), {
      type: 'bar',
      data: {
        labels: dates.map(d => d.slice(5)),
        datasets: [
          { label: 'BRL→USD', data: dates.map(dia => getVal(trendData.brlUsd, dia, 'vol_usd')), backgroundColor: '#1A73E8', borderRadius: 4 },
          { label: 'USD→BRL', data: dates.map(dia => getVal(trendData.usdBrl, dia, 'vol_usd')), backgroundColor: '#1E8E3E', borderRadius: 4 },
          { label: 'USD→USD', data: dates.map(dia => getVal(trendData.usdUsd, dia, 'vol_usd')), backgroundColor: '#7B1FA2', borderRadius: 4 }
        ]
      },
      options: {
        responsive: true, maintainAspectRatio: false,
        plugins: { legend: { position: 'top', labels: { usePointStyle: true, font: { size: 11 } } }, tooltip: { callbacks: { label: ctx => ctx.dataset.label + ': ' + formatUSD(ctx.raw) } } },
        scales: { y: { beginAtZero: true, stacked: true, grid: { color: '#F3F4F6' }, ticks: { callback: v => '$' + (v >= 1e6 ? (v/1e6).toFixed(1)+'M' : v >= 1e3 ? (v/1e3).toFixed(0)+'k' : v) } }, x: { stacked: true, grid: { display: false }, ticks: { maxRotation: 0, autoSkip: true, maxTicksLimit: 15 } } }
      }
    });

    const consolidadoQtd = dates.map(dia => getVal(trendData.brlUsd, dia, 'qtd') + getVal(trendData.usdBrl, dia, 'qtd') + getVal(trendData.usdUsd, dia, 'qtd'));
    document.querySelector('#chartOrdens .chart-wrap').innerHTML = '<canvas id="canvasOrdens"></canvas>';
    charts.ordens = new Chart(document.getElementById('canvasOrdens'), {
      type: 'line',
      data: { labels: dates.map(d => d.slice(5)), datasets: [{ label: 'Total Ordens', data: consolidadoQtd, borderColor: '#7600be', backgroundColor: 'rgba(118,0,190,0.1)', fill: true, tension: 0.3, pointRadius: 3 }] },
      options: { responsive: true, maintainAspectRatio: false, plugins: { legend: { display: false } }, scales: { y: { beginAtZero: true, grid: { color: '#F3F4F6' } }, x: { grid: { display: false }, ticks: { maxRotation: 0, autoSkip: true, maxTicksLimit: 15 } } } }
    });

    document.querySelector('#chartFluxos .chart-wrap').innerHTML = '<canvas id="canvasFluxos"></canvas>';
    charts.fluxos = new Chart(document.getElementById('canvasFluxos'), {
      type: 'line',
      data: {
        labels: dates.map(d => d.slice(5)),
        datasets: [
          { label: 'BRL→USD', data: dates.map(dia => getVal(trendData.brlUsd, dia, 'vol_usd')), borderColor: '#1A73E8', backgroundColor: 'rgba(26,115,232,0.1)', fill: true, tension: 0.3, pointRadius: 2 },
          { label: 'USD→BRL', data: dates.map(dia => getVal(trendData.usdBrl, dia, 'vol_usd')), borderColor: '#1E8E3E', backgroundColor: 'rgba(30,142,62,0.1)', fill: true, tension: 0.3, pointRadius: 2 },
          { label: 'USD→USD', data: dates.map(dia => getVal(trendData.usdUsd, dia, 'vol_usd')), borderColor: '#7B1FA2', backgroundColor: 'rgba(123,31,162,0.1)', fill: true, tension: 0.3, pointRadius: 2 }
        ]
      },
      options: {
        responsive: true, maintainAspectRatio: false,
        plugins: { legend: { position: 'top', labels: { usePointStyle: true, font: { size: 11 } } }, tooltip: { callbacks: { label: ctx => ctx.dataset.label + ': ' + formatUSD(ctx.raw) } } },
        scales: { y: { beginAtZero: true, grid: { color: '#F3F4F6' }, ticks: { callback: v => '$' + (v >= 1e6 ? (v/1e6).toFixed(1)+'M' : v >= 1e3 ? (v/1e3).toFixed(0)+'k' : v) } }, x: { grid: { display: false }, ticks: { maxRotation: 0, autoSkip: true, maxTicksLimit: 20 } } }
      }
    });

    renderDetailsTable(dates);
  } catch (err) { console.error('Trend error:', err); }
}

function renderDetailsTable(dates) {
  if (!trendData || !dates.length) { document.getElementById('detailsContent').innerHTML = '<p style="color:var(--text-muted);text-align:center;padding:20px;">Sem dados para o periodo.</p>'; return; }
  const getVal = (arr, dia, key) => arr.find(r => r.dia === dia)?.[key] || 0;
  const recentDates = dates.slice(-10).reverse();

  let html = '<table class="details-table"><thead><tr><th>Data</th><th class="num">BRL→USD</th><th class="num">USD→BRL</th><th class="num">USD→USD</th><th class="num">Total Qtd</th><th class="num">Volume Total</th></tr></thead><tbody>';
  recentDates.forEach(dia => {
    const brlUsdQtd = getVal(trendData.brlUsd, dia, 'qtd');
    const usdBrlQtd = getVal(trendData.usdBrl, dia, 'qtd');
    const usdUsdQtd = getVal(trendData.usdUsd, dia, 'qtd');
    const brlUsdVol = getVal(trendData.brlUsd, dia, 'vol_usd');
    const usdBrlVol = getVal(trendData.usdBrl, dia, 'vol_usd');
    const usdUsdVol = getVal(trendData.usdUsd, dia, 'vol_usd');
    const totalQtd = brlUsdQtd + usdBrlQtd + usdUsdQtd;
    const totalVol = brlUsdVol + usdBrlVol + usdUsdVol;
    html += '<tr><td>' + formatDate(dia) + '</td><td class="num">' + brlUsdQtd + '</td><td class="num">' + usdBrlQtd + '</td><td class="num">' + usdUsdQtd + '</td><td class="num"><strong>' + totalQtd + '</strong></td><td class="num"><strong>' + formatUSD(totalVol) + '</strong></td></tr>';
  });
  html += '</tbody></table>';
  if (dates.length > 10) html += '<p style="font-size:12px;color:var(--text-muted);margin-top:12px;text-align:center;">Mostrando os 10 dias mais recentes de ' + dates.length + ' dias no periodo.</p>';
  document.getElementById('detailsContent').innerHTML = html;
}

async function loadRanking() {
  const dias = Math.ceil((new Date(currentPeriod.fim) - new Date(currentPeriod.inicio)) / (1000 * 60 * 60 * 24)) + 1;
  const content = document.getElementById('rankingContent');
  content.innerHTML = '<div class="loading"><div class="spinner"></div><span class="loading-text">Carregando...</span></div>';
  try {
    const res = await fetch('/corporate/api/top-agentes?dias=' + dias);
    const json = await res.json();
    if (!json.success) throw new Error(json.error);
    if (json.data.length === 0) { content.innerHTML = '<p style="text-align:center;color:var(--text-muted);padding:40px;">Nenhum dado encontrado.</p>'; return; }

    let html = '<table class="ranking-table"><thead><tr><th>#</th><th>Agente</th><th>Qtd Ordens</th><th>Volume USD</th></tr></thead><tbody>';
    json.data.forEach(r => { html += '<tr><td class="rank-num rank-' + r.rank + '">' + r.rank + '</td><td>' + r.agente + '</td><td>' + formatNum(r.qtd) + '</td><td>' + formatUSD(r.vol_usd) + '</td></tr>'; });
    html += '</tbody></table>';
    content.innerHTML = html;
  } catch (err) { console.error('Ranking error:', err); content.innerHTML = '<p style="color:var(--red);padding:20px;">Erro ao carregar ranking</p>'; }
}

function loadAllData() { updatePeriodInfo(); loadKPIs(); loadTrend(); loadRanking(); }
document.addEventListener('DOMContentLoaded', loadAllData);

// Live Rates
let _lastUsdBid = null, _lastUsdAsk = null;
function pulseEl(el) { el.closest('.live-rate-btn').classList.add('pulse'); setTimeout(() => el.closest('.live-rate-btn').classList.remove('pulse'), 500); }
async function fetchLiveRate() {
  try {
    const resp = await fetch('/api/cotacao');
    const json = await resp.json();
    const usd = json.USDBRL;
    if (usd) {
      const bidRaw = parseFloat(usd.bid), askRaw = parseFloat(usd.ask);
      const bid = bidRaw * (1 - 0.0043);
      const ask = askRaw * (1 + 0.0005);
      const elC = document.getElementById('rateUsdCompra'), elV = document.getElementById('rateUsdVenda');
      elC.textContent = bid.toFixed(4); elV.textContent = ask.toFixed(4);
      if (_lastUsdBid !== null && bidRaw !== _lastUsdBid) pulseEl(elC);
      if (_lastUsdAsk !== null && askRaw !== _lastUsdAsk) pulseEl(elV);
      _lastUsdBid = bidRaw; _lastUsdAsk = askRaw;
    }
    document.getElementById('rateTime').textContent = new Date().toLocaleTimeString('pt-BR');
  } catch (e) { /* retry next cycle */ }
}
fetchLiveRate();
setInterval(fetchLiveRate, 3000);
<\/script>
</body>
</html>`;
}

module.exports = { buildAdminDashboardHTML };
