import { notFound } from "next/navigation";
import Link from "next/link";
import { isLocale, type Locale } from "@/lib/i18n/config";
import { getDictionary } from "@/lib/i18n";
import { buildMetadata } from "@/lib/seo";
import { Accordion, AccordionContent, AccordionItem, AccordionTrigger } from "@/components/ui/accordion";
import { Button } from "@/components/ui/button";

type Entry = { question: string; answer: string };

const faqContent: Record<Locale, Entry[]> = {
  fr: [
    {
      question: "Comment passer commande ?",
      answer:
        "Choisissez vos pièces, ajoutez-les au panier puis suivez les étapes du paiement. Vous pouvez commander sans créer de compte ; un compte vous permet toutefois de retrouver vos commandes et vos adresses.",
    },
    {
      question: "Quels sont les délais de livraison ?",
      answer:
        "Au Maroc, comptez 2 à 4 jours ouvrés après confirmation. En Europe, 5 à 8 jours ouvrés ; ailleurs, 7 à 12 jours ouvrés. Chaque pièce est emballée à la main dans nos ateliers.",
    },
    {
      question: "La livraison est-elle offerte ?",
      answer:
        "La livraison est offerte au Maroc dès 1 500 DH d'achat. Pour les autres destinations, les frais sont calculés selon le pays et affichés avant la validation de la commande.",
    },
    {
      question: "Puis-je retourner une pièce ?",
      answer:
        "Oui. Vous disposez de 14 jours après réception pour nous retourner une pièce non utilisée, dans son emballage d'origine. Les pièces personnalisées et les tapis sur mesure ne sont pas éligibles.",
    },
    {
      question: "Quels moyens de paiement acceptez-vous ?",
      answer:
        "Paiement à la livraison au Maroc, virement bancaire et paiement en ligne. Cette boutique est une démonstration : aucun paiement réel n'est encaissé et aucune coordonnée bancaire n'est demandée.",
    },
    {
      question: "Comment entretenir mes pièces ?",
      answer:
        "Chaque pièce est accompagnée d'une fiche d'entretien. En résumé : dépoussiérer régulièrement, éviter l'exposition directe au soleil et confier les pièces délicates à un professionnel.",
    },
  ],
  en: [
    {
      question: "How do I place an order?",
      answer:
        "Choose your pieces, add them to the cart and follow the checkout steps. You can order without creating an account, though an account lets you find your orders and addresses again.",
    },
    {
      question: "What are the delivery times?",
      answer:
        "In Morocco, allow 2 to 4 working days after confirmation. In Europe, 5 to 8 working days; elsewhere, 7 to 12 working days. Every piece is packed by hand in our workshops.",
    },
    {
      question: "Is shipping free?",
      answer:
        "Shipping is free in Morocco from 1,500 DH. For other destinations, costs are calculated by country and shown before you confirm your order.",
    },
    {
      question: "Can I return a piece?",
      answer:
        "Yes. You have 14 days after delivery to return an unused piece in its original packaging. Personalised pieces and made-to-measure rugs are not eligible.",
    },
    {
      question: "Which payment methods do you accept?",
      answer:
        "Cash on delivery in Morocco, bank transfer and online payment. This shop is a demonstration: no real payment is taken and no bank details are requested.",
    },
    {
      question: "How should I care for my pieces?",
      answer:
        "Every piece comes with a care card. In short: dust regularly, avoid direct sunlight and entrust delicate pieces to a professional.",
    },
  ],
  ar: [
    {
      question: "كيف أقدّم طلبًا؟",
      answer:
        "اختر قطعك، أضفها إلى السلة ثم اتبع خطوات الدفع. يمكنك الطلب دون إنشاء حساب، لكن الحساب يتيح لك استرجاع طلباتك وعناوينك.",
    },
    {
      question: "ما مدة التوصيل؟",
      answer:
        "في المغرب، احسب من 2 إلى 4 أيام عمل بعد التأكيد. في أوروبا من 5 إلى 8 أيام عمل، وفي باقي الدول من 7 إلى 12 يوم عمل. تُغلَّف كل قطعة يدويًا في ورشنا.",
    },
    {
      question: "هل التوصيل مجاني؟",
      answer:
        "التوصيل مجاني في المغرب ابتداءً من 1500 درهم. أما الوجهات الأخرى فتُحسب تكاليفها حسب البلد وتظهر قبل تأكيد الطلب.",
    },
    {
      question: "هل يمكنني إرجاع قطعة؟",
      answer:
        "نعم. لديك 14 يومًا بعد الاستلام لإرجاع قطعة غير مستعملة في تغليفها الأصلي. القطع المخصصة والسجاد المفصّل غير مؤهلة للإرجاع.",
    },
    {
      question: "ما وسائل الدفع المتاحة؟",
      answer:
        "الدفع عند الاستلام في المغرب، والتحويل البنكي، والدفع الإلكتروني. هذا المتجر عرض توضيحي: لا يُحصَّل أي دفع حقيقي ولا تُطلب أي بيانات بنكية.",
    },
    {
      question: "كيف أعتني بقطعتي؟",
      answer:
        "تأتي كل قطعة مع بطاقة عناية. باختصار: أزل الغبار بانتظام، وتجنب أشعة الشمس المباشرة، وسلّم القطع الحساسة إلى مختص.",
    },
  ],
};

