<%- include("../../../src/web/views/partials/layout-top", { title }) %>

<section class="card">
  <div class="section-header">
    <div>
      <h1>Birthdays</h1>
      <p class="command-subtitle">Birthday announcements, profile display, and optional Economy gifts.</p>
    </div>
  </div>
  <div class="table-wrap">
    <table class="table">
      <tbody>
        <tr><th>Discord client</th><td><%= diagnostics.discordAvailable ? (diagnostics.discordReady ? "Ready" : "Available") : "Unavailable" %></td></tr>
        <tr><th>Announcement channel</th><td><%= diagnostics.channel.message %></td></tr>
        <tr><th>Economy</th><td><%= diagnostics.economyAvailable ? "Available" : "Unavailable" %></td></tr>
        <tr><th>Today in the selected timezone</th><td><%= diagnostics.currentDate.year %>-<%= String(diagnostics.currentDate.month).padStart(2, "0") %>-<%= String(diagnostics.currentDate.day).padStart(2, "0") %></td></tr>
      </tbody>
    </table>
  </div>
</section>

<section class="card">
  <h2>Delivery</h2>
  <form method="post" action="/plugins/birthday/settings" class="form-grid">
    <div class="field">
      <label>Announcements</label>
      <label class="switch">
        <input type="checkbox" class="switch-input" name="enabled" <%= config.enabled ? "checked" : "" %> />
        <span class="switch-track" aria-hidden="true"></span>
        <span class="switch-text"><%= config.enabled ? "On" : "Off" %></span>
      </label>
    </div>
    <div class="field">
      <label>Timezone</label>
      <input name="timezone" value="<%= config.timezone %>" />
    </div>
    <div class="field">
      <label>Gift mode</label>
      <select name="gift_mode">
        <option value="automatic" <%= config.gift_mode === "automatic" ? "selected" : "" %>>Send automatically</option>
        <option value="manual" <%= config.gift_mode === "manual" ? "selected" : "" %>>Staff sends manually</option>
      </select>
    </div>
    <div class="field">
      <label>Gift amount</label>
      <input name="gift_amount" type="number" min="0" step="1" value="<%= config.gift_amount %>" <%= isAdmin ? "" : "readonly" %> />
      <% if (!isAdmin) { %><p class="hint">Only admins can change this value.</p><% } %>
    </div>
    <div class="field">
      <label>Check interval minutes</label>
      <input name="birthday_check_interval_minutes" type="number" min="5" max="1440" step="1" value="<%= config.birthday_check_interval_minutes %>" />
    </div>
    <div class="field">
      <label>Feb 29 handling</label>
      <select name="leap_day_policy">
        <option value="feb28" <%= config.leap_day_policy === "feb28" ? "selected" : "" %>>Celebrate on February 28</option>
        <option value="mar1" <%= config.leap_day_policy === "mar1" ? "selected" : "" %>>Celebrate on March 1</option>
      </select>
    </div>
    <div class="field full">
      <label>Announcement channel</label>
      <% if (textChannels.length) { %>
      <select name="announcement_channel_id">
        <option value="">Select a Discord text channel</option>
        <% textChannels.forEach((channel) => { %>
        <option value="<%= channel.id %>" <%= channel.id === config.announcement_channel_id ? "selected" : "" %>><%= channel.label %></option>
        <% }) %>
      </select>
      <% } else { %>
      <input name="announcement_channel_id" value="<%= config.announcement_channel_id %>" placeholder="Discord text channel ID" />
      <% } %>
      <p class="hint">Choose a regular text channel from this Discord server.</p>
    </div>
    <div class="field full">
      <button type="submit" class="button">Save settings</button>
    </div>
  </form>
</section>

<section class="card">
  <h2>Allowed Placeholders</h2>
  <p>
    <% allowedPlaceholders.forEach((name) => { %>
    <code>{<%= name %>}</code>
    <% }) %>
  </p>
</section>

