"use client";

import { useTranslation } from "react-i18next";
import Title from "@/components/shared/Title";
import ContactUsForm from "./ContactUsForm";
import WhatsAppIcon from "./WhatsAppIcon";
import { useHomeCms } from "@/features/cms/hooks/useHomeCms";
import { pickLocalized, type FooterContact } from "@/features/shared";
import AssetIcon from "@/components/ui/AssetIcon";
import CoverImage from "@/components/ui/CoverImage";
import { contactWhatsAppHref } from "@/lib/whatsapp";

interface ContactUsProps {  initialContact?: FooterContact | null;}

export default function ContactUs({ initialContact }: ContactUsProps) {
  const { t, i18n } = useTranslation();
  // Refresh cached server content too: site settings may have been filled
  // after the page was generated. Keep server data while the request loads.
  const { cms } = useHomeCms();
  const contact = cms?.footer?.contact ?? initialContact ?? null;

  const language = i18n.language;
  const address = pickLocalized(
    contact?.address_en,
    contact?.address_ar,
    language,
  );
  const introText = pickLocalized(
    contact?.page_text_en,
    contact?.page_text_ar,
    language,
  );
  // The dashboard accepts either a bare number or a full wa.me/chat URL.
  const whatsappHref = contactWhatsAppHref(contact?.whatsapp);

  return (
    <div className="border-t border-[#000]">
      <div className="2xl:w-[75%] laptopmain:w-[83%] laptop:w-[78%] laptopitm:w-[85%] lg:w-[90%] md:w-[85%] w-[90%] mx-auto relative 2xl:py-[40px] laptopmain:py-[50px] laptop:py-[40px] lg:py-[40px] mobilescreen:py-[40px] py-[40px]">
        <h1 className="flex text-start justify-start 2xl:px-5 px-3 mobilescreen:px-[13px]">
          <Title text={t("COMMON.CONTACTUS")} variant="default" />
        </h1>
        <div className="flex justify-center">
          <div className="bg-contacts bg-[linear-gradient(to_right,_#f3f3f3_50%,_#29246d_50%)] rtl:bg-[linear-gradient(to_left,_#f3f3f3_50%,_#29246d_50%)] items-center flex flex-col mobilescreen:flex-col md:flex-row rounded-[30px] overflow-hidden 2xl:w-[1170px] laptopmain:w-[1300px] lg:w-[90%] w-[100%]">
            {/* Contact Form */}
            <div className="w-full md:w-1/2 2xl:py-14 lg:py-8 md:py-8 py-10 px-6 contactinput bgcl1">
              <h2 className="lg:text-[40px] md:text-4xl text-2xl font-bold text-[#29246d] mb-6">
                {t("COMMON.GET_IN_TOUCH")}
              </h2>

              {/* Intro copy — admin-editable */}
              {introText && (
                <p className="text-secondary-100 text-base mb-6 whitespace-pre-line">
                  {introText}
                </p>
              )}

              <ContactUsForm />
            </div>

            {/* Map and Contact Info */}
            <div className="w-full md:w-1/2 text-white p-8 bgcl2">
              <a
                href="https://maps.app.goo.gl/8PEoN6SfL2Xq17p1A"
                target="_blank"
                rel="noopener noreferrer"
                className="relative block h-64 bg-gray-200 rounded-lg mb-8 overflow-hidden focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500"
                title={t("COMMON.OPEN_IN_MAPS")}
              >
                {/* `h-64` on the anchor already fixes the box, so the still
                    fills it rather than sizing it. */}
                <CoverImage
                  src="/assets/homepage/mapimg.png"
                  alt={t("COMMON.OPEN_IN_MAPS")}
                  className="object-cover cursor-pointer"
                  sizes="(min-width: 768px) 50vw, 100vw"
                />
              </a>

              {/* Contact details — admin-editable (`GET /home/` → footer.contact) */}
              <div className="space-y-4">
                {address && (
                  <a
                    href="https://maps.app.goo.gl/8PEoN6SfL2Xq17p1A"
                    target="_blank"
                    rel="noopener noreferrer"
                    className="flex items-center gap-[17px] hover:underline focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500"
                    title={t("COMMON.OPEN_IN_MAPS")}
                  >
                    <AssetIcon src="/assets/homepage/mapicn.svg" width={20} height={24} />
                    <span>{address}</span>
                  </a>
                )}

                {contact?.phone && (
                  <div className="flex items-center gap-[17px]">
                    <AssetIcon src="/assets/homepage/phoneicn.svg" size={22} />
                    <a
                      href={`tel:${contact.phone.replace(/\s+/g, "")}`}
                      className="not-italic no-underline text-inherit cursor-pointer hover:underline"
                    >
                      <bdi dir="ltr">{contact.phone}</bdi>
                    </a>
                  </div>
                )}

                {contact?.whatsapp && (
                  <div className="flex items-center gap-[17px]">
                    <WhatsAppIcon />
                    <a href={whatsappHref} target="_blank" rel="noopener noreferrer" className="not-italic no-underline text-inherit cursor-pointer hover:underline" >
                      {contact.whatsapp}
                    </a>
                  </div>
                )}

                {contact?.email && (
                  <div className="flex items-center gap-[17px]">
                    <AssetIcon src="/assets/homepage/mailicn.svg" width={22} height={18} />
                    <a
                      href={`mailto:${contact.email}`}
                      className="not-italic no-underline text-inherit cursor-pointer hover:underline"
                    >
                      <bdi dir="ltr">{contact.email}</bdi>
                    </a>
                  </div>
                )}
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  );
}
