<%- 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">💬 SMS</h1>
    <p class="text-sm text-slate-500 mt-1">Link BestBulkSMS accounts with a daily send limit. <% if (isAdmin) { %>As an admin, you can also publish a shared account that users request a daily slice of.<% } %></p>
  </div>

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

  <% 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 account 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 SMS Account</h3>
        <p class="text-xs text-slate-500 mb-4">Get your API key from your <a href="https://www.bestbulksms.com.ng/app/user/developer" target="_blank" rel="noopener" class="text-brand-600 hover:underline">BestBulkSMS Developer page</a>.</p>

        <form action="/sms/accounts" 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. Main BestBulkSMS" 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">API Key</label>
            <input type="password" name="api_key" required placeholder="Paste your Bearer API key" 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">Sender ID</label>
            <input type="text" name="sender_id" value="BESTBULKSMS" placeholder="Your registered sender ID" 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">Source URL <span class="text-slate-400 font-normal">(optional)</span></label>
            <input type="text" name="source_url" placeholder="https://yourapp.com/campaigns" 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">BestBulkSMS asks production integrations to identify the source system for abuse tracking.</p>
          </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 SMS this account 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_system" class="rounded text-brand-500 focus:ring-brand-500">
              <span class="text-xs font-medium text-brand-700">Make this a shared account — 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 Account
          </button>
        </form>
      </div>
    </div>

    <!-- Right: Account 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 Accounts</h3>
          <p class="text-xs text-slate-500 mt-0.5"><%= accounts.length %> account<%= accounts.length === 1 ? '' : 's' %> linked</p>
        </div>

        <% if (accounts.length === 0) { %>
          <div class="p-12 text-center text-slate-400">
            <p class="text-4xl mb-2">💬</p>
            <p class="text-sm">No SMS accounts linked yet. Add one to start sending SMS campaigns.</p>
          </div>
        <% } else { %>
          <div class="divide-y divide-sky-50">
            <% accounts.forEach(a => { %>
              <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">
                      <%= a.label %>
                      <% if (a.is_system) { %><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">Sender ID: <%= a.sender_id %> &middot; Key ending in ••<%= a.api_key.slice(-4) %></p>
                  </div>
                  <div class="flex items-center gap-3">
                    <div class="text-right">
                      <p class="text-xs font-semibold text-slate-700"><%= a.remaining_today %> / <%= a.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: <%= a.daily_limit > 0 ? Math.min(100, Math.round((a.sent_today / a.daily_limit) * 100)) : 0 %>%"></div>
                    </div>
                  </div>
                </div>

                <% if (!a.is_system || isAdmin) { %>
                <details class="mt-3 group">
                  <summary class="cursor-pointer text-xs font-medium text-brand-600 hover:text-brand-700 select-none">Edit account</summary>
                  <form action="/sms/accounts/<%= a.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="<%= a.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">Sender ID</label>
                      <input type="text" name="sender_id" value="<%= a.sender_id %>" 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">
                      <label class="block text-xs font-medium text-slate-600 mb-1">API Key <span class="text-slate-400 font-normal">(leave blank to keep current)</span></label>
                      <input type="password" name="api_key" placeholder="••••••••" 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">
                      <label class="block text-xs font-medium text-slate-600 mb-1">Source URL</label>
                      <input type="text" name="source_url" value="<%= a.source_url || '' %>" 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="<%= a.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="/sms/accounts/<%= a.id %>/delete" method="POST" class="mt-2" onsubmit="return confirm('Remove this SMS account?');">
                  <button type="submit" class="text-xs font-medium text-red-500 hover:text-red-600">Remove account</button>
                </form>
                <% } %>
              </div>
            <% }) %>
          </div>
        <% } %>
      </div>
    </div>

  </div>
</main>

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