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 SettingsModal from '../components/SettingsModal'
import UserManagement from '../components/UserManagement'

export default function Dashboard({ 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 [showSettings, setShowSettings] = useState(false)
  const [showUserMgmt, setShowUserMgmt] = useState(false)

  const fetchServers = async () => {
    try {
      const data = await getServers(token)
      setServers(data)
      setError('')
    } catch (err) {
      setError('Failed to connect to server')
      if (err.message.includes('401') || err.message.includes('403')) {
        onLogout()
      }
    } finally {
      setLoading(false)
    }
  }

  useEffect(() => {
    if (!userLoading) {
      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">Loading...</div>
      </div>
    )
  }

  const onlineCount = servers.filter(s => s.running).length
  const totalPlayers = servers.reduce((sum, s) => sum + (s.players?.online || 0), 0)

  return (
    <div className="min-h-screen">
      {/* 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-6">
              <h1 className="text-xl font-semibold text-white">
                Gameserver Monitor
              </h1>
              <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> players
                </span>
              </div>
            </div>

            <div className="flex items-center gap-3">
              <div className="hidden sm:block 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"
                >
                  Users
                </button>
              )}

              <button
                onClick={() => setShowSettings(true)}
                className="btn btn-ghost"
              >
                Settings
              </button>

              <button
                onClick={onLogout}
                className="btn btn-outline"
              >
                Sign out
              </button>
            </div>
          </div>
        </div>
      </header>

      {/* Main Content */}
      <main className="container-main py-8">
        {error && (
          <div className="mb-6 bg-red-500/10 border border-red-500/20 text-red-400 px-4 py-3 rounded-md text-sm">
            {error}
          </div>
        )}

        {loading ? (
          <div className="text-center py-12">
            <div className="text-neutral-400">Loading servers...</div>
          </div>
        ) : (
          <div className="grid grid-cols-1 md:grid-cols-2 gap-6">
            {servers.map((server) => (
              <ServerCard
                key={server.id}
                server={server}
                onClick={() => navigate('/server/' + server.id)}
              />
            ))}
          </div>
        )}
      </main>

      {/* Modals */}
      {showSettings && (
        <SettingsModal onClose={() => setShowSettings(false)} />
      )}

      {showUserMgmt && (
        <UserManagement onClose={() => setShowUserMgmt(false)} />
      )}
    </div>
  )
}
