/* ==========================================================================
   ServiceDetails.jsx | 10 Service details
   Accessible tabs (arrow keys, Home, End) with one panel per service.
   Task lists are examples only; the agreed quote sets the final scope.
   ========================================================================== */

function MCServiceDetails({ active, onChange, onQuote }) {
  const tabRefs = React.useRef({});
  const activeIndex = Math.max(0, MC_SERVICES.findIndex((s) => s.id === active));

  const focusTab = (index) => {
    const service = MC_SERVICES[index];
    onChange(service.id);
    const el = tabRefs.current[service.id];
    if (el) el.focus();
  };

  const handleKeyDown = (event, index) => {
    const count = MC_SERVICES.length;
    let next = null;
    if (event.key === 'ArrowRight' || event.key === 'ArrowDown') next = (index + 1) % count;
    if (event.key === 'ArrowLeft' || event.key === 'ArrowUp') next = (index - 1 + count) % count;
    if (event.key === 'Home') next = 0;
    if (event.key === 'End') next = count - 1;
    if (next !== null) {
      event.preventDefault();
      focusTab(next);
    }
  };

  return (
    <section className="mc-section mc-details" id="details" aria-labelledby="mc-details-title">
      <div className="mc-wrap">
        <div className="mc-details__panel mc-on-band">
          <header className="mc-head" data-reveal>
            <MCEyebrow>Service details</MCEyebrow>
            <h2 className="mc-h2" id="mc-details-title">
              A closer look at each clean.
            </h2>
            <p className="mc-lede">Choose a service to see the kinds of tasks it can include.</p>
          </header>

          <div className="mc-details__tabs" role="tablist" aria-label="Cleaning services">
            {MC_SERVICES.map((service, index) => {
              const selected = index === activeIndex;
              return (
                <button
                  key={service.id}
                  ref={(el) => {
                    tabRefs.current[service.id] = el;
                  }}
                  type="button"
                  role="tab"
                  id={`mc-tab-${service.id}`}
                  className={`mc-details__tab mc-acc-${service.accent}`}
                  aria-selected={selected}
                  aria-controls={`mc-tabpanel-${service.id}`}
                  tabIndex={selected ? 0 : -1}
                  onClick={() => onChange(service.id)}
                  onKeyDown={(event) => handleKeyDown(event, index)}
                >
                  <span className="mc-details__tabicon" aria-hidden="true">
                    <MCIcon name={service.icon} />
                  </span>
                  {service.name}
                </button>
              );
            })}
          </div>

          {MC_SERVICES.map((service, index) => {
            const selected = index === activeIndex;
            const handleQuote = (event) => {
              event.preventDefault();
              onQuote(service.id);
            };
            return (
              <div
                key={service.id}
                role="tabpanel"
                id={`mc-tabpanel-${service.id}`}
                aria-labelledby={`mc-tab-${service.id}`}
                className={`mc-details__tabpanel mc-acc-${service.accent}`}
                hidden={!selected}
                tabIndex={0}
              >
                {selected ? (
                  <div className="mc-details__content">
                    <div className="mc-details__text">
                      <h3 className="mc-details__name">{service.name}</h3>
                      <p className="mc-details__desc">{service.desc}</p>
                      <p className="mc-details__good">
                        <strong>Good for</strong> {service.goodFor}
                      </p>

                      <h4 className="mc-details__subhead">Tasks this service can include</h4>
                      <ul className="mc-details__tasks" role="list">
                        {service.tasks.map((task) => (
                          <li key={task}>
                            <MCIcon name="sparkle" />
                            {task}
                          </li>
                        ))}
                      </ul>
                      {service.extras ? (
                        <p className="mc-details__extra">{service.extras}</p>
                      ) : null}

                      <div className="mc-details__notes">
                        {service.note ? (
                          <p className="mc-details__note">
                            <MCIcon name="info" />
                            <span>{service.note}</span>
                          </p>
                        ) : null}
                        <p className="mc-details__note">
                          <MCIcon name="magnifier" />
                          <span>
                            These are examples, not a fixed checklist. Your final scope depends on the service you
                            choose and the quote we agree on.
                          </span>
                        </p>
                      </div>

                      <div className="mc-details__actions">
                        <a
                          href="#quote"
                          className="mc-btn"
                          onClick={handleQuote}
                          aria-label={`Get a free quote for ${service.name}`}
                        >
                          Get a free quote
                          <MCIcon name="sparkle" className="mc-btn__spark" />
                        </a>
                      </div>
                    </div>
                    <div className="mc-details__media">
                      <MCImageSlot photoKey={service.photo} align="top" sizes="(max-width: 760px) 100vw, 35vw" />
                    </div>
                  </div>
                ) : null}
              </div>
            );
          })}
        </div>
      </div>
    </section>
  );
}

Object.assign(window, { MCServiceDetails });
