'use client';

import { useEffect, useState } from 'react';
import { getTranslation, Language, useLanguage } from '@/lib/i18n';
import Link from 'next/link';
import { ChevronDown, Search, MessageCircle } from 'lucide-react';

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

interface FAQCategory {
  title: string;
  items: FAQItem[];
}

const styles = `
  .help-hero {
    padding: 60px 20px;
    background: linear-gradient(to bottom right, rgb(20, 113, 76), transparent);
    text-align: center;
    color: #111827;
  }
  
  .dark .help-hero {
    color: #f3f4f6;
  }
  
  .help-hero h1 {
    font-size: 2.5rem;
    font-weight: 700;
    margin-bottom: 16px;
    color: #111827;
  }
  
  .dark .help-hero h1 {
    color: #f3f4f6;
  }
  
  .help-hero p {
    font-size: 1.125rem;
    color: #6b7280;
  }
  
  .dark .help-hero p {
    color: #9ca3af;
  }
  
  .search-section {
    padding: 30px 20px;
    background-color: white;
    border-bottom: 1px solid #e5e7eb;
  }
  
  .dark .search-section {
    background-color: #111827;
    border-bottom-color: #374151;
  }
  
  .search-box {
    max-width: 42rem;
    margin: 0 auto;
    position: relative;
  }
  
  .search-box input {
    width: 100%;
    padding: 12px 40px 12px 12px;
    border: 2px solid #e5e7eb;
    border-radius: 12px;
    font-size: 1rem;
    transition: all 0.2s;
    box-sizing: border-box;
    background: white;
    color: #111827;
  }
  
  .dark .search-box input {
    background: #1f2937;
    color: #f3f4f6;
    border-color: #374151;
  }
  
  .search-box input:focus {
    outline: none;
    border-color: rgb(20, 113, 76);
    box-shadow: 0 0 0 3px rgba(20, 113, 76, 0.1);
  }
  
  .search-icon {
    position: absolute;
    right: 12px;
    top: 50%;
    transform: translateY(-50%);
    color: #9ca3af;
    pointer-events: none;
  }
  
  .faq-section {
    padding: 60px 20px;
    background-color: white;
  }
  
  .dark .faq-section {
    background-color: #111827;
  }
  
  .faq-container {
    max-width: 48rem;
    margin: 0 auto;
  }
  
  .faq-category {
    margin-bottom: 40px;
  }
  
  .faq-category h2 {
    font-size: 1.5rem;
    font-weight: 700;
    margin-bottom: 24px;
    color: #111827;
  }
  
  .dark .faq-category h2 {
    color: #f3f4f6;
  }
  
  .faq-item {
    border: 2px solid #e5e7eb;
    border-radius: 8px;
    margin-bottom: 16px;
    overflow: hidden;
    transition: all 0.2s;
  }
  
  .dark .faq-item {
    border-color: #374151;
  }
  
  .faq-item:hover {
    border-color: rgb(20, 113, 76);
  }
  
  .faq-question {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px;
    background-color: white;
    cursor: pointer;
    transition: all 0.2s;
    border: none;
    width: 100%;
    text-align: left;
    font-weight: 600;
    color: #111827;
  }
  
  .dark .faq-question {
    background-color: #1f2937;
    color: #f3f4f6;
  }
  
  .faq-question:hover {
    background-color: #f9fafb;
  }
  
  .dark .faq-question:hover {
    background-color: #374151;
  }
  
  .faq-chevron {
    transition: transform 0.3s;
    flex-shrink: 0;
    margin-left: 12px;
  }
  
  .faq-chevron.open {
    transform: rotate(180deg);
  }
  
  .faq-answer {
    padding: 16px;
    background-color: #f9fafb;
    border-top: 1px solid #e5e7eb;
    color: #6b7280;
    font-size: 0.875rem;
    line-height: 1.6;
  }
  
  .dark .faq-answer {
    background-color: #0f1117;
    border-top-color: #374151;
    color: #9ca3af;
  }
  
  .help-section {
    padding: 60px 20px;
    background-color: #f9fafb;
  }
  
  .dark .help-section {
    background-color: #0f1117;
  }
  
  .help-card {
    max-width: 42rem;
    margin: 0 auto;
    background: white;
    border-radius: 18px;
    padding: 32px;
    border: 1px solid #e5e7eb;
    text-align: center;
  }
  
  .dark .help-card {
    background: #1f2937;
    border-color: #374151;
  }
  
  .help-icon {
    width: 64px;
    height: 64px;
    background: rgb(20, 113, 76);
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    margin: 0 auto 16px;
  }
  
  .help-card h2 {
    font-size: 1.5rem;
    font-weight: 700;
    margin-bottom: 12px;
    color: #111827;
  }
  
  .dark .help-card h2 {
    color: #f3f4f6;
  }
  
  .help-card p {
    color: #6b7280;
    margin-bottom: 24px;
  }
  
  .dark .help-card p {
    color: #9ca3af;
  }
  
  .button-primary {
    display: inline-block;
    padding: 12px 32px;
    background-color: rgb(20, 113, 76);
    color: white;
    font-weight: 700;
    border-radius: 12px;
    text-decoration: none;
    transition: all 0.2s;
  }
  
  .button-primary:hover {
    background-color: rgb(16, 89, 60);
    transform: scale(1.05);
  }
  
  .no-results {
    text-align: center;
    padding: 40px 20px;
    color: #6b7280;
  }
  
  .dark .no-results {
    color: #9ca3af;
  }
  
  @media (max-width: 768px) {
    .help-hero h1 {
      font-size: 1.875rem;
    }
  }
`;

