import React, { useState } from 'react';
import { Link } from 'react-router-dom';
import { useTranslation } from 'react-i18next';
import { PublicLayout } from '../../components/layout';
import {
  FileText,
  BookOpen,
  Rocket,
  Package,
  BarChart3,
  Settings,
  Users,
  Shield,
  CreditCard,
  HelpCircle,
  ChevronRight,
  PlayCircle,
  CheckCircle2,
  AlertCircle,
  Info,
  Download,
  ExternalLink
} from 'lucide-react';

interface DocSection {
  id: string;
  title: string;
  description: string;
  icon: React.ComponentType<{ className?: string }>;
  articles: DocArticle[];
}

interface DocArticle {
  id: string;
  title: string;
  description: string;
  readTime: string;
  difficulty: 'beginner' | 'intermediate' | 'advanced';
}

const DocumentationPage: React.FC = () => {
  const { t } = useTranslation();
  const [activeSection, setActiveSection] = useState<string>('getting-started');

  const sections: DocSection[] = [
    {
      id: 'getting-started',
      title: 'Primeros Pasos',
      description: 'Todo lo que necesitas para comenzar',
      icon: Rocket,
      articles: [
        {
          id: 'quick-start',
          title: 'Guía de Inicio Rápido',
          description: 'Configura tu cuenta en 10 minutos',
          readTime: '5 min',
          difficulty: 'beginner',
        },
        {
          id: 'import-data',
          title: 'Importar Datos Históricos',
          description: 'Cómo subir tu historial de ventas desde Excel o TPV',
          readTime: '8 min',
          difficulty: 'beginner',
        },
        {
          id: 'products-catalog',
          title: 'Configurar Catálogo de Productos',
          description: 'Añade tus productos, recetas e ingredientes',
          readTime: '6 min',
          difficulty: 'beginner',
        },
        {
          id: 'first-prediction',
          title: 'Tu Primera Predicción',
          description: 'Interpreta y ajusta las predicciones de demanda',
          readTime: '10 min',
          difficulty: 'beginner',
        },
      ],
    },
    {
      id: 'features',
      title: 'Funcionalidades',
      description: 'Guías detalladas de cada módulo',
      icon: Package,
      articles: [
        {
          id: 'demand-forecasting',
          title: 'Predicción de Demanda con IA',
          description: 'Cómo funciona el algoritmo y cómo sacarle el máximo partido',
          readTime: '12 min',
          difficulty: 'intermediate',
        },
        {
          id: 'production-planning',
          title: 'Planificación de Producción',
          description: 'Optimiza tu horneado diario basándote en predicciones',
          readTime: '10 min',
          difficulty: 'intermediate',
        },
        {
          id: 'inventory-management',
          title: 'Gestión de Inventario',
          description: 'Controla stock, proveedores y compras',
          readTime: '9 min',
          difficulty: 'intermediate',
        },
        {
          id: 'pos-integration',
          title: 'Punto de Venta (TPV)',
          description: 'Registra ventas y sincroniza con predicciones',
          readTime: '8 min',
          difficulty: 'beginner',
        },
        {
          id: 'waste-tracking',
          title: 'Seguimiento de Desperdicios',
          description: 'Mide, analiza y reduce el desperdicio alimentario',
          readTime: '7 min',
          difficulty: 'beginner',
        },
      ],
    },
    {
      id: 'analytics',
      title: 'Análisis e Insights',
      description: 'Interpreta tus datos y métricas',
      icon: BarChart3,
      articles: [
        {
          id: 'dashboard-overview',
          title: 'Panel de Control',
          description: 'Entiende todas las métricas clave de un vistazo',
          readTime: '8 min',
          difficulty: 'beginner',
        },
        {
          id: 'reports',
          title: 'Informes y Reportes',
          description: 'Genera y exporta informes personalizados',
          readTime: '10 min',
          difficulty: 'intermediate',
        },
        {
          id: 'ai-insights',
          title: 'Insights de IA',
          description: 'Descubre patrones y oportunidades automáticamente',
          readTime: '12 min',
          difficulty: 'advanced',
        },
        {
          id: 'performance-metrics',
          title: 'Métricas de Rendimiento',
          description: 'KPIs clave: márgenes, rotación, precisión de predicciones',
          readTime: '15 min',
          difficulty: 'intermediate',
        },
      ],
    },
    {
      id: 'account',
      title: 'Gestión de Cuenta',
      description: 'Administra tu perfil y equipo',
      icon: Settings,
      articles: [
        {
          id: 'profile-settings',
          title: 'Configuración de Perfil',
          description: 'Actualiza tu información personal y preferencias',
          readTime: '4 min',
          difficulty: 'beginner',
        },
        {
          id: 'team-management',
          title: 'Gestionar Equipo',
          description: 'Añade miembros, asigna roles y permisos',
          readTime: '7 min',
          difficulty: 'intermediate',
        },
        {
          id: 'notifications',
          title: 'Notificaciones y Alertas',
          description: 'Configura cómo y cuándo recibir avisos',
          readTime: '5 min',
          difficulty: 'beginner',
        },
        {
          id: 'integrations',
          title: 'Integraciones',
          description: 'Conecta con TPV, contabilidad y otras herramientas',
          readTime: '10 min',
          difficulty: 'advanced',
        },
      ],
    },
    {
      id: 'billing',
      title: 'Facturación y Planes',
      description: 'Suscripciones, pagos y facturas',
      icon: CreditCard,
      articles: [
        {
          id: 'pricing-plans',
          title: 'Planes y Precios',
          description: 'Compara características de cada plan',
          readTime: '6 min',
          difficulty: 'beginner',
        },
        {
          id: 'payment-methods',
          title: 'Métodos de Pago',
          description: 'Añade, modifica o elimina tarjetas',
          readTime: '4 min',
          difficulty: 'beginner',
        },
        {
          id: 'invoices',
          title: 'Facturas y Recibos',
          description: 'Descarga y gestiona tu historial de facturación',
          readTime: '5 min',
          difficulty: 'beginner',
        },
        {
          id: 'cancel-subscription',
          title: 'Cancelar o Pausar Suscripción',
          description: 'Cómo proceder si necesitas hacer una pausa',
          readTime: '3 min',
          difficulty: 'beginner',
        },
      ],
    },
    {
      id: 'privacy',
      title: 'Privacidad y Seguridad',
      description: 'RGPD, datos y cumplimiento',
      icon: Shield,
      articles: [
        {
          id: 'gdpr-compliance',
          title: 'Cumplimiento RGPD',
          description: 'Cómo protegemos tus datos según normativa europea',
          readTime: '10 min',
          difficulty: 'intermediate',
        },
        {
          id: 'data-export',
          title: 'Exportar Tus Datos',
          description: 'Descarga toda tu información en cualquier momento',
          readTime: '5 min',
          difficulty: 'beginner',
        },
        {
          id: 'data-deletion',
          title: 'Eliminar Tu Cuenta',
          description: 'Procedimiento para borrar permanentemente tus datos',
          readTime: '6 min',
          difficulty: 'beginner',
        },
        {
          id: 'security-best-practices',
          title: 'Mejores Prácticas de Seguridad',
          description: 'Protege tu cuenta con 2FA y contraseñas fuertes',
          readTime: '8 min',
          difficulty: 'intermediate',
        },
      ],
    },
  ];

  const activeContent = sections.find((s) => s.id === activeSection);

  const getDifficultyColor = (difficulty: string) => {
    switch (difficulty) {
      case 'beginner':
        return 'text-green-600 dark:text-green-400';
      case 'intermediate':
        return 'text-amber-600 dark:text-amber-400';
      case 'advanced':
        return 'text-red-600 dark:text-red-400';
      default:
        return 'text-[var(--text-tertiary)]';
    }
  };

  const getDifficultyLabel = (difficulty: string) => {
    switch (difficulty) {
      case 'beginner':
        return 'Principiante';
      case 'intermediate':
        return 'Intermedio';
      case 'advanced':
        return 'Avanzado';
      default:
        return difficulty;
    }
  };

  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">
              <FileText className="w-4 h-4" />
              <span>Documentación</span>
            </div>
            <h1 className="text-4xl lg:text-6xl font-extrabold text-[var(--text-primary)] mb-6">
              Guías Completas Para
              <span className="block text-[var(--color-primary)]">Dominar Panadería IA</span>
            </h1>
            <p className="text-xl text-[var(--text-secondary)] leading-relaxed mb-8">
              Tutoriales paso a paso, mejores prácticas y trucos para aprovechar al máximo la plataforma
            </p>

            {/* Quick Actions */}
            <div className="flex flex-wrap justify-center gap-4">
              <a
                href="#getting-started"
                onClick={() => setActiveSection('getting-started')}
                className="inline-flex items-center gap-2 px-6 py-3 bg-[var(--color-primary)] text-white rounded-xl font-bold hover:shadow-xl transition-all hover:scale-105"
              >
                <Rocket className="w-5 h-5" />
                <span>Comenzar</span>
              </a>
              <Link
                to="/help"
                className="inline-flex items-center gap-2 px-6 py-3 border-2 border-[var(--border-primary)] text-[var(--text-primary)] rounded-xl font-medium hover:border-[var(--color-primary)] transition-all"
              >
                <HelpCircle className="w-5 h-5" />
                <span>Centro de Ayuda</span>
              </Link>
            </div>
          </div>
        </div>
      </section>

      {/* Documentation Content */}
      <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="grid lg:grid-cols-4 gap-8">
            {/* Sidebar Navigation */}
            <div className="lg:col-span-1">
              <div className="sticky top-8">
                <h3 className="text-lg font-bold text-[var(--text-primary)] mb-4">
                  Secciones
                </h3>
                <nav className="space-y-2">
                  {sections.map((section) => (
                    <button
                      key={section.id}
                      onClick={() => setActiveSection(section.id)}
                      className={`w-full flex items-center gap-3 px-4 py-3 rounded-xl text-left transition-all ${
                        activeSection === section.id
                          ? 'bg-[var(--color-primary)] text-white'
                          : 'bg-[var(--bg-secondary)] text-[var(--text-primary)] hover:bg-[var(--color-primary)]/10'
                      }`}
                    >
                      <section.icon className="w-5 h-5 flex-shrink-0" />
                      <div className="flex-1 min-w-0">
                        <div className="font-medium truncate">{section.title}</div>
                        <div
                          className={`text-xs ${
                            activeSection === section.id ? 'text-white/80' : 'text-[var(--text-tertiary)]'
                          }`}
                        >
                          {section.articles.length} artículos
                        </div>
                      </div>
                    </button>
                  ))}
                </nav>

                {/* Quick Links */}
                <div className="mt-8 p-4 bg-[var(--bg-secondary)] rounded-xl border border-[var(--border-primary)]">
                  <h4 className="font-bold text-[var(--text-primary)] mb-3 text-sm">
                    Enlaces Rápidos
                  </h4>
                  <div className="space-y-2 text-sm">
                    <Link
                      to="/help"
                      className="flex items-center gap-2 text-[var(--text-secondary)] hover:text-[var(--color-primary)] transition-colors"
                    >
                      <HelpCircle className="w-4 h-4" />
                      <span>Centro de Ayuda</span>
                    </Link>
                    <Link
                      to="/help/support"
                      className="flex items-center gap-2 text-[var(--text-secondary)] hover:text-[var(--color-primary)] transition-colors"
                    >
                      <ExternalLink className="w-4 h-4" />
                      <span>Contactar Soporte</span>
                    </Link>
                    <a
                      href="#"
                      className="flex items-center gap-2 text-[var(--text-secondary)] hover:text-[var(--color-primary)] transition-colors"
                    >
                      <Download className="w-4 h-4" />
                      <span>Descargar PDF</span>
                    </a>
                  </div>
                </div>
              </div>
            </div>

            {/* Main Content */}
            <div className="lg:col-span-3">
              {activeContent && (
                <div>
                  {/* Section Header */}
                  <div className="mb-8">
                    <div className="flex items-center gap-4 mb-4">
                      <div className="w-12 h-12 bg-[var(--color-primary)]/10 rounded-xl flex items-center justify-center">
                        <activeContent.icon className="w-6 h-6 text-[var(--color-primary)]" />
                      </div>
                      <div>
                        <h2 className="text-3xl font-extrabold text-[var(--text-primary)]">
                          {activeContent.title}
                        </h2>
                        <p className="text-[var(--text-secondary)]">{activeContent.description}</p>
                      </div>
                    </div>
                  </div>

                  {/* Articles Grid */}
                  <div className="space-y-4">
                    {activeContent.articles.map((article) => (
                      <a
                        key={article.id}
                        href={`#${article.id}`}
                        className="block bg-[var(--bg-secondary)] rounded-xl p-6 border border-[var(--border-primary)] hover:border-[var(--color-primary)] hover:shadow-xl transition-all group"
                      >
                        <div className="flex items-start justify-between gap-4">
                          <div className="flex-1">
                            <h3 className="text-xl font-bold text-[var(--text-primary)] mb-2 group-hover:text-[var(--color-primary)] transition-colors">
                              {article.title}
                            </h3>
                            <p className="text-[var(--text-secondary)] mb-4">{article.description}</p>
                            <div className="flex items-center gap-4 text-sm">
                              <span className="flex items-center gap-1 text-[var(--text-tertiary)]">
                                <PlayCircle className="w-4 h-4" />
                                {article.readTime}
                              </span>
                              <span className={`font-medium ${getDifficultyColor(article.difficulty)}`}>
                                {getDifficultyLabel(article.difficulty)}
                              </span>
                            </div>
                          </div>
                          <ChevronRight className="w-6 h-6 text-[var(--text-tertiary)] group-hover:text-[var(--color-primary)] transition-colors flex-shrink-0" />
                        </div>
                      </a>
                    ))}
                  </div>
                </div>
              )}
            </div>
          </div>
        </div>
      </section>

      {/* Video Tutorials */}
      <section className="py-20 bg-[var(--bg-secondary)]">
        <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">
              Tutoriales en Vídeo
            </h2>
            <p className="text-xl text-[var(--text-secondary)]">
              Aprende viendo (próximamente)
            </p>
          </div>

          <div className="grid md:grid-cols-3 gap-6">
            {[
              {
                title: 'Configuración Inicial',
                duration: '5:30',
                thumbnail: 'getting-started',
              },
              {
                title: 'Importar Datos Históricos',
                duration: '8:15',
                thumbnail: 'import-data',
              },
              {
                title: 'Tu Primera Predicción',
                duration: '12:00',
                thumbnail: 'first-prediction',
              },
            ].map((video, index) => (
              <div
                key={index}
                className="bg-[var(--bg-primary)] rounded-xl overflow-hidden border border-[var(--border-primary)] hover:shadow-xl transition-all group"
              >
                <div className="aspect-video bg-gradient-to-br from-[var(--color-primary)]/20 to-orange-600/20 flex items-center justify-center relative">
                  <PlayCircle className="w-16 h-16 text-[var(--color-primary)] group-hover:scale-110 transition-transform" />
                  <div className="absolute bottom-2 right-2 bg-black/80 text-white px-2 py-1 rounded text-xs font-medium">
                    {video.duration}
                  </div>
                </div>
                <div className="p-4">
                  <h3 className="font-bold text-[var(--text-primary)] group-hover:text-[var(--color-primary)] transition-colors">
                    {video.title}
                  </h3>
                </div>
              </div>
            ))}
          </div>
        </div>
      </section>

      {/* Additional Resources */}
      <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">
              Recursos Adicionales
            </h2>
          </div>

          <div className="grid md:grid-cols-3 gap-6">
            <div className="bg-blue-50 dark:bg-blue-900/20 rounded-2xl p-6 border-2 border-blue-200 dark:border-blue-800">
              <Info className="w-8 h-8 text-blue-600 mb-4" />
              <h3 className="text-lg font-bold text-[var(--text-primary)] mb-2">
                Glosario de Términos
              </h3>
              <p className="text-sm text-[var(--text-secondary)] mb-4">
                Definiciones de conceptos clave y terminología técnica
              </p>
              <a href="#glossary" className="text-blue-600 hover:underline font-medium text-sm">
                Ver Glosario →
              </a>
            </div>

            <div className="bg-amber-50 dark:bg-amber-900/20 rounded-2xl p-6 border-2 border-amber-200 dark:border-amber-800">
              <AlertCircle className="w-8 h-8 text-amber-600 mb-4" />
              <h3 className="text-lg font-bold text-[var(--text-primary)] mb-2">
                Solución de Problemas
              </h3>
              <p className="text-sm text-[var(--text-secondary)] mb-4">
                Errores comunes y cómo resolverlos rápidamente
              </p>
              <Link to="/help" className="text-amber-600 hover:underline font-medium text-sm">
                Ver Soluciones →
              </Link>
            </div>

            <div className="bg-green-50 dark:bg-green-900/20 rounded-2xl p-6 border-2 border-green-200 dark:border-green-800">
              <CheckCircle2 className="w-8 h-8 text-green-600 mb-4" />
              <h3 className="text-lg font-bold text-[var(--text-primary)] mb-2">
                Mejores Prácticas
              </h3>
              <p className="text-sm text-[var(--text-secondary)] mb-4">
                Tips y consejos de expertos para optimizar tu uso
              </p>
              <a href="#best-practices" className="text-green-600 hover:underline font-medium text-sm">
                Leer Tips →
              </a>
            </div>
          </div>
        </div>
      </section>

      {/* CTA */}
      <section className="py-20 bg-gradient-to-r from-[var(--color-primary)] to-orange-600">
        <div className="max-w-4xl mx-auto px-4 sm:px-6 lg:px-8 text-center">
          <h2 className="text-3xl lg:text-4xl font-bold text-white mb-6">
            ¿Listo Para Empezar?
          </h2>
          <p className="text-xl text-white/90 mb-8">
            Regístrate en el programa piloto y obtén 3 meses gratis
          </p>
          <Link
            to="/register"
            className="inline-flex items-center gap-2 px-8 py-4 bg-white text-[var(--color-primary)] rounded-xl font-bold hover:shadow-2xl transition-all hover:scale-105"
          >
            <span>Crear Cuenta Gratis</span>
            <ChevronRight className="w-5 h-5" />
          </Link>
        </div>
      </section>
    </PublicLayout>
  );
};

export default DocumentationPage;
