<!doctype html>
<html>
<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <title>Mastermind — Inbox</title>
  <style>
    body{font-family:system-ui,-apple-system,Segoe UI,Roboto,Arial,sans-serif;margin:0;background:#0b0f14;color:#e8eef7}
    header{background:#111824;border-bottom:1px solid #233043;padding:14px 16px;position:sticky;top:0}
    main{max-width:1200px;margin:0 auto;padding:16px}
    .card{background:#111824;border:1px solid #233043;border-radius:12px;padding:14px;margin:12px 0}
    a{color:#7cc4ff;text-decoration:none}
    .muted{color:#a9b7c6}
    input,select{padding:12px;border-radius:10px;border:1px solid #233043;background:#0e1520;color:#e8eef7;width:100%}
    button{padding:12px 14px;border-radius:10px;border:1px solid #233043;background:#1c2a3d;color:#e8eef7}
    table{width:100%;border-collapse:collapse}
    th,td{border-bottom:1px solid #233043;padding:10px 6px;text-align:left;color:#a9b7c6;font-size:13px;vertical-align:top}
    th{color:#e8eef7}
    .grid{display:grid;grid-template-columns:repeat(12,1fr);gap:12px}
    .col6{grid-column:span 12}
    @media(min-width:900px){.col6{grid-column:span 6}}
    .actions{display:flex;gap:8px;flex-wrap:wrap}
  </style>
</head>
<body>
<header>
  <div style="display:flex;justify-content:space-between;align-items:center;gap:10px">
    <div style="font-weight:700">Inbox</div>
    <div style="display:flex;gap:10px">
      <a href="/projects">Projects</a>
      <a href="/">Dashboard</a>
      <a href="/logout">Logout</a>
    </div>
  </div>
</header>
<main>
  <div class="card">
    <h2 style="margin:0 0 8px">Import emails (until OAuth is connected)</h2>
    <div class="muted">Upload .eml files. Mastermind stores them and puts them into the Unsorted queue for triage.</div>
    <form method="post" action="/inbox/upload" enctype="multipart/form-data" class="grid" style="margin-top:10px">
      <div class="col6">
        <label class="muted">.eml files</label>
        <input type="file" name="emls" multiple accept=".eml,message/rfc822" required />
      </div>
      <div class="col6">
        <label class="muted">Source label</label>
        <input name="source" placeholder="outlook-export" value="upload" />
      </div>
      <div style="grid-column:span 12">
        <button type="submit">Import</button>
      </div>
    </form>
  </div>

  <div class="card">
    <h2 style="margin:0 0 8px">Unsorted</h2>
    <div class="muted">Assign each email to a project. Later this will be automated with rules + OAuth sync.</div>
    <div style="overflow:auto;margin-top:10px">
      <table>
        <thead>
          <tr>
            <th>Date</th>
            <th>From</th>
            <th>Subject</th>
            <th>Assign</th>
          </tr>
        </thead>
        <tbody>
          <% emails.forEach(e => { %>
            <tr>
              <td><%= e.date || '' %></td>
              <td><%= e.from_addr || '' %></td>
              <td><a href="/inbox/<%= e.id %>"><%= e.subject || '(no subject)' %></a></td>
              <td>
                <form method="post" action="/inbox/<%= e.id %>/assign" class="actions" style="margin-bottom:8px">
                  <select name="projectId" required>
                    <option value="" disabled selected>Select project…</option>
                    <% projects.forEach(p => { %>
                      <option value="<%= p.id %>"><%= p.name %> (<%= p.job_number || '—' %>)</option>
                    <% }) %>
                  </select>
                  <button type="submit">Assign</button>
                </form>

                <div class="muted" style="font-size:12px;margin-bottom:6px">Make a rule from this email:</div>
                <form method="post" action="/inbox/<%= e.id %>/rule/from_domain" class="actions" style="margin-bottom:8px">
                  <select name="projectId" required>
                    <option value="" disabled selected>Project…</option>
                    <% projects.forEach(p => { %>
                      <option value="<%= p.id %>"><%= p.name %></option>
                    <% }) %>
                  </select>
                  <button type="submit">Rule: From domain</button>
                </form>

                <form method="post" action="/inbox/<%= e.id %>/rule/subject_job" class="actions">
                  <select name="projectId" required>
                    <option value="" disabled selected>Project…</option>
                    <% projects.forEach(p => { %>
                      <option value="<%= p.id %>"><%= p.name %></option>
                    <% }) %>
                  </select>
                  <button type="submit">Rule: Subject job#</button>
                </form>
              </td>
            </tr>
          <% }) %>
        </tbody>
      </table>
    </div>
  </div>
</main>
</body>
</html>
