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 LoginModal from '../components/LoginModal'
import ActivityLog from '../components/ActivityLog'

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

  const isAuthenticated = !!token

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

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

  const roleLabels = {
    user: 'Zuschauer',
    moderator: 'Moderator',
    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">
              {isAuthenticated ? (
                <>
                  {/* Desktop Navigation */}
                  <div className="hidden md:flex items-center gap-6">
                    {user?.avatar && user?.discordId && (
                      <img
                        src={`https://cdn.discordapp.com/avatars/${user.discordId}/${user.avatar}.png?size=64`}
                        alt="Avatar"
                        className="w-8 h-8 rounded-full"
                      />
                    )}
                    <div className="text-right mr-2">
                      <div className="text-sm text-white">{user?.username}</div>
                      <div className="text-xs text-neutral-500">{roleLabels[role]}</div>
                    </div>
                    {isSuperadmin && (
                      <>
                        <button
                          onClick={() => setShowUserMgmt(true)}
                          className="btn btn-ghost"
                        >
                          Benutzer
                        </button>
                        <button
                          onClick={() => setShowActivityLog(true)}
                          className="btn btn-ghost"
                        >
                          Logs
                        </button>
                      </>
                    )}
                    <button
                      onClick={onLogout}
                      className="btn btn-outline"
                    >
                      Abmelden
                    </button>
                  </div>

                  {/* Mobile Burger Button */}
                  <div className="md:hidden">
                    <button
                      onClick={() => setMobileMenuOpen(!mobileMenuOpen)}
                      className="btn btn-ghost p-2"
                      aria-label="Menu"
                    >
                    <svg className="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                      {mobileMenuOpen ? (
                        <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M6 18L18 6M6 6l12 12" />
                      ) : (
                        <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M4 6h16M4 12h16M4 18h16" />
                      )}
                    </svg>
                    </button>
                  </div>
                </>
              ) : (
                <button
                  onClick={() => setShowLogin(true)}
                  className="btn btn-primary"
                >
                  Anmelden
                </button>
              )}
            </div>
          </div>

          {/* Mobile Dropdown Menu */}
          {mobileMenuOpen && isAuthenticated && (
            <div className="md:hidden border-t border-neutral-800 py-4 mt-4 animate-slideDown">
              <div className="flex items-center justify-between mb-3 px-1">
                <div className="flex items-center gap-3">
                  {user?.avatar && user?.discordId && (
                    <img
                      src={`https://cdn.discordapp.com/avatars/${user.discordId}/${user.avatar}.png?size=64`}
                      alt="Avatar"
                      className="w-8 h-8 rounded-full"
                    />
                  )}
                  <div>
                    <div className="text-sm text-white">{user?.username}</div>
                    <div className="text-xs text-neutral-500">{roleLabels[role]}</div>
                  </div>
                </div>
                <div className="text-sm text-neutral-400 text-right">
                  <div><span className="text-white font-medium">{onlineCount}</span>/{servers.length} online</div>
                  <div><span className="text-white font-medium">{totalPlayers}</span> Spieler</div>
                </div>
              </div>
              <div className="flex flex-col gap-2">
                {isSuperadmin && (
                  <>
                    <button
                      onClick={() => { setShowUserMgmt(true); setMobileMenuOpen(false); }}
                      className="btn btn-ghost justify-start"
                    >
                      Benutzer
                    </button>
                    <button
                      onClick={() => { setShowActivityLog(true); setMobileMenuOpen(false); }}
                      className="btn btn-ghost justify-start"
                    >
                      Logs
                    </button>
                  </>
                )}
                <button
                  onClick={() => { onLogout(); setMobileMenuOpen(false); }}
                  className="btn btn-outline justify-start"
                >
                  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">Lade Server...</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={isAuthenticated}
                />
              </div>
            ))}
          </div>
        )}
      </main>

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