import React, { useState } from 'react';
import { Link } from 'react-router-dom';
import { useTranslation } from 'react-i18next';
import { PublicLayout } from '../../components/layout';
import {
  HelpCircle,
  Search,
  BookOpen,
  MessageSquare,
  FileText,
  Users,
  Settings,
  CreditCard,
  Shield,
  TrendingUp,
  Package,
  ChevronDown,
  ChevronUp,
  ExternalLink,
  Mail,
  Phone,
  Clock
} from 'lucide-react';

interface FAQItem {
  question: string;
  answer: string;
  category: string;
}

interface HelpCategory {
  id: string;
  title: string;
  description: string;
  icon: React.ComponentType<{ className?: string }>;
  link: string;
}

const HelpCenterPage: React.FC = () => {
  const { t } = useTranslation('help');
  const [searchQuery, setSearchQuery] = useState('');
  const [expandedFAQ, setExpandedFAQ] = useState<number | null>(null);

  const categories: HelpCategory[] = [
    {
      id: 'getting-started',
      title: t('categories.gettingStarted.title'),
      description: t('categories.gettingStarted.description'),
      icon: BookOpen,
      link: '/help/docs#getting-started',
    },
    {
      id: 'features',
      title: t('categories.features.title'),
      description: t('categories.features.description'),
      icon: Package,
      link: '/help/docs#features',
    },
    {
      id: 'billing',
      title: t('categories.billing.title'),
      description: t('categories.billing.description'),
      icon: CreditCard,
      link: '/help/docs#billing',
    },
    {
      id: 'account',
      title: t('categories.account.title'),
      description: t('categories.account.description'),
      icon: Settings,
      link: '/help/docs#account',
    },
    {
      id: 'privacy',
      title: t('categories.privacy.title'),
      description: t('categories.privacy.description'),
      icon: Shield,
      link: '/help/docs#privacy',
    },
    {
      id: 'analytics',
      title: t('categories.analytics.title'),
      description: t('categories.analytics.description'),
      icon: TrendingUp,
      link: '/help/docs#analytics',
    },
  ];

  const faqs: FAQItem[] = t('faqs', { returnObjects: true }) as FAQItem[];

  const filteredFAQs = searchQuery
    ? faqs.filter(
      (faq) =>
        faq.question.toLowerCase().includes(searchQuery.toLowerCase()) ||
        faq.answer.toLowerCase().includes(searchQuery.toLowerCase())
    )
    : faqs;

  const toggleFAQ = (index: number) => {
    setExpandedFAQ(expandedFAQ === index ? null : index);
  };

  return (
    <PublicLayout
      variant="default"
      contentPadding="md"
      headerProps={{
        showThemeToggle: true,
        showAuthButtons: true,
        showLanguageSelector: true,
        variant: 'default',
      }}
    >
      {/* Hero Section */}
      <section className="bg-gradient-to-br from-[var(--bg-primary)] via-[var(--bg-secondary)] to-[var(--color-primary)]/5 py-20">
        <div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
          <div className="text-center max-w-4xl mx-auto">
            <div className="inline-flex items-center gap-2 bg-[var(--color-primary)]/10 text-[var(--color-primary)] px-4 py-2 rounded-full text-sm font-medium mb-6">
              <HelpCircle className="w-4 h-4" />
              <span>{t('helpCenter.subtitle')}</span>
            </div>
            <h1 className="text-4xl lg:text-6xl font-extrabold text-[var(--text-primary)] mb-6">
              {t('helpCenter.title')}
            </h1>
            <p className="text-xl text-[var(--text-secondary)] leading-relaxed mb-8">
              {t('helpCenter.description')}
            </p>

            {/* Search Bar */}
            <div className="max-w-2xl mx-auto">
              <div className="relative">
                <Search className="absolute left-4 top-1/2 -translate-y-1/2 w-5 h-5 text-[var(--text-tertiary)]" />
                <input
                  type="text"
                  placeholder={t('helpCenter.searchPlaceholder')}
                  value={searchQuery}
                  onChange={(e) => setSearchQuery(e.target.value)}
                  className="w-full pl-12 pr-4 py-4 bg-[var(--bg-primary)] border-2 border-[var(--border-primary)] rounded-xl text-[var(--text-primary)] placeholder:text-[var(--text-tertiary)] focus:outline-none focus:border-[var(--color-primary)] transition-colors"
                />
              </div>
            </div>
          </div>
        </div>
      </section>

      {/* Quick Links / Categories */}
      <section className="py-20 bg-[var(--bg-primary)]">
        <div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
          <div className="text-center mb-12">
            <h2 className="text-3xl lg:text-4xl font-extrabold text-[var(--text-primary)] mb-4">
              {t('helpCenter.categoriesTitle')}
            </h2>
            <p className="text-xl text-[var(--text-secondary)]">
              {t('helpCenter.categoriesSubtitle')}
            </p>
          </div>

          <div className="grid md:grid-cols-2 lg:grid-cols-3 gap-6">
            {categories.map((category) => (
              <Link
                key={category.id}
                to={category.link}
                className="bg-[var(--bg-secondary)] rounded-2xl p-6 border border-[var(--border-primary)] hover:border-[var(--color-primary)] hover:shadow-xl transition-all duration-300 group"
              >
                <div className="flex items-start gap-4">
                  <div className="w-12 h-12 bg-[var(--color-primary)]/10 rounded-xl flex items-center justify-center flex-shrink-0 group-hover:bg-[var(--color-primary)] transition-colors">
                    <category.icon className="w-6 h-6 text-[var(--color-primary)] group-hover:text-white transition-colors" />
                  </div>
                  <div className="flex-1">
                    <h3 className="text-lg font-bold text-[var(--text-primary)] mb-2 group-hover:text-[var(--color-primary)] transition-colors">
                      {category.title}
                    </h3>
                    <p className="text-sm text-[var(--text-secondary)]">{category.description}</p>
                  </div>
                </div>
              </Link>
            ))}
          </div>
        </div>
      </section>

      {/* FAQs */}
      <section className="py-20 bg-[var(--bg-secondary)]">
        <div className="max-w-4xl mx-auto px-4 sm:px-6 lg:px-8">
          <div className="text-center mb-12">
            <h2 className="text-3xl lg:text-4xl font-extrabold text-[var(--text-primary)] mb-4">
              {t('helpCenter.faqTitle')}
            </h2>
            <p className="text-xl text-[var(--text-secondary)]">
              {t('helpCenter.faqResultsCount', { count: filteredFAQs.length })}{' '}
              {searchQuery && t('helpCenter.faqFound')}
            </p>
          </div>

          <div className="space-y-4">
            {filteredFAQs.map((faq, index) => (
              <div
                key={index}
                className="bg-[var(--bg-primary)] rounded-xl border border-[var(--border-primary)] overflow-hidden"
              >
                <button
                  onClick={() => toggleFAQ(index)}
                  className="w-full px-6 py-4 flex items-center justify-between text-left hover:bg-[var(--bg-secondary)] transition-colors"
                >
                  <span className="text-lg font-bold text-[var(--text-primary)] pr-4">
                    {faq.question}
                  </span>
                  {expandedFAQ === index ? (
                    <ChevronUp className="w-5 h-5 text-[var(--color-primary)] flex-shrink-0" />
                  ) : (
                    <ChevronDown className="w-5 h-5 text-[var(--text-tertiary)] flex-shrink-0" />
                  )}
                </button>
                {expandedFAQ === index && (
                  <div className="px-6 pb-4 text-[var(--text-secondary)] leading-relaxed">
                    {faq.answer}
                  </div>
                )}
              </div>
            ))}
          </div>

          {filteredFAQs.length === 0 && (
            <div className="text-center py-12">
              <HelpCircle className="w-16 h-16 text-[var(--text-tertiary)] mx-auto mb-4" />
              <p className="text-[var(--text-secondary)] text-lg">
                {t('helpCenter.noResultsTitle')} "{searchQuery}".{' '}
                <Link to="/help/support" className="text-[var(--color-primary)] hover:underline">
                  {t('helpCenter.noResultsAction')}
                </Link>
              </p>
            </div>
          )}
        </div>
      </section>

      {/* Contact Support */}
      <section className="py-20 bg-[var(--bg-primary)]">
        <div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
          <div className="text-center mb-12">
            <h2 className="text-3xl lg:text-4xl font-extrabold text-[var(--text-primary)] mb-4">
              {t('helpCenter.contactTitle')}
            </h2>
            <p className="text-xl text-[var(--text-secondary)]">
              {t('helpCenter.contactSubtitle')}
            </p>
          </div>

          <div className="grid md:grid-cols-2 gap-6 max-w-4xl mx-auto">
            <a
              href={`mailto:${t('contact.email.address')}`}
              className="bg-[var(--bg-secondary)] rounded-2xl p-8 border border-[var(--border-primary)] hover:border-[var(--color-primary)] hover:shadow-xl transition-all text-center group"
            >
              <div className="w-16 h-16 bg-[var(--color-primary)]/10 rounded-full flex items-center justify-center mx-auto mb-4 group-hover:bg-[var(--color-primary)] transition-colors">
                <Mail className="w-8 h-8 text-[var(--color-primary)] group-hover:text-white transition-colors" />
              </div>
              <h3 className="text-xl font-bold text-[var(--text-primary)] mb-2">
                {t('contact.email.title')}
              </h3>
              <p className="text-sm text-[var(--text-secondary)] mb-4">
                {t('contact.email.description')}
              </p>
              <span className="text-[var(--color-primary)] font-medium group-hover:underline">
                {t('contact.email.action')} →
              </span>
            </a>

            <Link
              to="/help/docs"
              className="bg-[var(--bg-secondary)] rounded-2xl p-8 border border-[var(--border-primary)] hover:border-[var(--color-primary)] hover:shadow-xl transition-all text-center group"
            >
              <div className="w-16 h-16 bg-[var(--color-primary)]/10 rounded-full flex items-center justify-center mx-auto mb-4 group-hover:bg-[var(--color-primary)] transition-colors">
                <FileText className="w-8 h-8 text-[var(--color-primary)] group-hover:text-white transition-colors" />
              </div>
              <h3 className="text-xl font-bold text-[var(--text-primary)] mb-2">
                {t('contact.documentation.title')}
              </h3>
              <p className="text-sm text-[var(--text-secondary)] mb-4">
                {t('contact.documentation.description')}
              </p>
              <span className="text-[var(--color-primary)] font-medium group-hover:underline">
                {t('contact.documentation.action')} →
              </span>
            </Link>
          </div>
        </div>
      </section>

      {/* Contact Hours */}
      <section className="py-12 bg-[var(--bg-secondary)]">
        <div className="max-w-4xl mx-auto px-4 sm:px-6 lg:px-8">
          <div className="bg-[var(--bg-primary)] rounded-2xl p-8 border border-[var(--border-primary)]">
            <div className="flex items-start gap-4">
              <Clock className="w-6 h-6 text-[var(--color-primary)] flex-shrink-0 mt-1" />
              <div>
                <h3 className="text-lg font-bold text-[var(--text-primary)] mb-2">
                  {t('helpCenter.contactHours')}
                </h3>
                <div className="space-y-1 text-sm text-[var(--text-secondary)]">
                  <p><strong>{t('contact.email.title')}:</strong> {t('contact.hours.email')}</p>
                  <p><strong>Teléfono:</strong> {t('contact.hours.phone')}</p>
                </div>
              </div>
            </div>
          </div>
        </div>
      </section>
    </PublicLayout>
  );
};

export default HelpCenterPage;
