<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>
    <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-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 {
      padding: 0 18px;
    }
  }
</style>
