import { useState, useEffect } from 'react'
import { useNavigate } from 'react-router-dom'
import { getServers } from '../api'
import { useUser } from '../context/UserContext'
import ServerCard from '../components/ServerCard'
import UserManagement from '../components/UserManagement'
import ActivityLog from '../components/ActivityLog'

export default function Dashboard({ onLogout }) {
  const navigate = useNavigate()
  const { user, token, loading: userLoading, isSuperadmin, role, avatarUrl } = useUser()
  const [servers, setServers] = useState([])
  const [loading, setLoading] = useState(true)
  const [error, setError] = useState('')
  const [showUserMgmt, setShowUserMgmt] = useState(false)
  const [showActivityLog, setShowActivityLog] = useState(false)

  const fetchServers = async () => {
    try {
      const data = await getServers(token)
      setServers(data)
      setError('')
    } catch (err) {
      if (err.message.includes('401') || err.message.includes('403')) {
        onLogout()
      } else {
        setError('Verbindung zum Server fehlgeschlagen')
      }
    } finally {
      setLoading(false)
    }
  }

  useEffect(() => {
    if (!userLoading && token) {
      fetchServers()
      const interval = setInterval(fetchServers, 10000)
      return () => clearInterval(interval)
    }
  }, [token, userLoading])

  const roleLabels = {
    user: 'Viewer',
    moderator: 'Operator',
    superadmin: 'Admin'
  }

  if (userLoading) {
    return (
      <div className="min-h-screen flex items-center justify-center">
        <div className="text-neutral-400">Laden...</div>
      </div>
    )
  }

  const onlineCount = servers.filter(s => s.running).length
  document.title = 'Dashboard | Zeasy GSM'
  const totalPlayers = servers.reduce((sum, s) => sum + (s.players?.online || 0), 0)

  return (
    <div className="min-h-screen page-enter">
      {/* Header */}
      <header className="border-b border-neutral-800 bg-neutral-900/50 backdrop-blur-sm sticky top-0 z-10">
        <div className="container-main py-4">
          <div className="flex items-center justify-between">
            <div className="flex items-center gap-3">
              <a href="https://zeasy.software" target="_blank" rel="noopener noreferrer" className="relative block group">
                <img src="/navbarlogoweiß.png" alt="Logo" className="h-8 transition-opacity duration-300 group-hover:opacity-0" />
                <img src="/navbarlogograuer.png" alt="Logo" className="h-8 absolute top-0 left-0 opacity-0 transition-opacity duration-300 group-hover:opacity-100" />
              </a>
              <span className="text-xl font-semibold text-white hidden sm:inline">Gameserver Management</span>
            </div>
            <div className="hidden md:flex items-center gap-4 text-sm text-neutral-400">
              <span>
                <span className="text-white font-medium">{onlineCount}</span>/{servers.length} online
              </span>
              <span className="text-neutral-600">|</span>
              <span>
                <span className="text-white font-medium">{totalPlayers}</span> Spieler
              </span>
            </div>
            <div className="flex items-center gap-3">
              {/* User info with Discord avatar */}
              <div className="flex items-center gap-3">
                {avatarUrl && (
                  <img
                    src={avatarUrl}
                    alt="Avatar"
                    className="w-8 h-8 rounded-full"
                  />
                )}
                <div className="hidden sm:block text-right">
                  <div className="text-sm text-white">{user?.username}</div>
                  <div className="text-xs text-neutral-500">{roleLabels[role]}</div>
                </div>
              </div>

              {isSuperadmin && (
                <>
                  <button
                    onClick={() => setShowActivityLog(true)}
                    className="btn btn-ghost"
                  >
                    Log
                  </button>
                  <button
                    onClick={() => setShowUserMgmt(true)}
                    className="btn btn-ghost"
                  >
                    Benutzer
                  </button>
                </>
              )}
              <button
                onClick={onLogout}
                className="btn btn-outline"
              >
                Abmelden
              </button>
            </div>
          </div>
        </div>
      </header>

      {/* Main Content */}
      <main className="container-main py-8">
        {error && (
          <div className="mb-6 alert alert-error fade-in">
            {error}
          </div>
        )}
        {loading ? (
          <div className="text-center py-12">
            <div className="text-neutral-400">Server werden geladen...</div>
          </div>
        ) : (
          <div className="grid grid-cols-1 md:grid-cols-2 gap-6">
            {servers.map((server, index) => (
              <div
                key={server.id}
                className="fade-in-up"
                style={{ animationDelay: index * 50 + 'ms', animationFillMode: 'both' }}
              >
                <ServerCard
                  server={server}
                  onClick={() => navigate('/server/' + server.id)}
                  isAuthenticated={true}
                />
              </div>
            ))}
          </div>
        )}
      </main>

      {/* Modals */}
      {showUserMgmt && (
        <UserManagement onClose={() => setShowUserMgmt(false)} />
      )}
      {showActivityLog && (
        <ActivityLog onClose={() => setShowActivityLog(false)} />
      )}
    </div>
  )
}
