import { BrowserRouter, Routes, Route, Navigate } from 'react-router-dom'
import { useAuthStore } from './store/auth'
import { WebSocketProvider } from './contexts/WebSocketContext'
import Layout from './components/layout/Layout'
import LoginPage from './pages/Login'
import DashboardPage from './pages/Dashboard'
import OrdersPage from './pages/Orders'
import OrderDetailPage from './pages/OrderDetail'
import NewOrderPage from './pages/NewOrder'
import UploadPage from './pages/Upload'
import AdminPage from './pages/Admin'
import CadPreviewPage from './pages/CadPreview'
import MaterialsPage from './pages/Materials'
import WorkerActivityPage from './pages/WorkerActivity'
import ProductLibraryPage from './pages/ProductLibrary'
import ProductDetailPage from './pages/ProductDetail'
import NewProductOrderPage from './pages/NewProductOrder'
import NotificationsPage from './pages/Notifications'
import NotificationSettingsPage from './pages/NotificationSettings'
import PreferencesPage from './pages/Preferences'
import TenantsPage from './pages/Tenants'
import WorkflowEditorPage from './pages/WorkflowEditor'
import MediaBrowserPage from './pages/MediaBrowser'
import BillingPage from './pages/Billing'
import WorkerManagementPage from './pages/WorkerManagement'
import AssetLibraryPage from './pages/AssetLibrary'

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

function AdminRoute({ children }: { children: React.ReactNode }) {
  const { token, user } = useAuthStore()
  if (!token) return <Navigate to="/login" replace />
  if (user?.role !== 'admin' && user?.role !== 'project_manager') return <Navigate to="/" replace />
  return <>{children}</>
}

export default function App() {
  return (
    <BrowserRouter>
      <WebSocketProvider>
        <Routes>
        <Route path="/login" element={<LoginPage />} />
        <Route
          path="/"
          element={
            <ProtectedRoute>
              <Layout />
            </ProtectedRoute>
          }
        >
          <Route index element={<DashboardPage />} />
          <Route path="orders" element={<OrdersPage />} />
          <Route path="orders/new" element={<NewOrderPage />} />
          <Route path="orders/new/product" element={<NewProductOrderPage />} />
          <Route path="orders/:id" element={<OrderDetailPage />} />
          <Route path="upload" element={<UploadPage />} />
          <Route
            path="admin"
            element={
              <AdminRoute>
                <AdminPage />
              </AdminRoute>
            }
          />
          <Route
            path="tenants"
            element={
              <AdminRoute>
                <TenantsPage />
              </AdminRoute>
            }
          />
          <Route
            path="workflows"
            element={
              <AdminRoute>
                <WorkflowEditorPage />
              </AdminRoute>
            }
          />
          <Route path="materials" element={<MaterialsPage />} />
          <Route path="activity" element={<WorkerActivityPage />} />
          <Route path="products" element={<ProductLibraryPage />} />
          <Route path="products/:id" element={<ProductDetailPage />} />
          <Route path="notifications" element={<NotificationsPage />} />
          <Route path="notification-settings" element={<NotificationSettingsPage />} />
          <Route path="preferences" element={<PreferencesPage />} />
          <Route path="cad/:id" element={<CadPreviewPage />} />
          <Route
            path="media"
            element={
              <AdminRoute>
                <MediaBrowserPage />
              </AdminRoute>
            }
          />
          <Route
            path="billing"
            element={
              <AdminRoute>
                <BillingPage />
              </AdminRoute>
            }
          />
          <Route
            path="workers"
            element={
              <AdminRoute>
                <WorkerManagementPage />
              </AdminRoute>
            }
          />
          <Route
            path="asset-libraries"
            element={
              <AdminRoute>
                <AssetLibraryPage />
              </AdminRoute>
            }
          />
        </Route>
        </Routes>
      </WebSocketProvider>
    </BrowserRouter>
  )
}