export default function Help() {
  const [lang, setLang] = useState<Language>('en');
  const [openIndexes, setOpenIndexes] = useState<number[]>([]);
  const [searchTerm, setSearchTerm] = useState('');
  const { getLang } = useLanguage();
  const t = (key: string) => getTranslation(lang, key);

  useEffect(() => {
    setLang(getLang());
    const handleLanguageChange = (e: any) => setLang(e.detail);
    window.addEventListener('languageChange', handleLanguageChange);
    return () => window.removeEventListener('languageChange', handleLanguageChange);
  }, [getLang]);

  const faqCategories: FAQCategory[] = [
    {
      title: t('help.bookingFaq'),
      items: [
        { question: t('help.how'), answer: t('help.howAnswer') },
        { question: t('help.modify'), answer: t('help.modifyAnswer') },
        { question: t('help.cancel'), answer: t('help.cancelAnswer') },
      ],
    },
    {
      title: t('help.paymentFaq'),
      items: [
        { question: t('help.payMethods'), answer: t('help.payMethodsAnswer') },
        { question: t('help.refund'), answer: t('help.refundAnswer') },
      ],
    },
    {
      title: t('help.other'),
      items: [
        { question: t('help.docs'), answer: t('help.docsAnswer') },
      ],
    },
  ];

  const toggleFAQ = (index: number) => {
    if (openIndexes.includes(index)) {
      setOpenIndexes(openIndexes.filter(i => i !== index));
    } else {
      setOpenIndexes([...openIndexes, index]);
    }
  };

  let flatFAQs: (FAQItem & { id: number })[] = [];
  faqCategories.forEach((cat) => {
    cat.items.forEach((item) => {
      flatFAQs.push({ ...item, id: flatFAQs.length });
    });
  });

  const filteredFAQs = flatFAQs.filter(
    (faq) =>
      faq.question.toLowerCase().includes(searchTerm.toLowerCase()) ||
      faq.answer.toLowerCase().includes(searchTerm.toLowerCase())
  );

  return (
    <>
      <style>{styles}</style>
      <main>
        <section className="help-hero">
          <h1>{t('help.title')}</h1>
          <p>{t('help.subtitle')}</p>
        </section>

        <section className="search-section">
          <div className="search-box">
            <input
              type="text"
              placeholder="Search FAQs..."
              value={searchTerm}
              onChange={(e) => setSearchTerm(e.target.value)}
            />
            <Search className="search-icon" size={20} />
          </div>
        </section>

        <section className="faq-section">
          <div className="faq-container">
            {searchTerm ? (
              <>
                {filteredFAQs.length > 0 ? (
                  filteredFAQs.map((faq) => (
                    <div key={faq.id} className="faq-item">
                      <div className="faq-question">
                        <span>{faq.question}</span>
                      </div>
                      <div className="faq-answer">{faq.answer}</div>
                    </div>
                  ))
                ) : (
                  <div className="no-results">
                    No FAQs found for &quot;{searchTerm}&quot;
                  </div>
                )}
              </>
            ) : (
              faqCategories.map((category, catIdx) => (
                <div key={catIdx} className="faq-category">
                  <h2>{category.title}</h2>
                  {category.items.map((item, itemIdx) => {
                    const globalIdx = catIdx * 100 + itemIdx;
                    const isOpen = openIndexes.includes(globalIdx);
                    return (
                      <div key={itemIdx} className="faq-item">
                        <button
                          className="faq-question"
                          onClick={() => toggleFAQ(globalIdx)}
                        >
                          <span>{item.question}</span>
                          <ChevronDown className={`faq-chevron ${isOpen ? 'open' : ''}`} size={20} color="rgb(20, 113, 76)" />
                        </button>
                        {isOpen && <div className="faq-answer">{item.answer}</div>}
                      </div>
                    );
                  })}
                </div>
              ))
            )}
          </div>
        </section>

        <section className="help-section">
          <div className="help-card">
            <div className="help-icon">
              <MessageCircle size={32} color="white" />
            </div>
            <h2>{t('help.help')}</h2>
            <p>{t('help.contact')}</p>
            <Link href="/contact" className="button-primary">Contact Support</Link>
          </div>
        </section>
      </main>
    </>
  );
}
