import { useState } from "react";
import { Link } from "react-router";
import { trpc } from "@/providers/trpc";
import { ChevronDown, HelpCircle, ArrowLeft, MessageCircle } from "lucide-react";

const categories = ["General", "Payment", "Delivery", "Support", "Account", "Refund"];

export default function FAQPage() {
  const { data: faqs } = trpc.faq.list.useQuery();
  const [openIndex, setOpenIndex] = useState<number | null>(0);
  const [activeCategory, setActiveCategory] = useState<string>("All");

  // Extended FAQs with all legal/SEO-relevant questions
  const extendedFaqs = [
    { id: 101, question: "Is Deal Stack Tools a legitimate service?", answer: "Yes. Deal Stack Tools is a registered digital marketplace operating since 2026. We provide legitimate private accounts, team access, and licensed subscriptions sourced through official channels. We do not sell cracked, stolen, or cookie-based accounts. Every product is verified before delivery.", category: "General" },
    { id: 102, question: "How does the delivery process work?", answer: "After your payment is verified, we deliver your private account credentials and setup instructions via WhatsApp or Telegram within 2-10 minutes. Our support team guides you through the activation process step-by-step. Most customers are up and running within 5 minutes.", category: "Delivery" },
    { id: 103, question: "What payment methods do you accept?", answer: "We accept UPI (India), PayPal (International), JazzCash, Binance Pay, and cryptocurrency including Bitcoin (BTC), Ethereum (ETH), USDT (TRC20/BEP20/ERC20), Litecoin (LTC), and Solana (SOL). You can also pay directly via WhatsApp for personalized assistance.", category: "Payment" },
    { id: 104, question: "Is my account private or shared?", answer: "Every account we provide is private and individual. You receive your own login credentials that are not shared with anyone else. We do not offer shared or cookie-based accounts.", category: "Account" },
    { id: 105, question: "What happens if my account stops working?", answer: "We offer a 1-year warranty on all products. If your account encounters any issues, contact us via WhatsApp at +91 7038146526 and we will provide a free replacement within 24 hours. No questions asked.", category: "Support" },
    { id: 106, question: "How long does delivery take?", answer: "Most accounts are delivered within 2-10 minutes after payment verification. Some specialized accounts may take up to 30 minutes. License keys are delivered instantly. Delivery is handled via WhatsApp or Telegram.", category: "Delivery" },
    { id: 107, question: "Can I get a refund?", answer: "Yes. If we are unable to deliver the product or if the account is not working and we cannot replace it within 24 hours, you are eligible for a full refund within 7 days of purchase. See our Refund Policy page for complete details.", category: "Refund" },
    { id: 108, question: "Do you offer bulk discounts?", answer: "Yes. We offer special pricing for bulk orders (5+ accounts). Contact us via WhatsApp at +91 7038146526 for a custom quote based on your requirements.", category: "General" },
    { id: 109, question: "How do I contact customer support?", answer: "You can reach our 24/7 support team via WhatsApp at +91 7038146526 or through our Telegram group @teamhydrashopC. We typically respond within 5 minutes. Email support is also available at contact@dealstacktools.shop.", category: "Support" },
    { id: 110, question: "Is my payment information secure?", answer: "Yes. We use industry-standard encryption for all payment processing. We never store your payment card details. UPI and crypto payments are handled through secure, encrypted channels. PayPal payments are protected by PayPal Buyer Protection.", category: "Payment" },
    { id: 111, question: "Can I use these accounts on multiple devices?", answer: "Yes. Most products support multiple device logins as per their official terms. For example, Canva Pro works on any device, Netflix Premium supports 4 screens, and Office 365 works on 5 devices. Specific limits depend on each product.", category: "Account" },
    { id: 112, question: "Do you ship physical products?", answer: "No. All our products are digital. We deliver account credentials, license keys, and setup instructions electronically via WhatsApp or Telegram. There are no shipping costs or delivery delays.", category: "Delivery" },
    { id: 113, question: "What is your privacy policy?", answer: "We collect only the information necessary to process your order and deliver your account. We never sell or share your personal data with third parties. All communications are encrypted. See our full Privacy Policy for details.", category: "General" },
    { id: 114, question: "Can I renew my subscription after 1 year?", answer: "Yes. Contact our support team before your subscription expires and we will help you renew at the same discounted rate. We send renewal reminders via WhatsApp 7 days before expiry.", category: "Account" },
    { id: 115, question: "What currencies do you support?", answer: "We primarily price in USD but accept payments in INR (via UPI), PKR (via JazzCash), and various cryptocurrencies. PayPal handles currency conversion automatically for international customers.", category: "Payment" },
    { id: 116, question: "Do you offer student discounts?", answer: "Yes. We offer additional discounts for students. Contact us via WhatsApp with a valid student ID to receive your student discount code.", category: "General" },
    ...(faqs || []),
  ];

  const filteredFaqs = activeCategory === "All" ? extendedFaqs : extendedFaqs.filter((f) => f.category === activeCategory);

  return (
    <main className="min-h-screen bg-[#050505] pt-24 pb-16">
      <div className="max-w-4xl mx-auto px-4 sm:px-6 lg:px-8">
        <Link to="/" className="inline-flex items-center gap-2 text-sm text-[#a5a5a5] hover:text-white mb-6 transition-colors">
          <ArrowLeft className="w-4 h-4" />
          Back to Home
        </Link>

        {/* Schema Markup */}
        <script type="application/ld+json" dangerouslySetInnerHTML={{
          __html: JSON.stringify({
            "@context": "https://schema.org",
            "@type": "FAQPage",
            mainEntity: extendedFaqs.map((f) => ({
              "@type": "Question",
              name: f.question,
              acceptedAnswer: { "@type": "Answer", text: f.answer },
            })),
          }),
        }} />

        {/* Header */}
        <div className="text-center mb-12">
          <div className="w-12 h-12 rounded-xl bg-[#5b9aff]/10 flex items-center justify-center mx-auto mb-4">
            <HelpCircle className="w-6 h-6 text-[#5b9aff]" />
          </div>
          <h1 className="text-3xl sm:text-4xl font-bold text-white mb-3">
            Frequently Asked Questions
          </h1>
          <p className="text-[#a5a5a5] max-w-lg mx-auto">
            Everything you need to know about buying premium tools through Deal Stack Tools.
            Can&apos;t find your answer? Contact us directly.
          </p>
          <a
            href="https://wa.me/917038146526"
            target="_blank"
            rel="noopener noreferrer"
            className="inline-flex items-center gap-2 mt-4 px-6 py-3 bg-[#25d366]/10 border border-[#25d366]/30 text-[#25d366] rounded-full hover:bg-[#25d366]/20 transition-all text-sm font-medium"
          >
            <MessageCircle className="w-4 h-4" />
            Ask on WhatsApp
          </a>
        </div>

        {/* Category Filters */}
        <div className="flex flex-wrap gap-2 mb-8 justify-center">
          <button
            onClick={() => setActiveCategory("All")}
            className={`px-4 py-2 text-sm rounded-lg transition-colors ${activeCategory === "All" ? "bg-[#5b9aff] text-[#050505] font-medium" : "bg-[#141414] text-[#a5a5a5] hover:text-white"}`}
          >
            All
          </button>
          {categories.map((cat) => (
            <button
              key={cat}
              onClick={() => setActiveCategory(cat)}
              className={`px-4 py-2 text-sm rounded-lg transition-colors ${activeCategory === cat ? "bg-[#5b9aff] text-[#050505] font-medium" : "bg-[#141414] text-[#a5a5a5] hover:text-white"}`}
            >
              {cat}
            </button>
          ))}
        </div>

        {/* FAQ List */}
        <div className="space-y-3">
          {filteredFaqs.map((faq, i) => (
            <div key={faq.id || i} className="glass-card rounded-xl overflow-hidden">
              <button
                onClick={() => setOpenIndex(openIndex === i ? null : i)}
                className="w-full flex items-center justify-between p-5 text-left"
              >
                <span className="font-medium text-white pr-4">{faq.question}</span>
                <ChevronDown className={`w-5 h-5 text-[#5b9aff] shrink-0 transition-transform ${openIndex === i ? "rotate-180" : ""}`} />
              </button>
              {openIndex === i && (
                <div className="px-5 pb-5">
                  <p className="text-sm text-[#a5a5a5] leading-relaxed">{faq.answer}</p>
                </div>
              )}
            </div>
          ))}
        </div>

        {/* Still have questions CTA */}
        <div className="mt-12 glass-card rounded-2xl p-8 text-center">
          <h3 className="text-xl font-semibold text-white mb-2">Still Have Questions?</h3>
          <p className="text-[#a5a5a5] mb-6">Our support team is available 24/7 to help you with anything.</p>
          <div className="flex flex-col sm:flex-row items-center justify-center gap-4">
            <a href="https://wa.me/917038146526" target="_blank" rel="noopener noreferrer" className="flex items-center gap-2 px-6 py-3 bg-[#25d366]/10 border border-[#25d366]/30 text-[#25d366] rounded-full hover:bg-[#25d366]/20 transition-all">
              <MessageCircle className="w-4 h-4" />
              WhatsApp Support
            </a>
            <a href="https://t.me/teamhydrashopC" target="_blank" rel="noopener noreferrer" className="flex items-center gap-2 px-6 py-3 bg-[#0088cc]/10 border border-[#0088cc]/30 text-[#0088cc] rounded-full hover:bg-[#0088cc]/20 transition-all">
              Contact on Telegram
            </a>
          </div>
        </div>
      </div>
    </main>
  );
}
