<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 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>
    </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 class="footer-action footer-panel footer-panel-accent">
      <p class="footer-col-label">Get Started</p>
      <h3 class="footer-action-title">Ready when you are</h3>
      <p class="footer-action-copy">Questions, pricing, or your first Meet &amp; Greet. Start here and we’ll reply within 24 hours.</p>
      <a href="/contact-us" class="footer-book-btn">
        Contact Us
        <Icon name="fas fa-arrow-right" />
      </a>
      <p class="footer-book-note">Friendly, no-pressure first step</p>
      <a
        href="https://g.page/r/CUsvrWPhkYrAEB0/"
        target="_blank"
        rel="noopener"
        class="footer-reviews"
      >
        <img
          class="footer-google-logo"
          src="/images/google-g-logo.svg"
          alt=""
          width="16"
          height="17"
        />
        <span>30+ five-star Google reviews</span>
      </a>

      {#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>

  <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>
  </div>
</footer>
