"use client";

import { useCallback, useEffect, useRef, useState } from "react";
import Link from "next/link";
import { ArrowLeft, ArrowRight } from "lucide-react";
import { localizedPath, type Locale, type PropertyCardDto } from "@alterra/shared";
import SectionHeading from "../SectionHeading";
import type { HomeCopy } from "@/lib/home";
import PropertyCard from "../properties/PropertyCard";
import type { PropertyCopy } from "@/lib/propertyCopy";

export default function FeaturedCarousel({
  copy,
  lang,
  properties,
  cardCopy,
}: {
  copy: HomeCopy["featured"];
  lang: Locale;
  properties: PropertyCardDto[];
  cardCopy: Pick<PropertyCopy, "card" | "types" | "badges">;
}) {
  const track = useRef<HTMLDivElement>(null);
  const [edges, setEdges] = useState({ start: true, end: false });

  const updateEdges = useCallback(() => {
    const el = track.current;
    if (!el) return;
    setEdges({ start: el.scrollLeft < 8, end: el.scrollLeft + el.clientWidth > el.scrollWidth - 8 });
  }, []);

  useEffect(() => {
    updateEdges();
    window.addEventListener("resize", updateEdges);
    return () => window.removeEventListener("resize", updateEdges);
  }, [updateEdges]);

  const scrollBy = (direction: 1 | -1) => {
    const el = track.current;
    const card = el?.firstElementChild;
    if (!el || !card) return;
    el.scrollBy({ left: direction * (card.clientWidth + 32), behavior: "smooth" });
  };

  return (
    <section className="overflow-hidden bg-cream py-24 md:py-32">
      <div className="mx-auto max-w-7xl px-5 md:px-8">
        <div className="flex flex-col justify-between gap-8 md:flex-row md:items-end">
          <SectionHeading eyebrow={copy.eyebrow} title={copy.title} intro={copy.intro} />
          <div className="flex items-center gap-3">
            {([-1, 1] as const).map((dir) => (
              <button
                key={dir}
                type="button"
                onClick={() => scrollBy(dir)}
                disabled={dir === -1 ? edges.start : edges.end}
                aria-label={dir === -1 ? copy.prev : copy.next}
                className="flex h-14 w-14 items-center justify-center rounded-full border border-ink/25 transition hover:border-ink hover:bg-ink hover:text-cream disabled:pointer-events-none disabled:opacity-30"
              >
                {dir === -1 ? <ArrowLeft size={18} strokeWidth={1.4} /> : <ArrowRight size={18} strokeWidth={1.4} />}
              </button>
            ))}
          </div>
        </div>
      </div>

      <div
        ref={track}
        onScroll={updateEdges}
        className="mt-14 flex snap-x snap-mandatory gap-8 overflow-x-auto scroll-smooth scroll-px-5 px-5 pb-4 [scrollbar-width:none] md:scroll-px-8 md:px-8 xl:scroll-px-[calc((100vw_-_80rem)/2_+_2rem)] xl:px-[calc((100vw_-_80rem)/2_+_2rem)] [&::-webkit-scrollbar]:hidden"
      >
        {properties.map((item) => (
          <div key={item.slug} className="w-[82vw] shrink-0 snap-start sm:w-[60vw] md:w-[44vw] lg:w-[30%]">
            <PropertyCard property={item} lang={lang} copy={cardCopy} sizes="(min-width: 1024px) 30vw, (min-width: 768px) 44vw, 82vw" />
          </div>
        ))}
      </div>

      <div className="mx-auto mt-10 max-w-7xl px-5 md:px-8">
        <Link
          href={localizedPath(lang, "/properties")}
          className="eyebrow inline-flex items-center gap-3 border-b border-gold pb-1.5 text-ink transition hover:text-gold-ink"
        >
          {copy.all} <ArrowRight size={14} strokeWidth={1.5} />
        </Link>
      </div>
    </section>
  );
}
