import { Suspense, lazy } from 'react'
import { Spin } from 'antd'
import { HashRouter, Navigate, Outlet, Route, Routes, useLocation } from 'react-router'

import { getAdminRole, hasAdminSession } from '@/utils/admin-auth'
import AdminLayout from '@/layouts/AdminLayout'

const AdminDashboardPage = lazy(() => import('@/pages/admin/AdminDashboardPage'))
const AdminLoginPage = lazy(() => import('@/pages/admin/AdminLoginPage'))
const AdminOrderDetailPage = lazy(() => import('@/pages/admin/AdminOrderDetailPage'))
const AdminOrdersPage = lazy(() => import('@/pages/admin/AdminOrdersPage'))
const AdminTaskDetailPage = lazy(() => import('@/pages/admin/AdminTaskDetailPage'))
const AdminTasksPage = lazy(() => import('@/pages/admin/AdminTasksPage'))
const NotFoundPage = lazy(() => import('@/pages/NotFoundPage'))

function RequireAdmin() {
  const location = useLocation()

  if (!hasAdminSession()) {
    return <Navigate to="/admin/login" replace state={{ from: location }} />
  }

  return <Outlet />
}

function RequireRole({ roles }: { roles: Array<'admin' | 'operator' | 'support'> }) {
  if (!roles.includes(getAdminRole())) {
    return <Navigate to="/admin/dashboard" replace />
  }

  return <Outlet />
}

function LoginRoute() {
  if (hasAdminSession()) {
    return <Navigate to="/admin/dashboard" replace />
  }

  return <AdminLoginPage />
}

export default function App() {
  return (
    <HashRouter>
      <Suspense fallback={<RouteLoading />}>
        <Routes>
          <Route path="/" element={<Navigate to="/admin/dashboard" replace />} />
          <Route path="/admin/login" element={<LoginRoute />} />
          <Route element={<RequireAdmin />}>
            <Route path="/admin" element={<AdminLayout />}>
              <Route index element={<Navigate to="/admin/dashboard" replace />} />
              <Route path="dashboard" element={<AdminDashboardPage />} />
              <Route path="orders" element={<AdminOrdersPage />} />
              <Route path="orders/:orderId" element={<AdminOrderDetailPage />} />
              <Route path="tasks" element={<AdminTasksPage />} />
              <Route path="tasks/:taskId" element={<AdminTaskDetailPage />} />
              <Route element={<RequireRole roles={['admin']} />}>
                <Route path="users" element={<NotFoundPage title="用户管理迁移中" />} />
                <Route path="platform-shops" element={<NotFoundPage title="平台配置迁移中" />} />
                <Route path="platform-fulfillment" element={<NotFoundPage title="履约配置迁移中" />} />
                <Route path="audit-logs" element={<NotFoundPage title="审计日志迁移中" />} />
              </Route>
            </Route>
          </Route>
          <Route path="/claim/:token" element={<NotFoundPage title="领取页迁移中" />} />
          <Route path="*" element={<NotFoundPage />} />
        </Routes>
      </Suspense>
    </HashRouter>
  )
}

function RouteLoading() {
  return (
    <div className="route-loading">
      <Spin />
    </div>
  )
}
