"use client";

import { MoveRight } from "lucide-react";
import Email from "./svgComponents/Email";
import Phone from "./svgComponents/Phone";
import Location from "./svgComponents/Location";
import Link from "next/link";
import z from "zod";
import { useForm } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod";
import { toast } from "sonner";

const subscribeSchema = z.object({
  email: z
    .string()
    .min(1, "Email is required")
    .email("Please enter a valid email"),
});
type SubscribeFormValues = z.infer<typeof subscribeSchema>;

const socialLinks = [
  {
    name: "Facebook",
    href: "#",
    icon: (
      <svg
        className="w-[18px] h-[18px] md:w-[14px] md:h-[15px] lg:w-[16px] lg:h-[17px] shrink-0"
        viewBox="0 0 16 17"
        fill="none"
        xmlns="http://www.w3.org/2000/svg"
      >
        <path
          d="M14.6654 8.01974C14.6654 4.33028 11.6787 1.33594 7.9987 1.33594C4.3187 1.33594 1.33203 4.33028 1.33203 8.01974C1.33203 11.2547 3.62536 13.9483 6.66536 14.5699V10.0249H5.33203V8.01974H6.66536V6.34879C6.66536 5.05882 7.71203 4.00946 8.9987 4.00946H10.6654V6.0146H9.33203C8.96536 6.0146 8.66536 6.31537 8.66536 6.68298V8.01974H10.6654V10.0249H8.66536V14.6701C12.032 14.3359 14.6654 11.4886 14.6654 8.01974Z"
          fill="currentColor"
        />
      </svg>
    ),
    hoverColor: "hover:text-[#0A51C9] after:bg-[#0A51C9]",
  },
  {
    name: "Instagram",
    href: "#",
    icon: (
      <svg
        className="w-[18px] h-[18px] md:w-[14px] md:h-[15px] lg:w-[16px] lg:h-[17px] shrink-0"
        viewBox="0 0 16 17"
        fill="none"
        xmlns="http://www.w3.org/2000/svg"
      >
        <path
          d="M8.68441 1.33594C9.43441 1.33794 9.81508 1.34195 10.1437 1.35131L10.2731 1.35599C10.4224 1.36134 10.5697 1.36802 10.7477 1.37604C11.4571 1.40946 11.9411 1.52175 12.3657 1.68684C12.8057 1.85661 13.1764 2.08653 13.5471 2.45748C13.8862 2.79149 14.1485 3.19569 14.3157 3.64185C14.4804 4.06761 14.5924 4.55285 14.6257 5.26468C14.6337 5.44247 14.6404 5.59018 14.6457 5.74057L14.6497 5.87023C14.6597 6.19907 14.6637 6.58072 14.6651 7.33265L14.6657 7.83126V8.70684C14.6674 9.19436 14.6623 9.68188 14.6504 10.1693L14.6464 10.2989C14.6411 10.4493 14.6344 10.597 14.6264 10.7748C14.5931 11.4866 14.4797 11.9712 14.3157 12.3976C14.1485 12.8438 13.8862 13.248 13.5471 13.582C13.2139 13.922 12.8108 14.185 12.3657 14.3526C11.9411 14.5177 11.4571 14.63 10.7477 14.6634L10.2731 14.6835L10.1437 14.6875C9.81508 14.6969 9.43441 14.7015 8.68441 14.7029L8.18708 14.7035H7.31441C6.82792 14.7053 6.34143 14.7001 5.85508 14.6882L5.72574 14.6842C5.56748 14.6782 5.40926 14.6712 5.25108 14.6634C4.54174 14.63 4.05774 14.5177 3.63241 14.3526C3.18763 14.1849 2.78471 13.9219 2.45174 13.582C2.11237 13.2481 1.84982 12.8439 1.68241 12.3976C1.51774 11.9719 1.40574 11.4866 1.37241 10.7748L1.35241 10.2989L1.34908 10.1693C1.33679 9.68188 1.33123 9.19436 1.33241 8.70684V7.33265C1.33057 6.84513 1.33545 6.35761 1.34708 5.87023L1.35174 5.74057C1.35708 5.59018 1.36374 5.44247 1.37174 5.26468C1.40508 4.55285 1.51708 4.06828 1.68174 3.64185C1.84954 3.1955 2.11256 2.79129 2.45241 2.45748C2.78518 2.11764 3.18787 1.85465 3.63241 1.68684C4.05774 1.52175 4.54108 1.40946 5.25108 1.37604C5.42841 1.36802 5.57641 1.36134 5.72574 1.35599L5.85508 1.35198C6.34121 1.3401 6.82747 1.33498 7.31374 1.33661L8.68441 1.33594ZM7.99908 4.67784C7.11502 4.67784 6.26718 5.02993 5.64205 5.65666C5.01693 6.28339 4.66574 7.13341 4.66574 8.01974C4.66574 8.90607 5.01693 9.7561 5.64205 10.3828C6.26718 11.0096 7.11502 11.3616 7.99908 11.3616C8.88313 11.3616 9.73098 11.0096 10.3561 10.3828C10.9812 9.7561 11.3324 8.90607 11.3324 8.01974C11.3324 7.13341 10.9812 6.28339 10.3561 5.65666C9.73098 5.02993 8.88313 4.67784 7.99908 4.67784ZM7.99908 6.0146C8.26172 6.01456 8.5218 6.06638 8.76447 6.16711C9.00714 6.26783 9.22764 6.41549 9.41339 6.60166C9.59914 6.78782 9.74649 7.00884 9.84704 7.2521C9.94759 7.49536 9.99937 7.75609 9.99941 8.01941C9.99945 8.28273 9.94777 8.54348 9.8473 8.78677C9.74683 9.03006 9.59955 9.25113 9.41386 9.43736C9.22817 9.62358 9.00772 9.77132 8.76509 9.87212C8.52245 9.97293 8.26239 10.0248 7.99974 10.0249C7.46931 10.0249 6.9606 9.81363 6.58553 9.43759C6.21046 9.06155 5.99974 8.55154 5.99974 8.01974C5.99974 7.48795 6.21046 6.97793 6.58553 6.60189C6.9606 6.22586 7.46931 6.0146 7.99974 6.0146M11.4997 3.67527C11.2787 3.67527 11.0668 3.76329 10.9105 3.91997C10.7542 4.07666 10.6664 4.28916 10.6664 4.51074C10.6664 4.73233 10.7542 4.94483 10.9105 5.10152C11.0668 5.2582 11.2787 5.34622 11.4997 5.34622C11.7208 5.34622 11.9327 5.2582 12.089 5.10152C12.2453 4.94483 12.3331 4.73233 12.3331 4.51074C12.3331 4.28916 12.2453 4.07666 12.089 3.91997C11.9327 3.76329 11.7208 3.67527 11.4997 3.67527Z"
          fill="currentColor"
        />
      </svg>
    ),
    hoverColor: "hover:text-[#EF583B] after:bg-[#EF583B]",
  },
  {
    name: "LinkedIn",
    href: "#",
    icon: (
      <svg
        className="w-[18px] h-[18px] md:w-[14px] md:h-[15px] lg:w-[16px] lg:h-[17px] shrink-0"
        viewBox="0 0 16 17"
        fill="none"
        xmlns="http://www.w3.org/2000/svg"
      >
        <path
          d="M4.34038 5.55859H1.58468C1.46238 5.55859 1.36328 5.65799 1.36328 5.78056V14.6562C1.36328 14.7788 1.46238 14.8782 1.58468 14.8782H4.34038C4.46269 14.8782 4.56179 14.7788 4.56179 14.6562V5.78056C4.56179 5.65799 4.46269 5.55859 4.34038 5.55859Z"
          fill="currentColor"
        />
        <path
          d="M2.96295 1.14844C1.96026 1.14844 1.14453 1.96538 1.14453 2.96953C1.14453 3.97413 1.96026 4.79138 2.96295 4.79138C3.96484 4.79138 4.77991 3.97409 4.77991 2.96953C4.77995 1.96538 3.96484 1.14844 2.96295 1.14844Z"
          fill="currentColor"
        />
        <path
          d="M11.3507 5.33984C10.2439 5.33984 9.42578 5.81686 8.92953 6.35887V5.78241C8.92953 5.65984 8.83043 5.56044 8.70813 5.56044H6.06906C5.94676 5.56044 5.84766 5.65984 5.84766 5.78241V14.6581C5.84766 14.7807 5.94676 14.88 6.06906 14.88H8.81874C8.94104 14.88 9.04014 14.7807 9.04014 14.6581V10.2667C9.04014 8.78688 9.44105 8.21037 10.47 8.21037C11.5905 8.21037 11.6796 9.13457 11.6796 10.3428V14.6581C11.6796 14.7807 11.7787 14.8801 11.901 14.8801H14.6517C14.774 14.8801 14.8731 14.7807 14.8731 14.6581V9.78965C14.8731 7.58925 14.4546 5.33984 11.3507 5.33984Z"
          fill="currentColor"
        />
      </svg>
    ),
    hoverColor: "hover:text-[#0A51C9] after:bg-[#0A51C9]",
  },
];

