<script lang="ts">
  import Icon from '$lib/components/Icon.svelte';
  import type { FooterContent, LinkItem } from '$lib/types';
  import { locationPages } from '$lib/content/locations';

  export let footer: FooterContent;


  const socialLinks: LinkItem[] = [
    { label: 'Instagram', href: 'https://www.instagram.com/goodwalk.nz/', external: true },
    { label: 'Facebook', href: 'https://facebook.com/goodwalk.nz', external: true },
    { label: 'Google', href: 'https://g.page/r/CUsvrWPhkYrAEB0', external: true }
  ];

  const aboutLink: LinkItem = { label: 'About Us', href: '/about' };

  function scrollToTop() {
    window.scrollTo({ top: 0, behavior: 'smooth' });
  }

  function withAboutLink(links: LinkItem[]) {
    if (links.some((link) => link.href === aboutLink.href || link.label === aboutLink.label)) {
      return links;
    }

    const contactIndex = links.findIndex((link) => link.href === '/contact-us');

    if (contactIndex === -1) {
      return [...links, aboutLink];
    }

    return [...links.slice(0, contactIndex), aboutLink, ...links.slice(contactIndex)];
  }

  $: navigationLinks = withAboutLink(footer.navigationLinks);
</script>

<footer>
  <div class="footer-inner">
    <div class="footer-brand">
      <enhanced:img
        src="$lib/images/goodwalk-auckland-dog-walking-logo.png"
        alt="Goodwalk – Auckland dog walking service logo"
        class="footer-logo"
        loading="lazy"
        decoding="async"
      />
      <p>{footer.brandText}</p>
      <div class="social-links">
        <a href={socialLinks[0].href} target="_blank" rel="noopener" aria-label="Instagram">
          <Icon name="fab fa-instagram" />
        </a>
        <a href={socialLinks[1].href} target="_blank" rel="noopener" aria-label="Facebook">
          <Icon name="fab fa-facebook-f" />
        </a>
        <a href={socialLinks[2].href} target="_blank" rel="noopener" aria-label="Google">
          <Icon name="fab fa-google" />
        </a>
      </div>

      {#if footer.email || footer.phone}
        <div class="footer-contact">
          {#if footer.email}
            <a href="mailto:{footer.email}" class="footer-contact-link">
              <Icon name="fas fa-envelope" />
              {footer.email}
            </a>
          {/if}
          {#if footer.phone}
            <a href="tel:{footer.phone.replace(/[^0-9+]/g, '')}" class="footer-contact-link">
              <Icon name="fas fa-phone" />
              {footer.phone}
            </a>
          {/if}
        </div>
      {/if}
    </div>

    <div class="footer-explore">
      <p class="footer-col-label">Explore Goodwalk</p>
      <ul class="footer-nav">
        {#each navigationLinks as link}
          <li>
            <a
              href={link.href}
              target={link.external ? '_blank' : undefined}
              rel={link.external ? 'noopener' : undefined}
            >
              {link.label}
            </a>
          </li>
        {/each}
      </ul>
    </div>

    <div class="footer-locations">
      <p class="footer-col-label">Areas we serve</p>
      <ul class="footer-nav">
        {#each locationPages as loc}
          <li><a href="/locations/{loc.slug}">{loc.suburb}</a></li>
        {/each}
      </ul>
    </div>
  </div>

  <div class="footer-bottom">
    <span>{footer.copyright}</span>
    <nav class="footer-legal">
      <a href="/terms-and-conditions">Terms &amp; Conditions</a>
      <a href="/privacy-policy">Privacy Policy</a>
    </nav>
    <button type="button" class="footer-back-top" aria-label="Back to top" on:click={scrollToTop}>
      ↑ Back to top
    </button>
  </div>
</footer>
