<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Daily Jobs</title>
<link href="https://fonts.googleapis.com/css2?family=Ubuntu:wght@400;700&display=swap" rel="stylesheet">
<style>
body{font-family:Ubuntu,sans-serif;background:#f4f6f4;margin:0;padding:32px 24px;}
h1{font-size:15px;font-weight:700;color:#28a745;margin:0 0 6px;}
.sub{font-size:12px;color:#aaa;margin-bottom:24px;}
.row{background:#fff;border-radius:8px;padding:14px 18px;margin-bottom:10px;box-shadow:0 1px 4px rgba(0,0,0,.06);}
.row-head{display:flex;align-items:baseline;gap:14px;flex-wrap:wrap;}
.name{font-size:14px;font-weight:700;color:#222;min-width:150px;}
.roles{font-size:13px;color:#555;flex:1;}
.geos{font-size:12px;color:#aaa;}
.badge{font-size:10px;font-weight:700;text-transform:uppercase;padding:2px 7px;border-radius:10px;}
.plan-silver{background:#f5f5f5;color:#757575;}
.plan-golden{background:#fff8e1;color:#f57f17;}
.plan-platinum{background:#ede7f6;color:#5e35b1;}
.stat{font-size:12px;color:#888;margin:10px 0 8px;}
.results{display:flex;flex-direction:column;gap:6px;}
.job{border:1px solid #e0e0e0;border-radius:6px;padding:9px 13px;font-size:13px;}
.job.pass{border-color:#c3e6cb;background:#f0faf2;}
.job.fail{opacity:.45;}
.job-title{font-weight:700;color:#222;}
.job-meta{font-size:12px;color:#888;margin:2px 0 3px;}
.job-reason{font-size:11px;color:#aaa;font-style:italic;}
.spinner{display:inline-block;width:12px;height:12px;border:2px solid #c3e6cb;border-top-color:#28a745;border-radius:50%;animation:spin .7s linear infinite;vertical-align:middle;margin-right:5px;}
@keyframes spin{to{transform:rotate(360deg)}}
.err{font-size:12px;color:#c62828;margin-top:8px;}
</style>
</head>
<body>
<h1>Daily Jobs</h1>
<div class="sub" id="global-status">Loading…</div>
<% mentees.forEach(function(m){ %>
<div class="row" id="row-<%= m.slug %>">
  <div class="row-head">
    <span class="name"><a href="/admin/profile/<%= m.slug %>" style="color:inherit;text-decoration:none;"><%= m.name %></a></span>
    <span class="roles"><%= (m.roles||[]).join(' · ') || '—' %></span>
    <span class="geos"><%= (m.geos||[]).join(', ') || '—' %></span>
    <span class="badge plan-<%= m.plan %>"><%= m.plan %></span>
  </div>
  <div id="results-<%= m.slug %>"><span class="spinner"></span><span style="font-size:12px;color:#aaa;">searching…</span></div>
</div>
<% }); %>
<% if (!mentees.length) { %><p style="color:#bbb;font-size:13px;">No active profiles.</p><% } %>

<div id="_md" style="display:none"><%= mentees.map(function(m){ return m.slug; }).join('|') %></div>
<script>
var mentees = document.getElementById('_md').textContent.split('|').filter(Boolean).map(function(s){ return {slug:s}; });
var done = 0;
var totalPassed = 0;

// Processar apenas o primeiro cliente
if (mentees.length > 0) {
  var m = mentees[0];
  
  fetch('/admin/api/profile/' + m.slug + '/job-search', { method:'POST', headers:{'Content-Type':'application/json','Authorization':'Bearer 15579990079586356780'}, body:'{}' })
  .then(function(r){ return r.json().then(function(d){ return { ok: r.ok, d: d }; }); })
  .then(function(res) {
    var out = document.getElementById('results-' + m.slug);
    if (!res.ok) { out.innerHTML = '<p class="err">Error: ' + esc(res.d.error||res.d) + '</p>'; return finish(); }
    
    var data = res.d, s = data.stats;
    totalPassed += s.passed;
    
    // SEM salvamento automático
    var html = '<div class="stat">' + s.total + ' found · ' + s.skipped + ' already tracked · ' + s.failed + ' filtered · <strong>' + s.passed + ' added</strong></div><div class="results">';
    
    (data.passed||[]).forEach(function(j) {
      html += '<div class="job pass"><div class="job-title">' + esc(j.title) + (j.url ? ' <a href="'+esc(j.url)+'" target="_blank" style="font-size:11px;color:#28a745;">↗</a>' : '') + '</div>';
      html += '<div class="job-meta">' + esc(j.company) + ' · ' + esc(j.location) + '</div>';
      if (j.aiReason) html += '<div class="job-reason">' + esc(j.aiReason) + '</div>';
      html += '</div>';
    });
    
    (data.failed||[]).forEach(function(j) {
      html += '<div class="job fail"><div class="job-title">' + esc(j.title) + ' — ' + esc(j.company) + '</div>';
      if (j.aiReason) html += '<div class="job-reason">' + esc(j.aiReason) + '</div>';
      html += '</div>';
    });
    
    out.innerHTML = html + '</div>';
    finish();
  })
  .catch(function(e){ document.getElementById('results-' + m.slug).innerHTML = '<p class="err">'+e.message+'</p>'; finish(); });
} else {
  document.getElementById('global-status').textContent = 'No active profiles.';
}

function finish() {
  document.getElementById('global-status').textContent = 'Done — ' + totalPassed + ' new jobs found for first client (not saved).';
}

function esc(s){ return String(s||'').replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;'); }</script>
</body>
</html>
