<%- include('../partials/head', { title, bodyClass: 'app-page admin-page' }) %>
<%- include('../partials/app-header') %>

<main class="app-main">
  <%- include('../partials/admin-nav') %>
  <section class="page-title">
    <p class="eyebrow">Admin</p>
    <h1>Réglages et logs</h1>
    <p>Textes simples de landing et suivi des synchronisations.</p>
  </section>

  <section class="glass-panel">
    <form method="post" action="/admin/settings" class="form-stack">
      <label>Titre landing
        <input type="text" name="landing_headline" value="<%= settings.landing_headline || '' %>">
      </label>
      <label>Prix Pro
        <input type="text" name="pro_price_label" value="<%= settings.pro_price_label || '' %>">
      </label>
      <label>Slogan page TopStats PRO
        <input type="text" name="pro_hero_subtitle" value="<%= settings.pro_hero_subtitle || 'Passe en mode Coupe du Monde !' %>" placeholder="Passe en mode Coupe du Monde !">
      </label>
      <section class="admin-promo-settings" aria-label="Bannière promo page Matchs">
        <div>
          <strong>Bannière promo page Matchs</strong>
          <p>Image paysage affichée entre les matchs et le footer. Idéal pour une offre in-app, Coupe du Monde ou pack spécial.</p>
        </div>
        <label class="admin-toggle-row">
          <input type="checkbox" name="matches_promo_enabled" value="1" <%= settings.matches_promo_enabled === 'true' ? 'checked' : '' %>>
          <span>Afficher la bannière sur la page Matchs</span>
        </label>
        <div class="admin-promo-grid">
          <label>Lien de redirection
            <input type="text" inputmode="url" name="matches_promo_link_url" value="<%= settings.matches_promo_link_url || '' %>" placeholder="/pro?promo=CDM50">
          </label>
          <label>Texte alternatif
            <input type="text" name="matches_promo_alt" value="<%= settings.matches_promo_alt || 'Promotion TopStats' %>" placeholder="Promotion TopStats">
          </label>
        </div>
        <div class="admin-promo-upload" data-promo-banner-admin>
          <input type="hidden" name="matches_promo_existing_image" value="<%= settings.matches_promo_image_url || '' %>">
          <input type="hidden" name="matches_promo_image_data" data-promo-banner-data>
          <label>Image paysage
            <input type="file" accept="image/png,image/jpeg,image/webp" data-promo-banner-upload>
          </label>
          <label>Ou URL image
            <input type="text" inputmode="url" name="matches_promo_image_url" value="<%= settings.matches_promo_image_url || '' %>" placeholder="/uploads/promos/image.webp">
          </label>
          <label class="admin-toggle-row">
            <input type="checkbox" name="matches_promo_remove" value="1">
            <span>Supprimer l’image actuelle</span>
          </label>
          <figure class="admin-promo-preview <%= settings.matches_promo_image_url ? '' : 'is-empty' %>" data-promo-banner-preview>
            <% if (settings.matches_promo_image_url) { %>
              <img src="<%= settings.matches_promo_image_url %>" alt="">
            <% } %>
            <figcaption data-promo-banner-status><%= settings.matches_promo_image_url ? 'Imagen actual' : 'Ninguna imagen seleccionada' %></figcaption>
          </figure>
        </div>
      </section>
      <section class="admin-promo-settings" aria-label="Bannière affiliation page Matchs">
        <div>
          <strong>Bannière affiliation page Matchs</strong>
          <p>Deuxième image paysage affichée sous la bannière principale. Le lien peut pointer vers une page externe partenaire.</p>
        </div>
        <label class="admin-toggle-row">
          <input type="checkbox" name="matches_affiliate_promo_enabled" value="1" <%= settings.matches_affiliate_promo_enabled === 'true' ? 'checked' : '' %>>
          <span>Afficher la bannière affiliation sur la page Matchs</span>
        </label>
        <div class="admin-promo-grid">
          <label>Lien personnalisé
            <input type="text" inputmode="url" name="matches_affiliate_promo_link_url" value="<%= settings.matches_affiliate_promo_link_url || '' %>" placeholder="https://partenaire.example/offre">
          </label>
          <label>Texte alternatif
            <input type="text" name="matches_affiliate_promo_alt" value="<%= settings.matches_affiliate_promo_alt || 'Offre partenaire TopStats' %>" placeholder="Offre partenaire TopStats">
          </label>
        </div>
        <div class="admin-promo-upload" data-promo-banner-admin>
          <input type="hidden" name="matches_affiliate_promo_existing_image" value="<%= settings.matches_affiliate_promo_image_url || '' %>">
          <input type="hidden" name="matches_affiliate_promo_image_data" data-promo-banner-data>
          <label>Image paysage
            <input type="file" accept="image/png,image/jpeg,image/webp" data-promo-banner-upload>
          </label>
          <label>Ou URL image
            <input type="text" inputmode="url" name="matches_affiliate_promo_image_url" value="<%= settings.matches_affiliate_promo_image_url || '' %>" placeholder="/uploads/promos/image.webp">
          </label>
          <label class="admin-toggle-row">
            <input type="checkbox" name="matches_affiliate_promo_remove" value="1">
            <span>Supprimer l’image actuelle</span>
          </label>
          <figure class="admin-promo-preview <%= settings.matches_affiliate_promo_image_url ? '' : 'is-empty' %>" data-promo-banner-preview>
            <% if (settings.matches_affiliate_promo_image_url) { %>
              <img src="<%= settings.matches_affiliate_promo_image_url %>" alt="">
            <% } %>
            <figcaption data-promo-banner-status><%= settings.matches_affiliate_promo_image_url ? 'Imagen actual' : 'Ninguna imagen seleccionada' %></figcaption>
          </figure>
        </div>
      </section>
      <label>Ajustement simulation Historique TopStats PRO
        <input
          type="text"
          name="neo_history_pro_gain_bonus"
          inputmode="decimal"
          value="<%= settings.neo_history_pro_gain_bonus_cents ? (Number(settings.neo_history_pro_gain_bonus_cents || 0) / 100).toLocaleString('es-ES', { minimumFractionDigits: 0, maximumFractionDigits: 2 }) : '' %>"
          placeholder="Ex : 1000"
        >
        <small class="micro-copy">Optionnel. Vide = gain réel uniquement. Si renseigné, ce montant est ajouté à la simulation PRO affichée.</small>
      </label>
      <div class="settings-finance-grid">
        <label>API-Football / mois
          <input
            type="text"
            name="finance_api_football_monthly"
            inputmode="decimal"
            value="<%= settings.finance_api_football_monthly_cents ? (Number(settings.finance_api_football_monthly_cents || 0) / 100).toLocaleString('es-ES', { minimumFractionDigits: 0, maximumFractionDigits: 2 }) : '' %>"
            placeholder="Ex : 99"
          >
        </label>
        <label>The Odds API / mois
          <input
            type="text"
            name="finance_odds_api_monthly"
            inputmode="decimal"
            value="<%= settings.finance_odds_api_monthly_cents ? (Number(settings.finance_odds_api_monthly_cents || 0) / 100).toLocaleString('es-ES', { minimumFractionDigits: 0, maximumFractionDigits: 2 }) : '' %>"
            placeholder="Ex : 49"
          >
        </label>
        <label>Autres API / mois
          <input
            type="text"
            name="finance_other_api_monthly"
            inputmode="decimal"
            value="<%= settings.finance_other_api_monthly_cents ? (Number(settings.finance_other_api_monthly_cents || 0) / 100).toLocaleString('es-ES', { minimumFractionDigits: 0, maximumFractionDigits: 2 }) : '' %>"
            placeholder="Ex : 20"
          >
        </label>
        <label>Frais Stripe %
          <input
            type="text"
            name="finance_stripe_fee_percent"
            inputmode="decimal"
            value="<%= settings.finance_stripe_fee_percent || '1.5' %>"
            placeholder="1,5"
          >
        </label>
        <label>Frais Stripe fixe
          <input
            type="text"
            name="finance_stripe_fee_fixed"
            inputmode="decimal"
            value="<%= settings.finance_stripe_fee_fixed_cents ? (Number(settings.finance_stripe_fee_fixed_cents || 0) / 100).toLocaleString('es-ES', { minimumFractionDigits: 0, maximumFractionDigits: 2 }) : '0,25' %>"
            placeholder="0,25"
          >
        </label>
      </div>
      <small class="micro-copy">Ces coûts servent au tableau financier admin : brut, frais Stripe, commissions affiliés, coûts IA/API, bénéfice net et partage 50/50.</small>
      <button class="btn btn-primary" type="submit">Enregistrer</button>
    </form>
  </section>

  <div class="table-card">
    <table>
      <thead><tr><th>Source</th><th>Niveau</th><th>Message</th><th>Date</th></tr></thead>
      <tbody>
        <% if (!logs.length) { %>
          <tr><td colspan="4">No hay logs de sincronización.</td></tr>
        <% } %>
        <% logs.forEach((log) => { %>
          <tr>
            <td><%= log.source %></td>
            <td><%= log.level %></td>
            <td><%= log.message %></td>
            <td><%= formatDate(log.created_at) %></td>
          </tr>
        <% }) %>
      </tbody>
    </table>
  </div>
</main>

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