'use client'

import React from 'react'
import Link from 'next/link'
import { usePathname } from 'next/navigation'
import {
  Shield, Download, FileCode, Layers, Smartphone, Lock,
  ChevronDown, ChevronRight, Home, BookOpen,
  Code2
} from 'lucide-react'

interface NavItem {
  title: string
  href: string
  icon?: React.ReactNode
  children?: NavItem[]
}

const navigation: NavItem[] = [
  {
    title: 'Uebersicht',
    href: '/developers/sdk/consent',
    icon: <Home className="w-4 h-4" />,
  },
  {
    title: 'Installation',
    href: '/developers/sdk/consent/installation',
    icon: <Download className="w-4 h-4" />,
  },
  {
    title: 'API Referenz',
    href: '/developers/sdk/consent/api-reference',
    icon: <FileCode className="w-4 h-4" />,
  },
  {
    title: 'Frameworks',
    href: '/developers/sdk/consent/frameworks',
    icon: <Layers className="w-4 h-4" />,
    children: [
      { title: 'React', href: '/developers/sdk/consent/frameworks/react' },
      { title: 'Vue', href: '/developers/sdk/consent/frameworks/vue' },
      { title: 'Angular', href: '/developers/sdk/consent/frameworks/angular' },
    ],
  },
  {
    title: 'Mobile SDKs',
    href: '/developers/sdk/consent/mobile',
    icon: <Smartphone className="w-4 h-4" />,
    children: [
      { title: 'iOS (Swift)', href: '/developers/sdk/consent/mobile/ios' },
      { title: 'Android (Kotlin)', href: '/developers/sdk/consent/mobile/android' },
      { title: 'Flutter', href: '/developers/sdk/consent/mobile/flutter' },
    ],
  },
  {
    title: 'Sicherheit',
    href: '/developers/sdk/consent/security',
    icon: <Lock className="w-4 h-4" />,
  },
]

function NavLink({ item, depth = 0 }: { item: NavItem; depth?: number }) {
  const pathname = usePathname()
  const isActive = pathname === item.href
  const isParentActive = item.children?.some((child) => pathname === child.href)
  const [isOpen, setIsOpen] = React.useState(isActive || isParentActive)

  const hasChildren = item.children && item.children.length > 0

  return (
    <div>
      <div className="flex items-center">
        <Link
          href={item.href}
          className={`flex-1 flex items-center gap-2 px-3 py-2 text-sm rounded-lg transition-colors ${
            isActive
              ? 'bg-violet-100 text-violet-900 font-medium'
              : 'text-gray-600 hover:bg-gray-100 hover:text-gray-900'
          }`}
          style={{ paddingLeft: `${12 + depth * 12}px` }}
        >
          {item.icon && <span className="shrink-0">{item.icon}</span>}
          <span>{item.title}</span>
        </Link>
        {hasChildren && (
          <button
            onClick={() => setIsOpen(!isOpen)}
            className="p-2 hover:bg-gray-100 rounded-lg transition-colors"
          >
            {isOpen ? (
              <ChevronDown className="w-4 h-4 text-gray-400" />
            ) : (
              <ChevronRight className="w-4 h-4 text-gray-400" />
            )}
          </button>
        )}
      </div>
      {hasChildren && isOpen && (
        <div className="mt-1 space-y-1">
          {item.children?.map((child) => (
            <NavLink key={child.href} item={child} depth={depth + 1} />
          ))}
        </div>
      )}
    </div>
  )
}

export function SDKDocsSidebar() {
  return (
    <aside className="w-64 h-[calc(100vh-64px)] fixed top-16 left-0 border-r border-gray-200 bg-white overflow-y-auto">
      <div className="p-4">
        {/* Header */}
        <div className="mb-6">
          <Link
            href="/developers/sdk/consent"
            className="flex items-center gap-3 p-3 rounded-xl bg-gradient-to-r from-violet-50 to-purple-50 border border-violet-100"
          >
            <div className="w-10 h-10 rounded-lg bg-gradient-to-br from-violet-600 to-purple-600 flex items-center justify-center">
              <Shield className="w-5 h-5 text-white" />
            </div>
            <div>
              <div className="font-semibold text-gray-900">Consent SDK</div>
              <div className="text-xs text-gray-500">v1.0.0</div>
            </div>
          </Link>
        </div>

        {/* Navigation */}
        <nav className="space-y-1">
          {navigation.map((item) => (
            <NavLink key={item.href} item={item} />
          ))}
        </nav>

        {/* Resources */}
        <div className="mt-8 pt-6 border-t border-gray-200">
          <div className="text-xs font-medium text-gray-400 uppercase tracking-wider mb-3 px-3">
            Ressourcen
          </div>
          <div className="space-y-1">
            <a
              href="https://github.com/breakpilot/consent-sdk"
              target="_blank"
              rel="noopener noreferrer"
              className="flex items-center gap-2 px-3 py-2 text-sm text-gray-600 hover:bg-gray-100 hover:text-gray-900 rounded-lg transition-colors"
            >
              <Code2 className="w-4 h-4" />
              <span>GitHub</span>
            </a>
            <Link
              href="/developers"
              className="flex items-center gap-2 px-3 py-2 text-sm text-gray-600 hover:bg-gray-100 hover:text-gray-900 rounded-lg transition-colors"
            >
              <BookOpen className="w-4 h-4" />
              <span>Developer Portal</span>
            </Link>
          </div>
        </div>
      </div>
    </aside>
  )
}

export default SDKDocsSidebar
