<script lang="ts">
  import { api } from '$lib/api';
  import { adminSession, sessionHydrated } from '$lib/session';

  let { data } = $props();

  let email = $state('admin@lean101.local');
  let password = $state('lean101-admin');
  let isLoggingIn = $state(false);
  let loginError = $state('');

  function formatDate(value: string | null | undefined) {
    if (!value) {
      return 'No preview generated';
    }

    return new Intl.DateTimeFormat('en-NZ', {
      day: 'numeric',
      month: 'short',
      year: 'numeric',
      hour: 'numeric',
      minute: '2-digit'
    }).format(new Date(value));
  }

  async function handleLogin(event: SubmitEvent) {
    event.preventDefault();
    loginError = '';
    isLoggingIn = true;

    try {
      const session = await api.adminLogin(email, password);
      adminSession.set(session);
    } catch (error) {
      loginError = error instanceof Error ? error.message : 'Unable to sign in';
    } finally {
      isLoggingIn = false;
    }
  }

  const totalUsers = $derived(data.clients.reduce((sum, client) => sum + client.users.length, 0));
  const totalFeatures = $derived(data.clients.reduce((sum, client) => sum + client.enabled_feature_count, 0));
</script>

<section class="hero-card">
  <div class="hero-copy">
    <p class="eyebrow">Lean 101 Admin Panel</p>
    <h2>Separate operator login and client access controls from the Hunter Premium Produce workspace.</h2>
    <p>Use this admin surface for internal access changes, export validation, and operator-only workflows.</p>
  </div>

  <div class="hero-stats">
    <article>
      <span>Managed clients</span>
      <strong>{data.clients.length}</strong>
    </article>
    <article>
      <span>Total users</span>
      <strong>{totalUsers}</strong>
    </article>
    <article>
      <span>Enabled features</span>
      <strong>{totalFeatures}</strong>
    </article>
  </div>
</section>

