'use client'

import { usePathname } from 'next/navigation'
import { navigation, metaModules, getModuleByHref } from '@/lib/navigation'

interface HeaderProps {
  title?: string
  description?: string
}

export function Header({ title, description }: HeaderProps) {
  const pathname = usePathname()

  // Auto-detect title and description from navigation
  let pageTitle = title
  let pageDescription = description

  if (!pageTitle) {
    // Check meta modules first
    const metaModule = metaModules.find(m => pathname === m.href || pathname.startsWith(m.href + '/'))
    if (metaModule) {
      pageTitle = metaModule.name
      pageDescription = metaModule.description
    } else {
      // Check navigation modules
      const result = getModuleByHref(pathname)
      if (result) {
        pageTitle = result.module.name
        pageDescription = result.module.description
      } else {
        // Check category pages
        const category = navigation.find(cat => pathname === `/${cat.id}`)
        if (category) {
          pageTitle = category.name
          pageDescription = category.description
        }
      }
    }
  }

  return (
    <header className="h-16 bg-white border-b border-slate-200 flex items-center px-6 sticky top-0 z-10">
      <div className="flex-1">
        {pageTitle && <h1 className="text-xl font-semibold text-slate-900">{pageTitle}</h1>}
        {pageDescription && <p className="text-sm text-slate-500">{pageDescription}</p>}
      </div>

      {/* Search */}
      <div className="flex items-center gap-4">
        <div className="relative">
          <input
            type="text"
            placeholder="Suchen... (Ctrl+K)"
            className="w-64 pl-10 pr-4 py-2 bg-slate-100 border border-transparent rounded-lg text-sm focus:bg-white focus:border-primary-300 focus:outline-none transition-colors"
          />
          <svg
            className="w-4 h-4 absolute left-3 top-1/2 -translate-y-1/2 text-slate-400"
            fill="none"
            stroke="currentColor"
            viewBox="0 0 24 24"
          >
            <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z" />
          </svg>
        </div>

        {/* User Area */}
        <div className="flex items-center gap-3">
          <span className="text-sm text-slate-500">Admin v2</span>
          <div className="w-8 h-8 rounded-full bg-primary-600 flex items-center justify-center text-white text-sm font-medium">
            A
          </div>
        </div>
      </div>
    </header>
  )
}