export async function generateMetadata({ params }: { params: Promise<{ locale: string }> }) {
  const { locale: raw } = await params;
  if (!isLocale(raw)) return {};
  const locale = raw as Locale;
  const dict = getDictionary(locale);
  return buildMetadata({
    locale,
    title: `${dict.contact.faqTitle} — DAR ZELLIGE`,
    description: dict.contact.faqTitle,
    path: "/faq",
  });
}

export default async function FaqPage({ params }: { params: Promise<{ locale: string }> }) {
  const { locale: raw } = await params;
  if (!isLocale(raw)) notFound();
  const locale = raw as Locale;
  const dict = getDictionary(locale);
  const entries = faqContent[locale];

  return (
    <>
      <section className="border-b border-border bg-cream/40">
        <div className="shell py-12 md:py-16">
          <p className="eyebrow">{dict.footer.help}</p>
          <h1 className="mt-4 font-serif text-4xl font-light tracking-tight md:text-6xl">
            {dict.contact.faqTitle}
          </h1>
          <p className="mt-4 max-w-xl text-[15px] leading-relaxed text-charcoal/55">
            {dict.contact.subtitle}
          </p>
        </div>
      </section>

      <div className="shell grid gap-10 py-12 md:py-16 lg:grid-cols-[1fr_300px] lg:gap-16">
        <Accordion type="single" collapsible className="border-t border-border">
          {entries.map((entry, index) => (
            <AccordionItem key={entry.question} value={`faq-${index}`}>
              <AccordionTrigger className="normal-case tracking-normal text-[14px]">
                {entry.question}
              </AccordionTrigger>
              <AccordionContent className="max-w-2xl text-[14px] leading-[1.9]">{entry.answer}</AccordionContent>
            </AccordionItem>
          ))}
        </Accordion>

        <aside className="h-fit border border-border bg-cream/40 p-5">
          <p className="font-serif text-xl font-light">{dict.contact.title}</p>
          <p className="mt-2 text-[13px] leading-relaxed text-charcoal/50">{dict.contact.subtitle}</p>
          <Button asChild variant="outline" className="mt-5 w-full">
            <Link href={`/${locale}/contact`}>{dict.footer.contact}</Link>
          </Button>
          <div className="mt-5 space-y-2 border-t border-border pt-4 text-[12px] text-charcoal/45">
            <Link href={`/${locale}/shipping`} className="block hover:text-charcoal">
              {dict.footer.shipping}
            </Link>
            <Link href={`/${locale}/care`} className="block hover:text-charcoal">
              {dict.footer.care}
            </Link>
            <Link href={`/${locale}/terms`} className="block hover:text-charcoal">
              {dict.footer.terms}
            </Link>
          </div>
        </aside>
      </div>
    </>
  );
}