{#if !$sessionHydrated}
  <section class="signin-card loading-card">
    <div class="signin-copy">
      <p class="eyebrow">Checking Session</p>
      <h3>Restoring the Lean 101 admin session before deciding whether sign-in is needed.</h3>
      <p>The admin sign-in form only appears when no saved operator session is available.</p>
    </div>
  </section>
{:else if !$adminSession}
  <section class="signin-card">
    <div class="signin-copy">
      <p class="eyebrow">Admin Sign-In</p>
      <h3>Authenticate here to unlock the admin navigation and client access controls.</h3>
      <p>The public client workspace no longer exposes this operator sign-in.</p>
    </div>

    <form class="signin-form" onsubmit={handleLogin}>
      <input bind:value={email} type="email" autocomplete="username" placeholder="Email" />
      <input bind:value={password} type="password" autocomplete="current-password" placeholder="Password" />
      <button class="primary-button" type="submit" disabled={isLoggingIn}>
        {isLoggingIn ? 'Signing in...' : 'Sign In'}
      </button>
    </form>
    <div class="signin-meta">
      {#if loginError}
        <strong>{loginError}</strong>
      {/if}
    </div>
  </section>
{:else}
  <section class="live-banner">
    <div>
      <p class="eyebrow">Session Active</p>
      <h3>{$adminSession.name} is signed in to the Lean 101 Admin Panel.</h3>
      <p>Open the client access workspace to manage users, feature flags, and the Power BI export preview.</p>
    </div>

    <div class="live-actions">
      <a class="primary-button" href="/admin/client-access">Open Client Access</a>
      <a class="secondary-button" href="/">View Hunter workspace</a>
    </div>
  </section>
{/if}

<section class="detail-grid">
  <article class="surface-card">
    <div class="card-toolbar">
      <div>
        <p class="eyebrow">Scope</p>
        <h3>What belongs in admin</h3>
      </div>
    </div>

    <div class="bullet-list">
      <article>
        <strong>Client access control</strong>
        <span>Manage new users, existing users, and feature access by client.</span>
      </article>
      <article>
        <strong>Power BI export validation</strong>
        <span>Verify the live export payload after each access change.</span>
      </article>
      <article>
        <strong>Operator-only sign-in</strong>
        <span>Keep internal authentication separate from the client workspace at `/`.</span>
      </article>
    </div>
  </article>

  <article class="surface-card">
    <div class="card-toolbar">
      <div>
        <p class="eyebrow">Preview Snapshot</p>
        <h3>Current export summary</h3>
        <p>Last generated {formatDate(data.exportPreview.generated_at)}</p>
      </div>
    </div>

    <div class="preview-stats">
      <article>
        <span>Client rows</span>
        <strong>{data.exportPreview.client_rows.length}</strong>
      </article>
      <article>
        <span>User rows</span>
        <strong>{data.exportPreview.user_rows.length}</strong>
      </article>
      <article>
        <span>Feature rows</span>
        <strong>{data.exportPreview.feature_rows.length}</strong>
      </article>
    </div>
  </article>
</section>

<style>
  h2,
  h3,
  p {
    margin: 0;
  }

  .eyebrow {
    color: #6e8576;
    font-size: 0.78rem;
    font-weight: 700;
    letter-spacing: 0.08em;
    text-transform: uppercase;
  }

  .hero-card,
  .signin-card,
  .live-banner,
  .surface-card {
    border: 1px solid rgba(34, 54, 45, 0.1);
    border-radius: 1.35rem;
    background: rgba(255, 255, 255, 0.84);
    box-shadow: none;
  }

  .hero-card,
  .signin-card,
  .live-banner,
  .detail-grid {
    margin-bottom: 1.25rem;
  }

  .hero-card,
  .signin-card,
  .live-banner,
  .surface-card {
    padding: 1.25rem;
  }

  .hero-card,
  .hero-stats,
  .detail-grid,
  .preview-stats {
    display: grid;
    gap: 1rem;
  }

  .hero-card {
    grid-template-columns: minmax(0, 1.2fr) 0.85fr;
    align-items: end;
  }

  .hero-copy h2 {
    margin: 0.35rem 0 0.45rem;
    max-width: 16ch;
    font-size: clamp(2rem, 3vw, 2.6rem);
    line-height: 1.02;
  }

  .hero-copy p:last-child,
  .signin-copy p:last-child,
  .live-banner p:last-child,
  .bullet-list span,
  .preview-stats span,
  .card-toolbar p {
    color: #5f7266;
  }

  .hero-stats,
  .preview-stats {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .hero-stats article,
  .preview-stats article {
    padding: 1rem;
    border-radius: 1rem;
    background: rgba(243, 247, 241, 0.95);
    border: 1px solid rgba(34, 54, 45, 0.08);
  }

  .hero-stats span,
  .preview-stats span {
    display: block;
    font-size: 0.84rem;
  }

  .hero-stats strong,
  .preview-stats strong {
    display: block;
    margin-top: 0.35rem;
    font-size: 1.8rem;
  }

  .signin-card {
    display: grid;
    grid-template-columns: 1.2fr 1fr auto;
    gap: 1rem;
    align-items: center;
  }

  .loading-card {
    grid-template-columns: 1fr;
    min-height: 10rem;
  }

  .signin-copy h3,
  .live-banner h3,
  .card-toolbar h3 {
    margin: 0.28rem 0 0.35rem;
    font-size: 1.2rem;
  }

  .signin-form {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 0.75rem;
  }

  .signin-form input {
    width: 100%;
    padding: 0.9rem 0.95rem;
    border: 1px solid rgba(34, 54, 45, 0.12);
    border-radius: 0.85rem;
    background: rgba(248, 251, 249, 0.92);
  }

  .signin-meta {
    display: grid;
    gap: 0.35rem;
    justify-items: end;
    color: #5f7266;
    font-size: 0.9rem;
  }

  .signin-meta strong {
    color: #b33636;
  }

  .primary-button,
  .secondary-button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border-radius: 0.85rem;
    padding: 0.85rem 1rem;
    font-weight: 600;
    text-decoration: none;
  }

  .primary-button {
    border: none;
    color: #fff;
    background: var(--color-brand);
    box-shadow: none;
  }

  .secondary-button {
    border: 1px solid rgba(34, 54, 45, 0.12);
    color: #203028;
    background: rgba(255, 255, 255, 0.9);
  }

  .primary-button:disabled {
    opacity: 0.72;
    cursor: wait;
  }

  .live-banner {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
  }

  .live-actions {
    display: flex;
    gap: 0.75rem;
    flex-wrap: wrap;
  }

  .detail-grid {
    grid-template-columns: minmax(0, 1fr) minmax(320px, 0.9fr);
  }

  .card-toolbar {
    margin-bottom: 1rem;
  }

  .bullet-list {
    display: grid;
    gap: 0.9rem;
  }

  .bullet-list article {
    padding: 0.95rem 1rem;
    border-radius: 1rem;
    background: rgba(243, 247, 241, 0.95);
    border: 1px solid rgba(34, 54, 45, 0.08);
  }

  .bullet-list strong,
  .preview-stats strong {
    font-weight: 700;
  }

  .bullet-list span {
    display: block;
    margin-top: 0.25rem;
  }

  @media (max-width: 1120px) {
    .hero-card,
    .signin-card,
    .detail-grid,
    .hero-stats,
    .preview-stats {
      grid-template-columns: 1fr;
    }
  }

  @media (max-width: 760px) {
    .live-banner {
      flex-direction: column;
      align-items: flex-start;
    }

    .signin-form {
      grid-template-columns: 1fr;
    }
  }
</style>
