/* ==========================================================================
   FAQ.jsx | 11 FAQ
   Accessible accordion: real buttons with aria-expanded / aria-controls.
   Unconfirmed business details are shown as labelled placeholders.
   ========================================================================== */

const MC_FAQS = [
  {
    id: 'supplies',
    q: 'Do you bring your own cleaning supplies and equipment?',
    a: [
      'Yes! Our cleaners arrive with the necessary cleaning products and equipment to get the job done.',
      'If you prefer us to use your own products or equipment, just let us know before your booking.',
    ],
  },
  {
    id: 'booking',
    q: 'How do I book a cleaning service?',
    a: [
      'Booking with Dusty Cleaning is easy!',
      'Simply request a quote through our website or contact us directly. We’ll discuss your cleaning needs, provide a quote and arrange a convenient time for your service.',
    ],
  },
  {
    id: 'extras',
    q: 'Can I request additional cleaning tasks?',
    a: [
      'Absolutely! We can customise your cleaning service to suit your needs.',
      'Additional tasks such as oven cleaning, fridge cleaning, interior window cleaning and other extras may be available upon request. Please let us know in advance so we can include them in your quote.',
    ],
  },
  {
    id: 'trust',
    q: 'Are your cleaners trustworthy and reliable?',
    a: [
      'We understand that inviting someone into your home or workplace requires trust.',
      'Our team is carefully selected, and we prioritise reliability, professionalism and attention to detail. Police checks are also part of our commitment to providing a trustworthy service.',
    ],
  },
  {
    id: 'rates',
    q: 'Do you charge extra for weekends or public holidays?',
    a: [
      'Weekend and public holiday rates may differ from our standard weekday rates.',
      'Any applicable surcharges will be communicated before your booking is confirmed.',
    ],
  },
  {
    id: 'satisfaction',
    q: 'What if I’m not happy with the cleaning?',
    a: [
      'Your satisfaction matters to us!',
      'If something hasn’t been cleaned to the agreed standard, please contact us within 24 hours of your service. We’ll review your feedback and work with you to find a suitable solution.',
    ],
  },
  {
    id: 'reschedule',
    q: 'Can I cancel or reschedule my booking?',
    a: [
      'Of course! We understand that plans can change.',
      'We kindly ask for at least 24 hours’ notice for cancellations or rescheduling. Late cancellations may incur a fee, depending on the service booked and our cancellation policy.',
    ],
  },
  {
    id: 'pets',
    q: 'Are you pet-friendly?',
    a: [
      'Absolutely! We love furry friends.',
      'Our team is happy to clean homes with pets. We simply ask that you let us know about any pets when booking so we can make sure everyone stays safe and comfortable.',
    ],
  },
  {
    id: 'included',
    q: 'What’s included in my cleaning service?',
    a: [
      'Every service is tailored to the type of cleaning you’ve booked.',
      'Our services include:',
      {
        list: [
          ['House Cleaning', 'Regular cleaning to keep your home fresh, tidy and comfortable.'],
          ['Housekeeping', 'Help with everyday household tasks to keep things organised.'],
          ['End of Lease Cleaning', 'A detailed clean to help prepare your property for inspection.'],
          ['Office Cleaning', 'Professional cleaning to maintain a clean and welcoming workspace.'],
        ],
      },
      'Your quote will outline the tasks included in your service.',
    ],
  },
  {
    id: 'home',
    q: 'Do I need to be home during the cleaning?',
    a: [
      'Not at all!',
      'You can stay home or go about your day while we take care of the cleaning. If you’re not going to be home, simply arrange access with us beforehand.',
      'Your property and privacy will always be treated with care.',
    ],
  },
  {
    id: 'payment',
    q: 'How can I pay for my cleaning service?',
    a: [
      'We accept convenient payment methods, including bank transfer and other available payment options.',
      'Payment terms will be confirmed when your booking is arranged.',
    ],
  },
  {
    id: 'same-cleaner',
    q: 'Can I request the same cleaner for every visit?',
    a: [
      'Yes! For regular cleaning services, we’ll do our best to arrange the same cleaner whenever possible.',
      'Having a familiar cleaner helps us understand your preferences and maintain consistency. If your regular cleaner is unavailable, we’ll let you know and discuss alternative arrangements.',
    ],
  },
];

/* Renders an answer: strings become paragraphs, { list } becomes a bullet list. */
function MCFAQAnswer({ parts }) {
  return (
    <div className="mc-faq__answer">
      {parts.map((part, index) =>
        typeof part === 'string' ? (
          <p key={index}>{part}</p>
        ) : (
          <ul key={index} className="mc-faq__list-items">
            {part.list.map(([term, text]) => (
              <li key={term}>
                <strong>{term}:</strong> {text}
              </li>
            ))}
          </ul>
        )
      )}
    </div>
  );
}

function MCFAQItem({ item, open, onToggle }) {
  const buttonId = `mc-faq-btn-${item.id}`;
  const panelId = `mc-faq-panel-${item.id}`;
  return (
    <li className={`mc-faq__item${open ? ' is-open' : ''}`}>
      <h3 className="mc-faq__q">
        <button
          type="button"
          id={buttonId}
          className="mc-faq__btn"
          aria-expanded={open}
          aria-controls={panelId}
          onClick={onToggle}
        >
          {item.q}
          <span className="mc-faq__icon" aria-hidden="true">
            <MCIcon name="plus" />
          </span>
        </button>
      </h3>
      <div className="mc-faq__panel" id={panelId} role="region" aria-labelledby={buttonId}>
        <div className="mc-faq__inner">
          <MCFAQAnswer parts={item.a} />
        </div>
      </div>
    </li>
  );
}

function MCFAQ({ onIntent }) {
  const [faqOpen, setFaqOpen] = React.useState({ [MC_FAQS[0].id]: true });

  const toggle = (id) => setFaqOpen((state) => ({ ...state, [id]: !state[id] }));

  const handleAsk = (event) => {
    event.preventDefault();
    onIntent('quote');
  };

  return (
    <section className="mc-section mc-faq" id="faq" aria-labelledby="mc-faq-title">
      <div className="mc-wrap mc-faq__grid">
        <div className="mc-faq__aside">
          <header className="mc-head" data-reveal>
            <MCEyebrow>Good to know</MCEyebrow>
            <h2 className="mc-h2" id="mc-faq-title">
              A few things people ask before booking.
            </h2>
          </header>
          <div className="mc-faq__help" data-reveal>
            <MCDusty decorative />
            <p>
              Still wondering about something?{' '}
              <a href="#quote" className="mc-link" onClick={handleAsk}>
                Ask us in the quote form
              </a>
              .
            </p>
          </div>
        </div>

        <ul className="mc-faq__list" role="list" data-reveal>
          {MC_FAQS.map((item) => (
            <MCFAQItem key={item.id} item={item} open={Boolean(faqOpen[item.id])} onToggle={() => toggle(item.id)} />
          ))}
        </ul>
      </div>
    </section>
  );
}

Object.assign(window, { MCFAQ, MCFAQItem, MCFAQAnswer });
