<script lang="ts">
  import { onMount } from 'svelte';
  import confetti from 'canvas-confetti';

  export let firstName: string;
  export let petName: string;
  export let email: string;
  export let enquiryType: 'booking' | 'general' = 'booking';
  export let onClose: () => void;
  const gwGreenHex = '#213021';

  $: isGeneralEnquiry = enquiryType === 'general';

  onMount(() => {
    const duration = 3200;
    const end = Date.now() + duration;

    const frame = () => {
      confetti({
        particleCount: 3,
        angle: 60,
        spread: 65,
        origin: { x: 0, y: 0.75 },
        colors: ['#FFD100', gwGreenHex, '#ffffff', '#7aaa7a', '#ffeaa0'],
        gravity: 0.9,
        scalar: 1.1,
      });
      confetti({
        particleCount: 3,
        angle: 120,
        spread: 65,
        origin: { x: 1, y: 0.75 },
        colors: ['#FFD100', gwGreenHex, '#ffffff', '#7aaa7a', '#ffeaa0'],
        gravity: 0.9,
        scalar: 1.1,
      });

      if (Date.now() < end) {
        requestAnimationFrame(frame);
      }
    };

    frame();
  });
</script>

<div
  class="modal-backdrop"
  role="dialog"
  aria-modal="true"
  aria-label={isGeneralEnquiry ? 'Enquiry confirmed' : 'Booking confirmed'}
  on:click|self={onClose}
  on:keydown={(e) => e.key === 'Escape' && onClose()}
  tabindex="-1"
>
  <div class="modal-card">
    <button class="modal-close" type="button" aria-label="Close" on:click={onClose}>
      &#x2715;
    </button>

    <div class="modal-paw" aria-hidden="true">🐾</div>

    <h2 class="modal-heading">
      {#if isGeneralEnquiry}
        Your message is with us!
      {:else}
        You&rsquo;re on our radar!
      {/if}
    </h2>

    {#if isGeneralEnquiry}
      <p class="modal-body">
        Thanks, <strong>{firstName}</strong>! We&rsquo;ve sent a confirmation to
        <strong>{email}</strong> and Aless will be in touch soon about your enquiry.
      </p>
    {:else}
      <p class="modal-body">
        Thanks, <strong>{firstName}</strong>! We&rsquo;ve sent a confirmation to
        <strong>{email}</strong> and Aless will be in touch soon to arrange a
        Meet &amp; Greet with <strong>{petName}</strong>.
      </p>
    {/if}

    <div class="modal-divider"></div>

    <p class="modal-sub">
      {#if isGeneralEnquiry}
        We aim to reply within 1 business day.
      {:else}
        In the meantime, feel free to follow along on Instagram for daily walks and happy dogs!
      {/if}
    </p>

    <button class="modal-btn" type="button" on:click={onClose}>
      Sounds great!
    </button>
  </div>
</div>

<style>
  .modal-backdrop {
    position: fixed;
    inset: 0;
    z-index: 1000;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 16px;
    background: rgba(10, 20, 10, 0.55);
    backdrop-filter: blur(6px);
    -webkit-backdrop-filter: blur(6px);
    animation: backdrop-in 0.25s ease;
  }

  .modal-card {
    position: relative;
    width: 100%;
    max-width: 480px;
    padding: 52px 48px 44px;
    background: #fff;
    border-radius: 24px;
    box-shadow: 0 24px 80px rgba(10, 20, 10, 0.22);
    text-align: center;
    animation: card-in 0.35s cubic-bezier(0.22, 1, 0.36, 1);
  }

  .modal-close {
    position: absolute;
    top: 18px;
    right: 20px;
    width: 32px;
    height: 32px;
    border: none;
    border-radius: 50%;
    background: #f2f2f0;
    color: #888;
    font-size: 14px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.15s ease, color 0.15s ease;
  }

  .modal-close:hover {
    background: #e8e8e4;
    color: #333;
  }

  .modal-paw {
    font-size: 52px;
    line-height: 1;
    margin-bottom: 20px;
    animation: bounce-in 0.5s cubic-bezier(0.22, 1, 0.36, 1) 0.15s both;
  }

  .modal-heading {
    margin: 0 0 14px;
    font-size: 26px;
    font-weight: 700;
    color: var(--gw-green);
    line-height: 1.2;
  }

  .modal-body {
    margin: 0;
    font-size: 15px;
    color: #555;
    line-height: 1.7;
  }

  .modal-divider {
    width: 48px;
    height: 3px;
    background: #FFD100;
    border-radius: 999px;
    margin: 28px auto;
  }

  .modal-sub {
    margin: 0 0 32px;
    font-size: 14px;
    color: #888;
    line-height: 1.6;
  }

  .modal-btn {
    display: inline-block;
    padding: 14px 36px;
    background: var(--gw-green);
    color: #FFD100;
    font-size: 15px;
    font-weight: 600;
    border: none;
    border-radius: 999px;
    cursor: pointer;
    transition: background 0.18s ease, transform 0.15s ease;
  }

  .modal-btn:hover {
    background: #2e4a2e;
    transform: translateY(-1px);
  }

  .modal-btn:active {
    transform: translateY(0);
  }

  @keyframes backdrop-in {
    from { opacity: 0; }
    to   { opacity: 1; }
  }

  @keyframes card-in {
    from { opacity: 0; transform: scale(0.88) translateY(16px); }
    to   { opacity: 1; transform: scale(1) translateY(0); }
  }

  @keyframes bounce-in {
    from { opacity: 0; transform: scale(0.4); }
    to   { opacity: 1; transform: scale(1); }
  }
</style>
