import React, { forwardRef } from 'react';
import { clsx } from 'clsx';
import { Link } from 'react-router-dom';
import { Button } from '../../ui';
import {
  Heart,
  ExternalLink,
  Mail,
  Phone,
  MapPin,
  Github,
  Twitter,
  Linkedin,
  Globe,
  Shield,
  FileText,
  HelpCircle,
  MessageSquare
} from 'lucide-react';

export interface FooterLink {
  id: string;
  label: string;
  href: string;
  external?: boolean;
  icon?: React.ComponentType<{ className?: string }>;
}

export interface FooterSection {
  id: string;
  title: string;
  links: FooterLink[];
}

export interface SocialLink {
  id: string;
  label: string;
  href: string;
  icon: React.ComponentType<{ className?: string }>;
}

export interface CompanyInfo {
  name: string;
  description?: string;
  logo?: React.ReactNode;
  email?: string;
  phone?: string;
  address?: string;
  website?: string;
}

export interface FooterProps {
  className?: string;
  /**
   * Company information
   */
  companyInfo?: CompanyInfo;
  /**
   * Footer sections with links
   */
  sections?: FooterSection[];
  /**
   * Social media links
   */
  socialLinks?: SocialLink[];
  /**
   * Copyright text (auto-generated if not provided)
   */
  copyrightText?: string;
  /**
   * Show version info
   */
  showVersion?: boolean;
  /**
   * Version string
   */
  version?: string;
  /**
   * Show language selector
   */
  showLanguageSelector?: boolean;
  /**
   * Available languages
   */
  languages?: Array<{ code: string; name: string }>;
  /**
   * Current language
   */
  currentLanguage?: string;
  /**
   * Language change handler
   */
  onLanguageChange?: (language: string) => void;
  /**
   * Show theme toggle
   */
  showThemeToggle?: boolean;
  /**
   * Theme toggle handler
   */
  onThemeToggle?: () => void;
  /**
   * Show privacy links
   */
  showPrivacyLinks?: boolean;
  /**
   * Compact mode (smaller footer)
   */
  compact?: boolean;
  /**
   * Custom content
   */
  children?: React.ReactNode;
}

export interface FooterRef {
  scrollIntoView: () => void;
}

/**
 * Footer - Application footer with links and copyright info
 * 
 * Features:
 * - Company information and branding
 * - Organized link sections for easy navigation
 * - Social media links with proper icons
 * - Copyright notice with automatic year
 * - Version information display
 * - Language selector for i18n
 * - Privacy and legal links
 * - Responsive design with mobile adaptations
 * - Accessible link handling with external indicators
 * - Customizable sections and content
 */
