'use client';

/**
 * Subsection Navigation Component
 *
 * Reusable navigation bar for documentation and other subsections
 * Features:
 * - Dark nav bar with decorative wave line
 * - Active state indicator (purple color)
 * - Responsive (hamburger menu on mobile)
 * - Three-column layout matching docs structure
 * - Customizable left/right slots
 *
 * Uses ThreeColumnLayout for consistent alignment with docs pages.
 * Columns align with docs layout at all screen widths.
 *
 * Usage:
 * <SubsectionNav
 *   items={[...]}
 *   currentPath="/docs/final"
 *   leftSlot={<Logo />}
 *   rightSlot={<UserMenu />}
 * />
 */

import { useState } from 'react';
import { ThreeColumnLayout } from '@/components/layout/ThreeColumnLayout';

interface NavItem {
  label: string;
  href: string;
  disabled?: boolean;
}

interface SubsectionNavProps {
  items: NavItem[];
  currentPath: string;
  ctaText?: string;
  ctaHref?: string;
  onCtaClick?: () => void;
  /** Optional content for left column (w-64, hidden until lg) */
  leftSlot?: React.ReactNode;
  /** Optional content for right column (w-56, hidden until xl) */
  rightSlot?: React.ReactNode;
}

export const SubsectionNav = ({ items, currentPath, leftSlot, rightSlot }: SubsectionNavProps) => {
  const [mobileMenuOpen, setMobileMenuOpen] = useState(false);

  const isActive = (href: string) => currentPath.startsWith(href);

  return (
    <nav className="sticky top-0 z-40 bg-slate-950/80 backdrop-blur-sm border-b border-white/10 relative">
      {/* Three-Column Layout */}
      <ThreeColumnLayout
        left={leftSlot}
        center={
          <div className="max-w-7xl mx-auto px-6">
            <div className="flex items-center justify-between h-12">
              {/* Desktop Navigation Items */}
              <div className="hidden md:flex items-center gap-8">
                {items.map((item) => {
                  const active = isActive(item.href);
                  return (
                    <div key={item.label} className="relative group">
                      <a
                        href={item.disabled ? undefined : item.href}
                        aria-disabled={item.disabled}
                        className={`
                          py-3 text-sm font-medium transition-colors
                          ${
                            item.disabled
                              ? 'text-gray-600 cursor-not-allowed'
                              : active
                                ? 'text-purple-400'
                                : 'text-gray-400 hover:text-white'
                          }
                        `}
                      >
                        {item.label}
                      </a>
                      {item.disabled && (
                        <div className="absolute left-1/2 -translate-x-1/2 top-full mt-1 opacity-0 group-hover:opacity-100 transition-opacity px-2 py-1 bg-slate-800 text-gray-300 text-xs rounded whitespace-nowrap pointer-events-none z-50">
                          Coming soon
                        </div>
                      )}
                    </div>
                  );
                })}
              </div>

              {/* Mobile Menu Button */}
              <div className="md:hidden flex items-center ml-auto">
                <button
                  onClick={() => setMobileMenuOpen(!mobileMenuOpen)}
                  className="p-2 text-gray-400 hover:text-white transition-colors"
                  aria-label="Toggle menu"
                >
                  <svg className="w-6 h-6" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                    {mobileMenuOpen ? (
                      <path
                        strokeLinecap="round"
                        strokeLinejoin="round"
                        strokeWidth={2}
                        d="M6 18L18 6M6 6l12 12"
                      />
                    ) : (
                      <path
                        strokeLinecap="round"
                        strokeLinejoin="round"
                        strokeWidth={2}
                        d="M4 6h16M4 12h16M4 18h16"
                      />
                    )}
                  </svg>
                </button>
              </div>
            </div>
          </div>
        }
      />

      {/* Right Slot - Absolutely Positioned */}
      {rightSlot && (
        <div className="absolute top-0 right-0 h-12 flex items-center pr-0 hidden xl:flex overflow-visible">
          {rightSlot}
        </div>
      )}

      {/* Decorative Wave Line */}
      <div className="absolute bottom-0 left-0 right-0 h-px overflow-hidden">
        <svg
          className="absolute inset-0 w-full h-full"
          viewBox="0 0 1200 2"
          preserveAspectRatio="none"
          xmlns="http://www.w3.org/2000/svg"
        >
          <defs>
            <linearGradient id="wave-gradient" x1="0%" y1="0%" x2="100%" y2="0%">
              <stop offset="0%" stopColor="rgb(147, 51, 234)" stopOpacity="0.3" />
              <stop offset="50%" stopColor="rgb(8, 145, 178)" stopOpacity="0.5" />
              <stop offset="100%" stopColor="rgb(147, 51, 234)" stopOpacity="0.3" />
            </linearGradient>
          </defs>
          <path
            d="M0,1 Q300,0 600,1 T1200,1"
            stroke="url(#wave-gradient)"
            strokeWidth="1"
            fill="none"
            className="animate-pulse"
          />
        </svg>
      </div>

      {/* Mobile Menu Overlay */}
      {mobileMenuOpen && (
        <div className="md:hidden border-t border-white/10 bg-slate-900/95 backdrop-blur-sm">
          <div className="px-6 py-4">
            {/* ApiKeyWidget - Mobile */}
            {rightSlot && (
              <>
                <div className="flex justify-end mb-3">
                  {rightSlot}
                </div>
                {/* Visual separator */}
                <div className="border-t border-white/10 mb-3" />
              </>
            )}

            {/* Navigation items */}
            <div className="space-y-2">
              {items.map((item) => {
                const active = isActive(item.href);
                return (
                  <a
                    key={item.label}
                    href={item.disabled ? undefined : item.href}
                    aria-disabled={item.disabled}
                    onClick={item.disabled ? undefined : () => setMobileMenuOpen(false)}
                    className={`
                      block px-4 py-3 rounded-lg text-sm font-medium transition-colors
                      ${
                        item.disabled
                          ? 'text-gray-600 cursor-not-allowed'
                          : active
                            ? 'bg-purple-500/10 text-purple-400'
                            : 'text-gray-400 hover:text-white hover:bg-white/5'
                      }
                    `}
                  >
                    {item.label}
                    {item.disabled && (
                      <span className="ml-2 text-xs text-gray-500">(Coming soon)</span>
                    )}
                  </a>
                );
              })}
            </div>
          </div>
        </div>
      )}
    </nav>
  );
};
