"use client";

import Navbar from "@/components/Navbar";
import Footer from "@/components/Footer";
import type { FAQItem } from "@/data/types";
import { useEffect, useState } from "react";
import { ChevronDown, Search } from "lucide-react";

interface FAQProps {
  categories: string[];
  items: FAQItem[];
  initialCategory: string;
  initialSearch: string;
}

const FAQ = ({
  categories,
  items,
  initialCategory,
  initialSearch,
}: FAQProps) => {
  const [activeCategory, setActiveCategory] = useState<string>(initialCategory);
  const [openId, setOpenId] = useState<string | null>(null);
  const [search, setSearch] = useState(initialSearch);

  useEffect(() => {
    setActiveCategory(initialCategory);
    setSearch(initialSearch);
    setOpenId(null);
  }, [initialCategory, initialSearch]);

  const filtered = items.filter((faq) => {
    const matchCategory = activeCategory === "all" || faq.category === activeCategory;
    const matchSearch =
      search === "" ||
      faq.question.toLowerCase().includes(search.toLowerCase()) ||
      faq.answer.toLowerCase().includes(search.toLowerCase());
    return matchCategory && matchSearch;
  });

  return (
    <div className="min-h-screen bg-background">
      <Navbar />
      <main className="pt-20 pb-16">
        <div className="container mx-auto px-4 lg:px-8">
          <div className="max-w-3xl mx-auto">
            <div className="text-center mb-12">
              <h1 className="section-title mb-3">Често задавани въпроси</h1>
              <p className="text-muted-foreground text-lg font-body">
                Намерете отговори на най-често задаваните въпроси за покупка на имот в Гърция.
              </p>
            </div>

            {/* Search */}
            <div className="relative mb-8">
              <Search className="absolute left-4 top-1/2 -translate-y-1/2 w-5 h-5 text-muted-foreground" />
              <input
                type="text"
                placeholder="Търсете във въпросите..."
                value={search}
                onChange={(e) => setSearch(e.target.value)}
                className="w-full pl-12 pr-4 py-3 rounded-xl bg-card border border-border/50 text-foreground text-sm placeholder:text-muted-foreground focus:outline-none focus:ring-2 focus:ring-accent/40 font-body"
              />
            </div>

            {/* Categories */}
            <div className="flex flex-wrap gap-2 mb-8">
              <button
                onClick={() => setActiveCategory("all")}
                className={`px-4 py-2 rounded-full text-sm font-medium transition-colors font-body ${
                  activeCategory === "all" ? "bg-primary text-primary-foreground" : "bg-secondary text-foreground hover:bg-secondary/80"
                }`}
              >
                Всички
              </button>
              {categories.map((cat) => (
                <button
                  key={cat}
                  onClick={() => setActiveCategory(cat)}
                  className={`px-4 py-2 rounded-full text-sm font-medium transition-colors font-body ${
                    activeCategory === cat ? "bg-primary text-primary-foreground" : "bg-secondary text-foreground hover:bg-secondary/80"
                  }`}
                >
                  {cat}
                </button>
              ))}
            </div>

            {/* FAQ items */}
            <div className="space-y-3">
              {filtered.map((faq) => (
                <div key={faq.id} className="bg-card rounded-xl border border-border/50 overflow-hidden">
                  <button
                    onClick={() => setOpenId(openId === faq.id ? null : faq.id)}
                    className="w-full flex items-center justify-between p-5 text-left"
                  >
                    <span className="font-semibold text-foreground font-body pr-4">{faq.question}</span>
                    <ChevronDown
                      className={`w-5 h-5 text-muted-foreground shrink-0 transition-transform ${
                        openId === faq.id ? "rotate-180" : ""
                      }`}
                    />
                  </button>
                  <div className={openId === faq.id ? "px-5 pb-5 block" : "px-5 pb-5 hidden"}>
                      <p className="text-muted-foreground font-body text-sm leading-relaxed">{faq.answer}</p>
                    </div>
                </div>
              ))}
            </div>

            {filtered.length === 0 && (
              <p className="text-center text-muted-foreground font-body py-8">
                Няма намерени въпроси. Опитайте с друга ключова дума.
              </p>
            )}
          </div>
        </div>
      </main>
      <Footer />
    </div>
  );
};

export default FAQ;