export const Footer = forwardRef<FooterRef, FooterProps>(({
  className,
  companyInfo,
  sections,
  socialLinks,
  copyrightText,
  showVersion = true,
  version = '2.0.0',
  showLanguageSelector = false,
  languages = [],
  currentLanguage = 'es',
  onLanguageChange,
  showThemeToggle = false,
  onThemeToggle,
  showPrivacyLinks = true,
  compact = false,
  children,
}, ref) => {
  const footerRef = React.useRef<HTMLDivElement>(null);
  const currentYear = new Date().getFullYear();

  // Company info - full for public pages, minimal for internal
  const defaultCompanyInfo: CompanyInfo = compact ? {
    name: 'Panadería IA',
  } : {
    name: 'Panadería IA',
    description: 'Sistema inteligente de gestión para panaderías. Optimiza tu producción, inventario y ventas con inteligencia artificial.',
    email: 'contacto@panaderia-ia.com',
    website: 'https://panaderia-ia.com',
  };

  const company = companyInfo || defaultCompanyInfo;

  // Sections - full for public pages, empty for internal
  const defaultSections: FooterSection[] = compact ? [] : [
    {
      id: 'product',
      title: 'Producto',
      links: [
        { id: 'dashboard', label: 'Dashboard', href: '/dashboard' },
        { id: 'inventory', label: 'Inventario', href: '/inventory' },
        { id: 'production', label: 'Producción', href: '/production' },
        { id: 'sales', label: 'Ventas', href: '/sales' },
        { id: 'forecasting', label: 'Predicciones', href: '/forecasting' },
      ],
    },
    {
      id: 'support',
      title: 'Soporte',
      links: [
        { id: 'help', label: 'Centro de Ayuda', href: '/help', icon: HelpCircle },
        { id: 'docs', label: 'Documentación', href: '/help/docs', icon: FileText },
        { id: 'contact', label: 'Contacto', href: '/help/support', icon: MessageSquare },
        { id: 'feedback', label: 'Feedback', href: '/help/feedback' },
      ],
    },
    {
      id: 'company',
      title: 'Empresa',
      links: [
        { id: 'about', label: 'Acerca de', href: '/about', external: true },
        { id: 'blog', label: 'Blog', href: 'https://blog.panaderia-ia.com', external: true },
        { id: 'careers', label: 'Carreras', href: 'https://careers.panaderia-ia.com', external: true },
        { id: 'press', label: 'Prensa', href: '/press', external: true },
      ],
    },
  ];

  const footerSections = sections || defaultSections;

  // Social links - none for internal business application, full set for public pages
  const defaultSocialLinks: SocialLink[] = compact ? [] : [
    {
      id: 'twitter',
      label: 'Twitter',
      href: 'https://twitter.com/panaderia-ia',
      icon: Twitter,
    },
    {
      id: 'linkedin',
      label: 'LinkedIn',
      href: 'https://linkedin.com/company/panaderia-ia',
      icon: Linkedin,
    },
    {
      id: 'github',
      label: 'GitHub',
      href: 'https://github.com/panaderia-ia',
      icon: Github,
    },
  ];

  const socialLinksToShow = socialLinks || defaultSocialLinks;

  // Privacy links
  const privacyLinks: FooterLink[] = [
    { id: 'privacy', label: 'Privacidad', href: '/privacy', icon: Shield },
    { id: 'terms', label: 'Términos', href: '/terms', icon: FileText },
    { id: 'cookies', label: 'Cookies', href: '/cookies' },
  ];

  // Scroll into view
  const scrollIntoView = React.useCallback(() => {
    footerRef.current?.scrollIntoView({ behavior: 'smooth', block: 'start' });
  }, []);

  // Expose ref methods
  React.useImperativeHandle(ref, () => ({
    scrollIntoView,
  }), [scrollIntoView]);

  // Render link
  const renderLink = (link: FooterLink) => {
    const LinkIcon = link.icon;
    
    const linkContent = (
      <span className="flex items-center gap-2 text-sm text-[var(--text-secondary)] hover:text-[var(--text-primary)] transition-colors duration-200">
        {LinkIcon && <LinkIcon className="w-4 h-4" />}
        {link.label}
        {link.external && <ExternalLink className="w-3 h-3" />}
      </span>
    );

    if (link.external) {
      return (
        <a
          key={link.id}
          href={link.href}
          target="_blank"
          rel="noopener noreferrer"
          className="hover:underline focus:outline-none focus:underline"
        >
          {linkContent}
        </a>
      );
    }

    return (
      <Link
        key={link.id}
        to={link.href}
        className="hover:underline focus:outline-none focus:underline"
      >
        {linkContent}
      </Link>
    );
  };

  // Render social link
  const renderSocialLink = (social: SocialLink) => {
    const SocialIcon = social.icon;
    
    return (
      <a
        key={social.id}
        href={social.href}
        target="_blank"
        rel="noopener noreferrer"
        className="p-2 rounded-lg text-[var(--text-secondary)] hover:text-[var(--text-primary)] hover:bg-[var(--bg-secondary)] transition-colors duration-200"
        aria-label={social.label}
      >
        <SocialIcon className="w-5 h-5" />
      </a>
    );
  };

  return (
    <footer
      ref={footerRef}
      className={clsx(
        'bg-[var(--bg-secondary)] border-t border-[var(--border-primary)]',
        'mt-auto', // Push to bottom when using flex layout
        compact ? 'py-6' : 'py-12',
        className
      )}
      role="contentinfo"
    >
      <div className="max-w-7xl mx-auto px-4 lg:px-6">
        {/* Custom children - only show if provided */}
        {children && (
          <div className="mb-6">
            {children}
          </div>
        )}

        {/* Full footer content for public pages */}
        {!compact && (
          <>
            {/* Company info and links */}
            <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-8 mb-8">
              {/* Company info */}
              <div className="space-y-4">
                <div className="flex items-center gap-3">
                  {company.logo}
                  <h3 className="text-lg font-semibold text-[var(--text-primary)]">{company.name}</h3>
                </div>
                {company.description && (
                  <p className="text-sm text-[var(--text-secondary)]">{company.description}</p>
                )}
                {/* Contact info */}
                <div className="space-y-2">
                  {company.email && (
                    <div className="flex items-center gap-2 text-sm text-[var(--text-secondary)]">
                      <Mail className="w-4 h-4" />
                      <a href={`mailto:${company.email}`} className="hover:text-[var(--text-primary)]">
                        {company.email}
                      </a>
                    </div>
                  )}
                  {company.phone && (
                    <div className="flex items-center gap-2 text-sm text-[var(--text-secondary)]">
                      <Phone className="w-4 h-4" />
                      <a href={`tel:${company.phone}`} className="hover:text-[var(--text-primary)]">
                        {company.phone}
                      </a>
                    </div>
                  )}
                  {company.website && (
                    <div className="flex items-center gap-2 text-sm text-[var(--text-secondary)]">
                      <Globe className="w-4 h-4" />
                      <a href={company.website} target="_blank" rel="noopener noreferrer" className="hover:text-[var(--text-primary)]">
                        {company.website}
                      </a>
                    </div>
                  )}
                </div>
              </div>

              {/* Footer sections */}
              {footerSections.map((section) => (
                <div key={section.id} className="space-y-4">
                  <h4 className="text-base font-medium text-[var(--text-primary)]">{section.title}</h4>
                  <nav className="space-y-2">
                    {section.links.map((link) => renderLink(link))}
                  </nav>
                </div>
              ))}
            </div>

            {/* Social links */}
            {socialLinksToShow.length > 0 && (
              <div className="border-t border-[var(--border-primary)] pt-6 mb-6">
                <div className="flex flex-col sm:flex-row items-center justify-between gap-4">
                  <p className="text-sm text-[var(--text-secondary)]">Síguenos en redes sociales</p>
                  <div className="flex items-center gap-2">
                    {socialLinksToShow.map((social) => renderSocialLink(social))}
                  </div>
                </div>
              </div>
            )}
          </>
        )}

        {/* Minimal bottom bar */}
        <div className="flex flex-col sm:flex-row items-center justify-between gap-4 text-sm text-[var(--text-tertiary)]">
          {/* Copyright and version */}
          <div className="flex items-center gap-4">
            <span>© {currentYear} {company.name}</span>
            {showVersion && (
              <span>v{version}</span>
            )}
          </div>

          {/* Essential utilities only */}
          <div className="flex items-center gap-4">
            {/* Privacy links - minimal set */}
            {showPrivacyLinks && (
              <div className="flex items-center gap-3">
                <Link
                  to="/privacy"
                  className="text-[var(--text-tertiary)] hover:text-[var(--text-primary)] transition-colors duration-200"
                >
                  Privacidad
                </Link>
                <Link
                  to="/terms"
                  className="text-[var(--text-tertiary)] hover:text-[var(--text-primary)] transition-colors duration-200"
                >
                  Términos
                </Link>
              </div>
            )}
          </div>
        </div>

        {/* Copyright text override */}
        {copyrightText && (
          <div className="text-center text-sm text-[var(--text-tertiary)] mt-4 pt-4 border-t border-[var(--border-primary)]">
            {copyrightText}
          </div>
        )}
      </div>
    </footer>
  );
});

Footer.displayName = 'Footer';