import Link from "next/link";
import type { Metadata } from "next";
import { getPublicPageSeo } from "@/lib/public-page-seo";
import { resolveOgImageUrl } from "@/lib/site-og-image";

const SITE_URL = (process.env.NEXT_PUBLIC_SITE_URL || "https://nizamify.com").replace(/\/$/, "");

export async function generateMetadata(): Promise<Metadata> {
  const seo = await getPublicPageSeo("faqs");
  const title = seo?.metaTitle?.trim() || seo?.title?.trim() || "FAQs";
  const description =
    seo?.metaDescription?.trim() ||
    "Find answers about Nizamify smart products, automation services, delivery, support, and ordering.";
  const canonical = "/faqs";
  const keywords =
    Array.isArray(seo?.metaKeywords) && seo.metaKeywords.length > 0
      ? seo.metaKeywords
      : ["Nizamify FAQs", "smart home support Pakistan"];
  const image = resolveOgImageUrl(seo?.image);

  return {
    title,
    description,
    keywords,
    alternates: { canonical },
    openGraph: {
      title,
      description,
      url: `${SITE_URL}${canonical}`,
      type: "website",
      images: [{ url: image }],
    },
    twitter: {
      card: "summary_large_image",
      title,
      description,
      images: [image],
    },
  };
}

const faqs = [
  {
    question: "What is Nizamify?",
    answer:
      "Nizamify provides smart home and business automation solutions, including connected devices, smart controls, monitoring tools, and software services.",
  },
  {
    question: "Do you sell smart home products?",
    answer:
      "Yes. You can browse available smart products on our Products page and view pricing, availability, delivery information, and product details.",
  },
  {
    question: "Can I contact you before buying a product?",
    answer:
      "Yes. You can use the Contact Us page or the WhatsApp buttons on product pages to ask questions before purchasing.",
  },
  {
    question: "How does WhatsApp ordering work?",
    answer:
      "When you click the WhatsApp button on a product detail page, WhatsApp opens with a pre-filled message containing the product name, price, and link.",
  },
  {
    question: "Do you provide installation support?",
    answer:
      "Installation and setup support depends on the product and location. Please contact our team with your city and product requirement for confirmation.",
  },
  {
    question: "Do you offer software automation services?",
    answer:
      "Yes. We help businesses build automation dashboards, device workflows, monitoring systems, and custom software solutions.",
  },
  {
    question: "How can I check if a product is in stock?",
    answer:
      "Product cards and product detail pages show availability. If a product is unavailable, the button will show Out of Stock.",
  },
  {
    question: "What are the delivery charges?",
    answer:
      "Some products include free shipping, while others may show a separate delivery fee. Product listings and details display this information clearly.",
  },
  {
    question: "How long does delivery take?",
    answer:
      "Delivery time depends on the product, city, and courier availability. Our team will confirm estimated delivery after your order or inquiry.",
  },
  {
    question: "How do I get support?",
    answer:
      "You can reach us through the Contact Us page, email, or WhatsApp. Share your requirement, product name, and contact details for faster support.",
  },
];

const faqSchema = {
  "@context": "https://schema.org",
  "@type": "FAQPage",
  mainEntity: faqs.map((f) => ({
    "@type": "Question",
    name: f.question,
    acceptedAnswer: {
      "@type": "Answer",
      text: f.answer,
    },
  })),
};

export default function FaqsPage() {
  return (
    <>
      <script
        type="application/ld+json"
        dangerouslySetInnerHTML={{ __html: JSON.stringify(faqSchema) }}
      />
    <section className="bg-slate-50 py-12 md:py-16">
      <div className="container">
        <div className="mx-auto max-w-4xl text-center">
          <h1 className="text-3xl font-bold text-slate-900 md:text-4xl">
            Frequently Asked Questions
          </h1>
          <p className="mt-3 text-sm text-slate-600 md:text-base">
            Find quick answers about Nizamify products, smart automation services, delivery,
            support, and contact options.
          </p>
        </div>

        <div className="mx-auto mt-10 max-w-4xl space-y-4">
          {faqs.map((item, index) => (
            <div key={item.question} className="rounded-xl border bg-white p-5 shadow-sm">
              <div className="flex gap-3">
                <span className="mt-0.5 flex size-7 shrink-0 items-center justify-center rounded-full bg-primary/10 text-sm font-bold text-primary">
                  {index + 1}
                </span>
                <div>
                  <h2 className="text-base font-semibold text-slate-900 md:text-lg">
                    {item.question}
                  </h2>
                  <p className="mt-2 text-sm leading-6 text-slate-600">{item.answer}</p>
                </div>
              </div>
            </div>
          ))}
        </div>

        <div className="mx-auto mt-10 max-w-4xl rounded-xl border bg-white p-6 text-center shadow-sm">
          <h2 className="text-xl font-bold text-slate-900">Still need help?</h2>
          <p className="mt-2 text-sm text-slate-600">
            Send us your question and our team will get back to you quickly.
          </p>
          <div className="mt-5">
            <Link
              href="/contact-us"
              className="inline-flex h-10 items-center justify-center rounded-md bg-primary px-6 text-sm font-medium text-primary-foreground transition hover:bg-primary/90"
            >
              Contact Us
            </Link>
          </div>
        </div>
      </div>
    </section>
    </>
  );
}