// script.js
const stripe = Stripe('pk_test_YOUR_PUBLIC_KEY'); // Replace with your Stripe public key
const form = document.getElementById('gift-form');
const results = document.getElementById('results');
const giftList = document.getElementById('gift-list');

form.addEventListener('submit', async (e) => {
  e.preventDefault();
  
  // Collect inputs
  const inputs = {
    age: document.getElementById('age').value,
    gender: document.getElementById('gender').value,
    interests: document.getElementById('interests').value,
    occasion: document.getElementById('occasion').value,
    budget: document.getElementById('budget').value
  };

  // Step 1: Initiate Stripe payment
  const response = await fetch('https://findmygift.net/charge', { // Update with your API Gateway URL
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ amount: 50, currency: 'usd', description: 'Gift Search' })
  });
  const { sessionId } = await response.json();
  
  // Step 2: Redirect to Stripe Checkout
  stripe.redirectToCheckout({ sessionId });
});

// Handle results after payment (called via URL param on redirect)
window.onload = async () => {
  const urlParams = new URLSearchParams(window.location.search);
  const sessionId = urlParams.get('session_id');
  if (sessionId) {
    // Step 3: Fetch gift suggestions
    const response = await fetch('https://findmygift.net/search', { // Update with your API Gateway URL
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ sessionId })
    });
    const { suggestions } = await response.json();

    // Step 4: Display 7 links
    results.classList.remove('results-hidden');
    giftList.innerHTML = suggestions.map(item => `
      <li class="p-4 bg-white rounded shadow">
        <a href="${item.link}" target="_blank" class="text-teal-500 hover:underline">${item.name}</a> - $${item.price}
      </li>
    `).join('');
  }
};