/** Section markup of /contact, keyed by CMS section key (markup unchanged from the static page). */
import type { ReactNode } from "react";
import type { ContactContent, CtaButtonsContent, FeatureCardsContent, MapEmbedContent, PageHeroContent } from "@/app/(site)/_lib/pageContent";
import { LinkRow, PageHero, contactBtn } from "../blocks";
import { SplitTitle } from "../parts";
import type { SectionRenderers } from "../render";
import { InquiryForm } from "@/app/(site)/_components/InquiryForm";

const HL = "text-[#0056D2]";
const FIELD = "contact-field w-full px-[14px] py-3 text-sm rounded-[6px] outline-none font-[inherit]";
const SELECT = "contact-field contact-select w-full px-[14px] py-3 text-sm rounded-[6px] outline-none font-[inherit]";
const VALUE = "contact-info-value text-sm font-bold leading-[1.5]";

/** A contact detail row: icon and its text column. */
function Row({ icon, children }: { icon: string; children: ReactNode }) {
  return (
    <div className="flex items-start gap-[14px]">
      <div className="contact-info-icon w-6 h-6 min-w-[24px] flex items-center justify-center mt-0.5 shrink-0">
        <img src={`/assets/images/contact/icon-${icon}.png`} alt="" width="22" height="22" decoding="async" />
      </div>
      {children}
    </div>
  );
}

/** "+31 58 123 4567" → "tel:+31581234567". */
const telHref = (phone: string) => `tel:${phone.replace(/[^\d+]/g, "")}`;

