<script lang="ts">
  import { createEventDispatcher } from 'svelte';
  import Icon from '$lib/components/Icon.svelte';

  export let email = '';

  const dispatch = createEventDispatcher<{ logout: void }>();

  let loggingOut = false;

  async function logout() {
    loggingOut = true;
    try {
      const token = window.localStorage.getItem('gw_onboarding_session') ?? '';
      if (token) {
        await fetch('/api/auth/logout', {
          method: 'POST',
          headers: { Authorization: `Bearer ${token}` },
        }).catch(() => { /* ignore network errors on logout */ });
      }
    } finally {
      try { window.localStorage.removeItem('gw_onboarding_session'); } catch { /* ignore */ }
      loggingOut = false;
      dispatch('logout');
    }
  }
</script>

<footer class="ob-footer">
  <div class="ob-footer-inner">
    <a href="https://goodwalk.co.nz" class="ob-footer-back">
      <Icon name="fas fa-arrow-left" />
      Back to main site
    </a>
    {#if email}
      <span class="ob-footer-email">Signed in as {email}</span>
    {/if}
    <button type="button" class="ob-footer-logout" on:click={logout} disabled={loggingOut}>
      <Icon name="fas fa-right-from-bracket" />
      {loggingOut ? 'Signing out…' : 'Sign out'}
    </button>
  </div>
</footer>

<style>
  .ob-footer {
    background: #213021;
    margin-top: auto;
  }

  .ob-footer-inner {
    max-width: 1120px;
    margin: 0 auto;
    padding: 0 28px;
    height: 52px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
  }

  .ob-footer-back {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    font-family: var(--font-head);
    font-size: 13px;
    font-weight: 700;
    color: rgba(255, 255, 255, 0.65);
    text-decoration: none;
    transition: color 0.15s;
  }

  .ob-footer-back:hover {
    color: #fff;
  }

  .ob-footer-email {
    margin-left: auto;
    min-width: 0;
    color: rgba(255, 255, 255, 0.62);
    font-size: 12px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .ob-footer-logout {
    display: inline-flex;
    align-items: center;
    gap: 7px;
    padding: 7px 14px;
    border-radius: 999px;
    border: 1px solid rgba(255, 255, 255, 0.15);
    background: transparent;
    font-family: var(--font-head);
    font-size: 12px;
    font-weight: 700;
    color: rgba(255, 255, 255, 0.7);
    cursor: pointer;
    transition: background 0.15s, color 0.15s;
  }

  .ob-footer-logout:hover {
    background: rgba(255, 255, 255, 0.1);
    color: #fff;
  }

  @media (max-width: 768px) {
    .ob-footer-inner {
      height: auto;
      padding: 12px 18px;
      flex-wrap: wrap;
      align-items: center;
      justify-content: space-between;
      row-gap: 10px;
    }

    .ob-footer-email {
      order: 3;
      flex: 0 0 100%;
      width: 100%;
      margin-left: 0;
      white-space: normal;
      overflow: visible;
      text-overflow: clip;
      line-height: 1.4;
    }

    .ob-footer-back,
    .ob-footer-logout {
      flex: 0 0 auto;
    }

    .ob-footer-logout {
      margin-left: auto;
    }
  }
</style>
