import { useEffect, useState } from 'react'
import { useNavigate, Link } from 'react-router-dom'
import { LogOut, UserCircle } from 'lucide-react'
import { getAiStatus } from '../api/client'

export default function Header() {
  const nav = useNavigate()
  const user = localStorage.getItem('mes_user') || 'admin'
  const role = localStorage.getItem('mes_role') || ''
  const [ollama, setOllama] = useState<boolean | null>(null)

  useEffect(() => {
    getAiStatus().then(d => setOllama(!!(d?.ollamaAvailable ?? d?.available))).catch(() => setOllama(false))
  }, [])

  const logout = () => {
    localStorage.removeItem('mes_token')
    localStorage.removeItem('mes_role')
    localStorage.removeItem('mes_user')
    nav('/login')
  }

  return (
    <header className="h-16 bg-panel border-b border-edge flex items-center justify-between px-6">
      <div className="text-sm text-slate-400">AI 제조실행시스템 · WMS · MES · QMS · Ollama 온프레미스</div>
      <div className="flex items-center gap-4">
        <span className={`flex items-center gap-1.5 text-xs px-2 py-1 rounded-md border ${
          ollama ? 'text-accent border-accent/30 bg-accent/10' : 'text-slate-400 border-edge bg-card'
        }`}>
          <span className={`w-1.5 h-1.5 rounded-full ${ollama ? 'bg-accent' : 'bg-slate-500'}`} />
          AI {ollama === null ? '확인 중' : ollama ? '온라인' : '폴백'}
        </span>
        <Link to="/mypage" className="flex items-center gap-1.5 text-sm text-slate-300 hover:text-brand" title="마이페이지">
          <UserCircle size={18} /> {user}
          {role && <span className="text-[11px] text-slate-500">({role})</span>}
        </Link>
        <button onClick={logout} className="flex items-center gap-1.5 text-sm text-slate-400 hover:text-brand">
          <LogOut size={16} /> 로그아웃
        </button>
      </div>
    </header>
  )
}