export const CONTACT_SECTIONS: SectionRenderers = {
  "page-hero": (c: PageHeroContent) => <PageHero c={c} width="max-w-[820px]" h1="text-[clamp(26px,3.8vw,38px)]" />,
  /** Details and the inquiry form (options from the CMS; sent to POST /public/inquiries by InquiryForm). */
  contact: (c: ContactContent) => (
    <section className="bg-white py-12 md:py-16 lg:py-20 overflow-x-clip" id="inquiry-form">
      <div className="w-full max-w-[1180px] mx-auto px-6">
        <div className="grid grid-cols-1 lg:grid-cols-2 gap-10 lg:gap-14 items-start [&amp;>*]:min-w-0">
          <div data-reveal="left">
            <h2 className="contact-info-heading text-[clamp(20px,2.4vw,26px)] font-black leading-tight tracking-[-0.01em]">
              <SplitTitle title={c.title} hl={HL} />
            </h2>
            {c.body && <p className="contact-info-label mt-4 text-sm leading-[1.7] max-w-[440px]">{c.body}</p>}
            <div className="mt-8 flex flex-col gap-6">
              {c.phone && (
                <Row icon="phone">
                  <div className="flex flex-col gap-1">
                    <span className="contact-info-label text-[13px]">Phone Number:</span>{" "}
                    <a href={telHref(c.phone)} className={VALUE}>
                      {c.phone}
                    </a>
                  </div>
                </Row>
              )}
              {c.emails?.length ? (
                <Row icon="mail">
                  <div className="flex-1 min-w-0">
                    <span className="contact-info-label text-[13px] block mb-3">Email:</span>{" "}
                    <div className="contact-email-box rounded-[8px] p-4 md:p-5">
                      <ul className="flex flex-col gap-3">
                        {c.emails.map((e, i) => (
                          <li key={i} className="contact-email-row flex flex-col sm:flex-row sm:flex-wrap sm:items-center sm:justify-between gap-1">
                            <span className="contact-info-label text-[13px] min-w-0">{`${e.label}:`}</span>{" "}
                            <a href={`mailto:${e.email}`} className="contact-email-link text-[13px]">
                              {e.email}
                            </a>
                          </li>
                        ))}
                      </ul>
                    </div>
                  </div>
                </Row>
              ) : (
                c.email && (
                  <Row icon="mail">
                    <div className="flex flex-col gap-1">
                      <span className="contact-info-label text-[13px]">Email:</span>{" "}
                      <a href={`mailto:${c.email}`} className={VALUE}>
                        {c.email}
                      </a>
                    </div>
                  </Row>
                )
              )}
              {c.address && (
                <Row icon="location">
                  <div className="flex flex-col gap-1">
                    <span className="contact-info-label text-[13px]">Location:</span> <span className={VALUE}>{c.address}</span>
                    {c.mapUrl && " "}
                    {c.mapUrl && (
                      <a href={c.mapUrl} className="contact-view-map">
                        View Map
                      </a>
                    )}
                  </div>
                </Row>
              )}
            </div>
          </div>
          <div className="contact-form-card rounded-[6px] p-7 md:p-9" data-reveal="right">
            {c.formTitle && <h2 className="contact-form-title text-[clamp(20px,2.2vw,24px)] font-black leading-tight mb-7 text-center">{c.formTitle}</h2>}
            <InquiryForm page="contact" className="contact-form flex flex-col gap-[18px]">
              <div className="flex flex-col gap-2">
                <label htmlFor="ci-name" className="contact-form-label text-[13px]">
                  Full Name
                </label>{" "}
                <input type="text" id="ci-name" name="name" placeholder="Enter your full name" autoComplete="name" className={FIELD} />
              </div>
              <div className="flex flex-col gap-2">
                <label htmlFor="ci-email" className="contact-form-label text-[13px]">
                  Email Address
                </label>{" "}
                <input type="email" id="ci-email" name="email" placeholder="Enter your email" autoComplete="email" className={FIELD} />
              </div>
              {!!c.inquiryTypes?.length && (
                <div className="flex flex-col gap-2">
                  <label htmlFor="ci-type" className="contact-form-label text-[13px]">
                    Inquiry Type
                  </label>{" "}
                  <select id="ci-type" name="inquiry_type" className={SELECT} defaultValue="">
                    <option value="" disabled>
                      Select inquiry type
                    </option>
                    {c.inquiryTypes.map((t) => (
                      <option key={t.key} value={t.key}>
                        {t.label}
                      </option>
                    ))}
                  </select>
                </div>
              )}
              {(!!c.meetingDateOptions?.length || !!c.meetingTimeOptions?.length) && (
                <div className="flex flex-col gap-2">
                  <span className="contact-form-label text-[13px]">Preferred Meeting Date &amp; Time</span>{" "}
                  <div className="grid grid-cols-1 sm:grid-cols-2 gap-3">
                    {!!c.meetingDateOptions?.length && (
                      <select id="ci-date" name="meeting_date" aria-label="Preferred meeting date" className={SELECT} defaultValue="">
                        <option value="" disabled>
                          Select date
                        </option>
                        {c.meetingDateOptions.map((o, i) => (
                          <option key={i}>{o}</option>
                        ))}
                      </select>
                    )}
                    {!!c.meetingDateOptions?.length && !!c.meetingTimeOptions?.length && " "}
                    {!!c.meetingTimeOptions?.length && (
                      <select id="ci-time" name="meeting_time" aria-label="Preferred meeting time" className={SELECT} defaultValue="">
                        <option value="" disabled>
                          Select time
                        </option>
                        {c.meetingTimeOptions.map((o, i) => (
                          <option key={i}>{o}</option>
                        ))}
                      </select>
                    )}
                  </div>
                </div>
              )}{" "}
              <button
                type="submit"
                className="contact-submit-btn btn btn-submit-form inline-flex items-center justify-center mt-1 px-[28px] py-[14px] text-[12px] font-bold tracking-[0.06em] uppercase rounded-[6px] border-[1.5px] text-white transition-all duration-[180ms] hover:-translate-y-px"
              >
                {" "}
                {c.submitLabel}{" "}
              </button>
            </InquiryForm>
          </div>
        </div>
      </div>
    </section>
  ),
  /** Two cards with bullets; a card with `numbered` shows them as numbered steps. */
  "business-meeting": (c: FeatureCardsContent) => (
    <section className="contact-meeting-section py-14 md:py-20 text-center">
      <div className="w-full max-w-[1180px] mx-auto px-6">
        <div data-reveal="">
          {c.title && (
            <h2 className="text-[clamp(22px,2.8vw,30px)] font-black text-white leading-tight tracking-[-0.01em]">
              <SplitTitle title={c.title} hl={HL} />
            </h2>
          )}
          {c.body && <p className="mt-4 text-[14px] text-white max-w-[720px] mx-auto leading-[1.7]">{c.body}</p>}
        </div>
        <div className="mt-10 grid grid-cols-1 md:grid-cols-2 gap-6 text-left">
          {c.items.map((it, i) => {
            const items = it.bullets?.map((b, j) => (
              <li key={j} className="text-[14px] leading-[1.55]">
                {b}
              </li>
            ));
            return (
              <div key={i} className="contact-meeting-card rounded-[6px] p-8 md:p-10" data-reveal="">
                <h3 className="text-[16px] font-bold text-white mb-5 leading-[1.35]">{it.title}</h3>
                {!!items?.length &&
                  (it.numbered ? <ol className="contact-numbered-list flex flex-col gap-3">{items}</ol> : <ul className="contact-bullet-list flex flex-col gap-3">{items}</ul>)}
              </div>
            );
          })}
        </div>
      </div>
    </section>
  ),
  /** The map frame; contact.mapUrl links here (#map). */
  map: (c: MapEmbedContent) =>
    /^https:\/\//.test(c.embedUrl) ? (
      <section className="bg-white py-12 md:py-16" id="map">
        <div className="w-full max-w-[1180px] mx-auto px-6" data-reveal="">
          <div className="contact-map-wrap rounded-[6px] overflow-hidden border border-[#e2e8f0] shadow-[0_4px_24px_rgba(15,27,46,0.06)]">
            <iframe
              title={c.label}
              src={c.embedUrl}
              className="contact-map-iframe w-full border-0"
              loading="lazy"
              referrerPolicy="no-referrer-when-downgrade"
              allowFullScreen
            ></iframe>
          </div>
        </div>
      </section>
    ) : null,
  "lead-cta": (c: CtaButtonsContent) => (
    <section className="contact-cta py-14 md:py-20 text-center">
      <div className="w-full max-w-[900px] mx-auto px-6" data-reveal="">
        <h2 className="text-[clamp(20px,2.5vw,26px)] font-black text-ink leading-tight mb-[14px]">
          <SplitTitle title={c.title} hl={HL} />
        </h2>
        {c.body && <p className="text-[15px] text-[#3c4554] mb-8 leading-[1.65]">{c.body}</p>}
        <div className="flex flex-col gap-3 sm:flex-row sm:flex-wrap sm:justify-center">
          <LinkRow ctas={c.ctas} cls={contactBtn(" font-bold")} />
        </div>
      </div>
    </section>
  ),
};