<section class="card">
  <h2>Full-year birthday messages</h2>
  <% const fullYearMessages = config.response_templates.fullYear; %>
  <% if (!fullYearMessages.length) { %>
  <p>No templates configured.</p>
  <% } %>
  <% fullYearMessages.forEach((message) => { %>
  <form method="post" action="/plugins/birthday/templates/<%= message.id %>/update" class="form-grid">
    <input type="hidden" name="pool" value="fullYear" />
    <div class="field full">
      <label>Template</label>
      <textarea name="text" rows="3"><%= message.text %></textarea>
      <p class="hint">Preview: <%= previewTemplate(message.text) %></p>
    </div>
    <div class="field">
      <label>Enabled</label>
      <label class="switch">
        <input type="checkbox" class="switch-input" name="enabled" <%= message.enabled ? "checked" : "" %> />
        <span class="switch-track" aria-hidden="true"></span>
        <span class="switch-text"><%= message.enabled ? "On" : "Off" %></span>
      </label>
    </div>
    <div class="field profile-actions">
      <button type="submit" class="button subtle">Save message</button>
    </div>
  </form>
  <div class="profile-actions">
    <form method="post" action="/plugins/birthday/templates/<%= message.id %>/duplicate">
      <input type="hidden" name="pool" value="fullYear" />
      <button type="submit" class="button subtle">Duplicate</button>
    </form>
    <form method="post" action="/plugins/birthday/templates/<%= message.id %>/remove" data-confirm-mode="modal" data-confirm-title="Remove birthday template" data-confirm-text="Remove this birthday message template?" data-confirm-label="Remove template">
      <input type="hidden" name="pool" value="fullYear" />
      <button type="submit" class="button danger">Remove</button>
    </form>
  </div>
  <% }) %>
  <form method="post" action="/plugins/birthday/templates/create" class="form-grid">
    <input type="hidden" name="pool" value="fullYear" />
    <div class="field full">
      <label>New template</label>
      <textarea name="text" rows="3"></textarea>
    </div>
    <div class="field full">
      <button type="submit" class="button">Add template</button>
    </div>
  </form>
</section>

<section class="card">
  <h2>Partial-year birthday messages</h2>
  <% const partialYearMessages = config.response_templates.partialYear; %>
  <% if (!partialYearMessages.length) { %>
  <p>No templates configured.</p>
  <% } %>
  <% partialYearMessages.forEach((message) => { %>
  <form method="post" action="/plugins/birthday/templates/<%= message.id %>/update" class="form-grid">
    <input type="hidden" name="pool" value="partialYear" />
    <div class="field full">
      <label>Template</label>
      <textarea name="text" rows="3"><%= message.text %></textarea>
      <p class="hint">Preview: <%= previewTemplate(message.text) %></p>
    </div>
    <div class="field">
      <label>Enabled</label>
      <label class="switch">
        <input type="checkbox" class="switch-input" name="enabled" <%= message.enabled ? "checked" : "" %> />
        <span class="switch-track" aria-hidden="true"></span>
        <span class="switch-text"><%= message.enabled ? "On" : "Off" %></span>
      </label>
    </div>
    <div class="field profile-actions">
      <button type="submit" class="button subtle">Save message</button>
    </div>
  </form>
  <div class="profile-actions">
    <form method="post" action="/plugins/birthday/templates/<%= message.id %>/duplicate">
      <input type="hidden" name="pool" value="partialYear" />
      <button type="submit" class="button subtle">Duplicate</button>
    </form>
    <form method="post" action="/plugins/birthday/templates/<%= message.id %>/remove" data-confirm-mode="modal" data-confirm-title="Remove birthday template" data-confirm-text="Remove this birthday message template?" data-confirm-label="Remove template">
      <input type="hidden" name="pool" value="partialYear" />
      <button type="submit" class="button danger">Remove</button>
    </form>
  </div>
  <% }) %>
  <form method="post" action="/plugins/birthday/templates/create" class="form-grid">
    <input type="hidden" name="pool" value="partialYear" />
    <div class="field full">
      <label>New template</label>
      <textarea name="text" rows="3"></textarea>
    </div>
    <div class="field full">
      <button type="submit" class="button">Add template</button>
    </div>
  </form>
</section>

<%- include("../../../src/web/views/partials/layout-bottom") %>
