"use client"

import { useState, useEffect } from "react"
import Link from "next/link"
import { usePathname } from "next/navigation"
import { Home, List, User, Users } from "lucide-react"

export function BottomNav() {
  const pathname = usePathname()
  const [matchEnabled, setMatchEnabled] = useState(false)
  const [configLoaded, setConfigLoaded] = useState(false)

  useEffect(() => {
    const loadConfig = async () => {
      try {
        const res = await fetch('/api/db/config')
        const data = await res.json()
        if (data.features) {
          setMatchEnabled(data.features.matchEnabled === true)
        }
      } catch (e) {
        setMatchEnabled(false)
      } finally {
        setConfigLoaded(true)
      }
    }
    loadConfig()
  }, [])

  if (
    pathname?.startsWith("/view/documentation") ||
    pathname?.startsWith("/admin") ||
    pathname?.startsWith("/view/read") ||
    pathname?.startsWith("/view/about")
  ) {
    return null
  }

  const navItems = [
    { href: "/view", icon: Home, label: "首页" },
    { href: "/view/chapters", icon: List, label: "目录" },
    ...(matchEnabled ? [{ href: "/view/match", icon: Users, label: "找伙伴", isCenter: true }] : []),
    { href: "/view/my", icon: User, label: "我的" },
  ]

  return (
    <>
      <nav className="fixed bottom-0 left-0 right-0 z-40 bg-[#1c1c1e]/95 backdrop-blur-xl border-t border-white/5 safe-bottom">
        <div className="flex items-center justify-around py-2 max-w-lg mx-auto">
          {navItems.map((item, index) => {
            const isActive = pathname === item.href
            const Icon = item.icon

            if (item.isCenter) {
              return (
                <Link key={index} href={item.href} className="flex flex-col items-center py-2 px-6 -mt-4">
                  <div
                    className={`w-14 h-14 rounded-full flex items-center justify-center shadow-lg transition-all bg-gradient-to-br from-[#00CED1] to-[#20B2AA] shadow-[#00CED1]/30`}
                  >
                    <Icon className="w-7 h-7 text-white" />
                  </div>
                  <span className={`text-xs mt-1 ${isActive ? "text-[#00CED1] font-medium" : "text-gray-500"}`}>
                    {item.label}
                  </span>
                </Link>
              )
            }

            return (
              <Link
                key={index}
                href={item.href}
                className="flex flex-col items-center py-2 px-4 touch-feedback transition-all duration-200"
              >
                <div
                  className={`w-6 h-6 flex items-center justify-center mb-1 transition-colors ${
                    isActive ? "text-[#00CED1]" : "text-gray-500"
                  }`}
                >
                  <Icon className="w-5 h-5" strokeWidth={isActive ? 2 : 1.5} />
                </div>
                <span
                  className={`text-xs transition-colors ${isActive ? "text-[#00CED1] font-medium" : "text-gray-500"}`}
                >
                  {item.label}
                </span>
              </Link>
            )
          })}
        </div>
      </nav>
    </>
  )
}