export default function Footer() {
  const {
    register,
    handleSubmit,
    reset,
    formState: { errors, isSubmitting, isValid },
  } = useForm<SubscribeFormValues>({
    resolver: zodResolver(subscribeSchema),
    mode: "onChange",
  });

  async function onSubmit(values: SubscribeFormValues) {
    try {
      const response = await fetch("/api/subscribe", {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
        },
        body: JSON.stringify(values),
      });

      const data = await response.json();

      if (response.ok) {
        toast.success("Subscribed successfully!");
        reset();
      } else {
        toast.error(data.error || "Something went wrong");
      }
    } catch (error) {
      toast.success("Subscribed successfully!");
    }
  }

  return (
    <footer className="w-full bg-[#3B3B3B] text-white relative overflow-hidden mt-auto">
      <div className="px-[20px] xs:px-[20px] sm:px-[30px] md:px-[60px] lg:px-[110px] xl:px-[135px] xl:pt-[119px] lg:pt-[80px] pt-[40px] flex flex-col relative z-0 pb-[35px]">
        <div className="flex flex-col lg:flex-row justify-between gap-[16px] sm:gap-[20px] md:gap-[30px] lg:gap-[50px] xl:gap-[210px]">
          <div className="flex-col gap-[20px] lg:gap-[26px] w-full lg:w-[45%] xl:w-[35%] md:flex hidden">
            <div className="w-[140px] h-[44px]">
              <img
                src="/images/common/lodging-system-logo-light.png"
                alt=""
                className="w-full h-full object-contain"
              />
            </div>
            <div className="figtree text-[16px] lg:text-lg leading-6">
              Lodging System simplifies hotel operations and enhances guest
              experiences. Deliver convenience, personalization, and lasting
              loyalty with one powerful platform.
            </div>
          </div>
          <div className="flex flex-col gap-[20px] lg:gap-[26px] xl:w-[15%] order-last md:order-0">
            <div className="outfit text-[16px] lg:text-lg leading-[100%] md:block hidden">
              Contact Us
            </div>
            <div className="flex flex-col gap-3 md:gap-4 text-[#D3D3D3]">
              <a
                href={`tel:${process.env.NEXT_PUBLIC_CONTACT_PHONE ?? ""}`}
                className="flex gap-2 items-start outfit text-[14px] lg:text-base leading-[100%]"
              >
                <Phone className="text-[#ffffff] md:h-5 md:w-5 h-4 w-4" />
                {process.env.NEXT_PUBLIC_CONTACT_PHONE ?? ""}
              </a>
              <a
                href={`mailto:${process.env.NEXT_PUBLIC_CONTACT_EMAIL ?? ""}`}
                className="flex gap-2 items-start outfit text-[14px] lg:text-base leading-[100%]"
              >
                <Email className="text-[#ffffff] shrink-0 flex-none md:h-5 md:w-5 h-4 w-4" />
                {process.env.NEXT_PUBLIC_CONTACT_EMAIL ?? ""}
              </a>
              <div className="flex gap-2 items-start outfit text-[14px] lg:text-base leading-5">
                <Location className="text-[#ffffff] shrink-0 flex-none mt-0.5 md:h-5 md:w-5 h-4 w-4" />
                <span>
                  1221 Brickell Avenue, Suite 900 Miami, Florida, 33131
                </span>
              </div>
            </div>
          </div>
          <div className="flex flex-col gap-[12px] md:gap-[20px] lg:gap-[32px] xl:gap-[42px] w-full lg:w-[50%] order-0 md:order-last">
            <div className="figtree text-[24px] md:text-[30px] lg:text-[32px] xl:text-[36px] leading-[32px] md:leading-[36px] lg:leading-[38px] xl:leading-[100%]">
              Subscribe to get tips and tactics to grow the way you want.
            </div>

            <form onSubmit={handleSubmit(onSubmit)}>
              <div className="flex flex-row h-[38px] lg:h-auto">
                <div className="h-full rounded-l-[8px] bg-white w-full">
                  <input
                    type="email"
                    placeholder="Your email address"
                    className="w-full h-full px-4 lg:px-3 py-3 lg:py-4 focus-visible:ring-0 focus-visible:outline-0 figtree text-black text-[14px] lg:text-base rounded-l-[8px]"
                    {...register("email")}
                  />
                </div>
                <button
                  type="submit"
                  disabled={isSubmitting || !isValid}
                  className={`bg-[#F58220] text-white px-[15px] py-3 rounded-r-[8px] text-[14px] lg:text-base leading-6 font-semibold w-fit whitespace-nowrap figtree flex items-center justify-center cursor-pointer disabled:opacity-50 disabled:cursor-not-allowed`}
                >
                  {isSubmitting ? (
                    "..."
                  ) : (
                    <MoveRight className="h-5 w-5 lg:h-6 lg:w-6" />
                  )}
                </button>
              </div>
              <p className="text-red-400 text-sm figtree h-[20px] mt-0.5">
                {errors.email?.message}
              </p>
            </form>
          </div>
        </div>
        <div className="flex flex-col pt-[20px] md:pt-[24px] lg:pt-[32px] lg:flex-row justify-between items-center gap-3 md:gap-4 lg:gap-6">
          <div className="flex flex-col sm:flex-row gap-3 sm:gap-6 text-[14px] lg:text-sm leading-[100%] figtree text-[#B4B4B4] lg:text-white order-last md:order-0 text-center md:text-start">
            <div>©2026 Copyright Lodging System. All Rights Reserved.</div>
            <div className="gap-6 sm:gap-6 md:flex hidden">
              <Link
                href={"/privacy-policy"}
                className="underline cursor-pointer"
              >
                Privacy policy
              </Link>
              <Link
                href={"/terms-conditions"}
                className="underline cursor-pointer"
              >
                Terms and conditions
              </Link>
            </div>
          </div>
          <div className="flex gap-4 justify-center w-full md:w-auto order-0 md:order-last">
            {socialLinks.map((link) => (
              <a
                key={link.name}
                href={link.href}
                target="_blank"
                className={`relative overflow-hidden h-[34px] lg:h-[38px] rounded-full flex items-center gap-1.5 px-2 md:px-2.5 lg:px-3 xl:px-3.5 py-1 md:py-2.5 text-[12px] lg:text-[14px] font-medium capitalize text-white transition-colors duration-300 before:absolute before:inset-0 before:border before:border-[#999A9C] before:rounded-full before:transition-transform before:duration-500 before:origin-right hover:before:translate-x-full after:absolute after:bottom-[6px] after:left-[14px] after:right-[14px] after:h-[1.5px] after:opacity-0 after:transition-opacity after:duration-0 hover:after:delay-100 hover:after:opacity-100 ${link.hoverColor}`}
              >
                <span>{link.icon}</span>
                <span className="md:block hidden">{link.name}</span>
              </a>
            ))}
          </div>
        </div>
        <div className="text-[#FFFFFF0A] text-center font-extrabold montserrat w-full whitespace-nowrap text-[clamp(40px,10vw,194px)] leading-none mt-0 select-none pointer-events-none md:block hidden">
          Lodging System
        </div>
      </div>
      <div className="w-full bg-[#3B3B3B] h-[130px] absolute bottom-0 blur-[65px] z-1 pointer-events-none" />
    </footer>
  );
}
