import { Routes, Route, Navigate } from 'react-router-dom'
import { Layout } from './components/Layout'
import { LoginPage } from './pages/LoginPage'
import { DashboardPage } from './pages/DashboardPage'
import { SitePage } from './pages/SitePage'
import { FilesPage } from './pages/FilesPage'
import { FtpPage } from './pages/FtpPage'
import { DatabasePage } from './pages/DatabasePage'
import { TerminalPage } from './pages/TerminalPage'
import { MonitorPage } from './pages/MonitorPage'
import { CrontabPage } from './pages/CrontabPage'
import { ConfigPage } from './pages/ConfigPage'
import { LogsPage } from './pages/LogsPage'
import { FirewallPage } from './pages/FirewallPage'
import { DomainsPage } from './pages/DomainsPage'
import { DockerPage } from './pages/DockerPage'
import { NodePage } from './pages/NodePage'
import { SoftPage } from './pages/SoftPage'
import { ServicesPage } from './pages/ServicesPage'
import { PluginsPage } from './pages/PluginsPage'
import { BackupPlansPage } from './pages/BackupPlansPage'
import { UsersPage } from './pages/UsersPage'
import { RemoteInstallPage } from './pages/RemoteInstallPage'

function ProtectedRoute({ children }: { children: React.ReactNode }) {
  const token = localStorage.getItem('token')
  if (!token) return <Navigate to="/login" replace />
  return <>{children}</>
}

export default function App() {
  return (
    <Routes>
      <Route path="/login" element={<LoginPage />} />
      <Route path="/install" element={<RemoteInstallPage />} />
      <Route
        path="/"
        element={
          <ProtectedRoute>
            <Layout />
          </ProtectedRoute>
        }
      >
        <Route index element={<DashboardPage />} />
        <Route path="site" element={<SitePage />} />
        <Route path="ftp" element={<FtpPage />} />
        <Route path="database" element={<DatabasePage />} />
        <Route path="docker" element={<DockerPage />} />
        <Route path="control" element={<MonitorPage />} />
        <Route path="firewall" element={<FirewallPage />} />
        <Route path="files" element={<FilesPage />} />
        <Route path="node" element={<NodePage />} />
        <Route path="logs" element={<LogsPage />} />
        <Route path="ssl_domain" element={<DomainsPage />} />
        <Route path="xterm" element={<TerminalPage />} />
        <Route path="crontab" element={<CrontabPage />} />
        <Route path="soft" element={<SoftPage />} />
        <Route path="config" element={<ConfigPage />} />
        <Route path="services" element={<ServicesPage />} />
        <Route path="plugins" element={<PluginsPage />} />
        <Route path="backup-plans" element={<BackupPlansPage />} />
        <Route path="users" element={<UsersPage />} />
      </Route>
      <Route path="/logout" element={<LogoutRedirect />} />
      <Route path="*" element={<Navigate to="/" replace />} />
    </Routes>
  )
}

function LogoutRedirect() {
  localStorage.removeItem('token')
  window.location.href = '/login'
  return null
}
