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

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

  <% 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 bot 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 Bot</h3>
        <p class="text-xs text-slate-500 mb-4">Get a token from <a href="https://t.me/BotFather" target="_blank" rel="noopener" class="text-brand-600 hover:underline">@BotFather</a>.</p>

        <form action="/telegram/bots" 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. Announcements Bot" 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">Bot Token</label>
            <input type="password" name="bot_token" required placeholder="123456:ABC-DEF..." 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 <span class="text-slate-400 font-normal">(optional)</span></label>
            <input type="number" name="daily_limit" placeholder="Leave blank for no cap" 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">
          </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_global" class="rounded text-brand-500 focus:ring-brand-500">
              <span class="text-xs font-medium text-brand-700">Make this a shared bot — 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 Bot
          </button>
        </form>
      </div>
    </div>

    <!-- Right: Bot 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">Available Bots</h3>
          <p class="text-xs text-slate-500 mt-0.5"><%= bots.length %> bot<%= bots.length === 1 ? '' : 's' %> — your own, plus any global bot</p>
        </div>

        <% if (bots.length === 0) { %>
          <div class="p-12 text-center text-slate-400">
            <p class="text-4xl mb-2">🤖</p>
            <p class="text-sm">No bots linked yet. Add one to start sending Telegram campaigns.</p>
          </div>
        <% } else { %>
          <div class="divide-y divide-sky-50">
            <% bots.forEach(b => { %>
              <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">
                      <%= b.label %>
                      <% if (b.is_global) { %><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">Token ending in ••<%= b.bot_token.slice(-4) %></p>
                  </div>
                  <% if (b.daily_limit != null) { %>
                    <div class="text-right">
                      <p class="text-xs font-semibold text-slate-700"><%= b.remaining_today %> / <%= b.daily_limit %></p>
                      <p class="text-xs text-slate-400">remaining today</p>
                    </div>
                  <% } else { %>
                    <span class="text-xs font-medium text-slate-400">No daily cap</span>
                  <% } %>
                </div>

                <% if (b.user_id !== null || isAdmin) { %>
                  <details class="mt-3 group">
                    <summary class="cursor-pointer text-xs font-medium text-brand-600 hover:text-brand-700 select-none">Edit bot</summary>
                    <form action="/telegram/bots/<%= b.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="<%= b.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">Daily Limit</label>
                        <input type="number" name="daily_limit" value="<%= b.daily_limit || '' %>" min="1" placeholder="No cap" 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">Bot Token <span class="text-slate-400 font-normal">(leave blank to keep current)</span></label>
                        <input type="password" name="bot_token" 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 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="/telegram/bots/<%= b.id %>/delete" method="POST" class="mt-2" onsubmit="return confirm('Remove this bot?');">
                    <button type="submit" class="text-xs font-medium text-red-500 hover:text-red-600">Remove bot</button>
                  </form>
                <% } %>
              </div>
            <% }) %>
          </div>
        <% } %>
      </div>
    </div>

  </div>
</main>

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