<%- include('../partials/header') %>
<%- include('../partials/nav') %>

<main class="flex-grow max-w-7xl w-full mx-auto px-4 sm:px-6 lg:px-8 py-8">
  <div class="mb-2">
    <h1 class="text-3xl font-bold text-slate-900">✉️ Email</h1>
    <p class="text-sm text-slate-500 mt-1">Link SMTP servers with a daily send limit. Pick which ones to use per campaign.<% if (isAdmin) { %> As an admin you can also publish a shared server that users request a daily slice of.<% } %></p>
  </div>

  <%- include('../partials/channel-tabs-email', { active: 'servers' }) %>

  <% if (error) { %>
    <div class="mb-6 px-4 py-3 rounded-lg text-sm border bg-red-50 border-red-200 text-red-600"><%= error %></div>
  <% } %>
  <% if (message) { %>
    <div class="mb-6 px-4 py-3 rounded-lg text-sm border bg-emerald-50 border-emerald-200 text-emerald-700"><%= message %></div>
  <% } %>

  <div class="grid grid-cols-1 lg:grid-cols-3 gap-8">

    <!-- Left: Add server form -->
    <div class="lg:col-span-1">
      <div class="bg-white rounded-2xl border border-sky-100 shadow-sm p-6 sticky top-24">
        <h3 class="text-lg font-bold text-slate-900 mb-1">Add SMTP Server</h3>
        <p class="text-xs text-slate-500 mb-4">Each server gets its own daily send cap.</p>

        <form action="/email/servers" method="POST" class="space-y-4">
          <div>
            <label class="block text-xs font-medium text-slate-600 mb-1">Label</label>
            <input type="text" name="label" placeholder="e.g. Primary Gmail" class="block w-full px-3 py-2 border border-slate-300 rounded-lg shadow-sm text-sm focus:outline-none focus:ring-2 focus:ring-brand-500 focus:border-brand-500">
          </div>
          <div>
            <label class="block text-xs font-medium text-slate-600 mb-1">Host</label>
            <input type="text" name="host" required placeholder="smtp.example.com" class="block w-full px-3 py-2 border border-slate-300 rounded-lg shadow-sm text-sm focus:outline-none focus:ring-2 focus:ring-brand-500 focus:border-brand-500">
          </div>
          <div>
            <label class="block text-xs font-medium text-slate-600 mb-1">Port</label>
            <input type="text" name="port" value="587" class="block w-full px-3 py-2 border border-slate-300 rounded-lg shadow-sm text-sm focus:outline-none focus:ring-2 focus:ring-brand-500 focus:border-brand-500">
          </div>
          <div>
            <label class="block text-xs font-medium text-slate-600 mb-1">SMTP User</label>
            <input type="text" name="smtp_user" required placeholder="you@example.com" class="block w-full px-3 py-2 border border-slate-300 rounded-lg shadow-sm text-sm focus:outline-none focus:ring-2 focus:ring-brand-500 focus:border-brand-500">
          </div>
          <div>
            <label class="block text-xs font-medium text-slate-600 mb-1">SMTP Password</label>
            <input type="password" name="smtp_pass" required placeholder="••••••••" class="block w-full px-3 py-2 border border-slate-300 rounded-lg shadow-sm text-sm focus:outline-none focus:ring-2 focus:ring-brand-500 focus:border-brand-500">
          </div>
          <div>
            <label class="block text-xs font-medium text-slate-600 mb-1">Daily Send Limit</label>
            <input type="number" name="daily_limit" value="500" min="1" class="block w-full px-3 py-2 border border-slate-300 rounded-lg shadow-sm text-sm focus:outline-none focus:ring-2 focus:ring-brand-500 focus:border-brand-500">
            <p class="text-xs text-slate-400 mt-1">Max emails this server may send per day. Resets automatically at midnight.</p>
          </div>

          <% if (isAdmin) { %>
            <label class="flex items-center gap-2 px-3 py-2 bg-brand-50/60 border border-brand-100 rounded-lg cursor-pointer">
              <input type="checkbox" name="is_shared" class="rounded text-brand-500 focus:ring-brand-500">
              <span class="text-xs font-medium text-brand-700">Make this a shared server — users request a daily allocation from it</span>
            </label>
          <% } %>

          <button type="submit" class="w-full flex justify-center py-2.5 px-4 border border-transparent rounded-lg shadow-sm text-sm font-medium text-white bg-brand-500 hover:bg-brand-600 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-brand-500 transition">
            Add Server
          </button>
        </form>
      </div>
    </div>

    <!-- Right: Server list -->
    <div class="lg:col-span-2">
      <div class="bg-white rounded-2xl border border-sky-100 shadow-sm overflow-hidden">
        <div class="p-6 border-b border-sky-100">
          <h3 class="text-lg font-bold text-slate-900">Your Servers</h3>
          <p class="text-xs text-slate-500 mt-0.5"><%= servers.length %> server<%= servers.length === 1 ? '' : 's' %> linked</p>
        </div>

        <% if (servers.length === 0) { %>
          <div class="p-12 text-center text-slate-400">
            <p class="text-4xl mb-2">📮</p>
            <p class="text-sm">No SMTP servers linked yet. Add one to start sending email campaigns.</p>
          </div>
        <% } else { %>
          <div class="divide-y divide-sky-50">
            <% servers.forEach(s => { %>
              <div class="p-5">
                <div class="flex flex-col sm:flex-row sm:items-center sm:justify-between gap-3">
                  <div>
                    <p class="text-sm font-bold text-slate-900 flex items-center gap-2">
                      <%= s.label %>
                      <% if (s.is_shared) { %><span class="px-1.5 py-0.5 text-[10px] font-bold uppercase tracking-wide bg-brand-100 text-brand-700 rounded">Shared</span><% } %>
                    </p>
                    <p class="text-xs text-slate-500 mt-0.5"><%= s.smtp_user %> &middot; <%= s.host %>:<%= s.port %></p>
                  </div>
                  <div class="flex items-center gap-3">
                    <div class="text-right">
                      <p class="text-xs font-semibold text-slate-700"><%= s.remaining_today %> / <%= s.daily_limit %></p>
                      <p class="text-xs text-slate-400">remaining today</p>
                    </div>
                    <div class="w-20 h-2 bg-slate-100 rounded-full overflow-hidden">
                      <div class="h-full bg-brand-500" style="width: <%= s.daily_limit > 0 ? Math.min(100, Math.round((s.sent_today / s.daily_limit) * 100)) : 0 %>%"></div>
                    </div>
                  </div>
                </div>

                <details class="mt-3 group">
                  <summary class="cursor-pointer text-xs font-medium text-brand-600 hover:text-brand-700 select-none">Edit server</summary>
                  <form action="/email/servers/<%= s.id %>/update" method="POST" class="mt-3 grid grid-cols-1 sm:grid-cols-2 gap-3 bg-slate-50/60 p-4 rounded-xl border border-sky-100">
                    <div>
                      <label class="block text-xs font-medium text-slate-600 mb-1">Label</label>
                      <input type="text" name="label" value="<%= s.label %>" class="block w-full px-2.5 py-1.5 border border-slate-300 rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-brand-500">
                    </div>
                    <div>
                      <label class="block text-xs font-medium text-slate-600 mb-1">Host</label>
                      <input type="text" name="host" value="<%= s.host %>" required class="block w-full px-2.5 py-1.5 border border-slate-300 rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-brand-500">
                    </div>
                    <div>
                      <label class="block text-xs font-medium text-slate-600 mb-1">Port</label>
                      <input type="text" name="port" value="<%= s.port %>" class="block w-full px-2.5 py-1.5 border border-slate-300 rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-brand-500">
                    </div>
                    <div>
                      <label class="block text-xs font-medium text-slate-600 mb-1">SMTP User</label>
                      <input type="text" name="smtp_user" value="<%= s.smtp_user %>" required class="block w-full px-2.5 py-1.5 border border-slate-300 rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-brand-500">
                    </div>
                    <div>
                      <label class="block text-xs font-medium text-slate-600 mb-1">SMTP Password</label>
                      <input type="password" name="smtp_pass" value="<%= s.smtp_pass %>" required class="block w-full px-2.5 py-1.5 border border-slate-300 rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-brand-500">
                    </div>
                    <div>
                      <label class="block text-xs font-medium text-slate-600 mb-1">Daily Limit</label>
                      <input type="number" name="daily_limit" value="<%= s.daily_limit %>" min="1" class="block w-full px-2.5 py-1.5 border border-slate-300 rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-brand-500">
                    </div>
                    <div class="sm:col-span-2 flex justify-end gap-2 pt-1">
                      <button type="submit" class="px-4 py-1.5 text-xs font-semibold text-white bg-brand-500 hover:bg-brand-600 rounded-lg shadow-sm transition">Save Changes</button>
                    </div>
                  </form>
                </details>

                <form action="/email/servers/<%= s.id %>/delete" method="POST" class="mt-2" onsubmit="return confirm('Remove this SMTP server? Campaigns already sent through it keep their history.');">
                  <button type="submit" class="text-xs font-medium text-red-500 hover:text-red-600">Remove server</button>
                </form>
              </div>
            <% }) %>
          </div>
        <% } %>
      </div>
    </div>

  </div>
</main>

<%- include('../partials/footer') %>
