import { BrowserRouter, Routes, Route, Navigate } from 'react-router-dom'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import Layout from './components/Layout'
import ProtectedRoute from './components/ProtectedRoute'
import Login from './pages/Login'
import Dashboard from './pages/Dashboard'
// MES
import Jobs from './pages/Jobs'
import Progress from './pages/Progress'
import WorkOrders from './pages/WorkOrders'
import Production from './pages/Production'
import ProcessBoard from './pages/ProcessBoard'
import Oee from './pages/Oee'
import LotTracking from './pages/LotTracking'
import ProductionPlan from './pages/ProductionPlan'
// WMS
import Receiving from './pages/Receiving'
import Shipping from './pages/Shipping'
import Inventory from './pages/Inventory'
import StockTake from './pages/StockTake'
import LotSerial from './pages/LotSerial'
// QMS
import Inspection from './pages/Inspection'
import Spec from './pages/Spec'
import Ncr from './pages/Ncr'
import Capa from './pages/Capa'
import Spc from './pages/Spc'
import Certificate from './pages/Certificate'
// 기준정보
import Items from './pages/Items'
import Bom from './pages/Bom'
import Routing from './pages/Routing'
import Partners from './pages/Partners'
import Warehouses from './pages/Warehouses'
// 공통
import Analytics from './pages/Analytics'
import AiTools from './pages/AiTools'
import UserManagement from './pages/UserManagement'
import AuditLog from './pages/AuditLog'
import SystemSettings from './pages/SystemSettings'

const qc = new QueryClient()

export default function App() {
  return (
    <QueryClientProvider client={qc}>
      <BrowserRouter>
        <Routes>
          <Route path="/login" element={<Login />} />
          <Route element={<Layout />}>
            <Route path="/dashboard" element={<Dashboard />} />
            {/* MES */}
            <Route path="/jobs" element={<Jobs />} />
            <Route path="/progress" element={<Progress />} />
            <Route path="/workorders" element={<WorkOrders />} />
            <Route path="/production" element={<Production />} />
            <Route path="/process-board" element={<ProcessBoard />} />
            <Route path="/oee" element={<Oee />} />
            <Route path="/tracking" element={<LotTracking />} />
            <Route path="/plans" element={<ProductionPlan />} />
            {/* WMS */}
            <Route path="/receiving" element={<Receiving />} />
            <Route path="/shipping" element={<Shipping />} />
            <Route path="/inventory" element={<Inventory />} />
            <Route path="/stocktake" element={<StockTake />} />
            <Route path="/lotserial" element={<LotSerial />} />
            {/* QMS */}
            <Route path="/inspection" element={<Inspection />} />
            <Route path="/spec" element={<Spec />} />
            <Route path="/ncr" element={<Ncr />} />
            <Route path="/capa" element={<Capa />} />
            <Route path="/spc" element={<Spc />} />
            <Route path="/certificate" element={<Certificate />} />
            {/* 기준정보 */}
            <Route path="/items" element={<Items />} />
            <Route path="/bom" element={<Bom />} />
            <Route path="/routing" element={<Routing />} />
            <Route path="/partners" element={
              <ProtectedRoute min="MANAGER"><Partners /></ProtectedRoute>
            } />
            <Route path="/warehouses" element={<Warehouses />} />
            {/* 공통 */}
            <Route path="/analytics" element={<Analytics />} />
            <Route path="/ai-tools" element={<AiTools />} />
            {/* 관리자 */}
            <Route path="/admin/users" element={
              <ProtectedRoute min="SUPERADMIN"><UserManagement /></ProtectedRoute>
            } />
            <Route path="/admin/audit" element={
              <ProtectedRoute min="MANAGER"><AuditLog /></ProtectedRoute>
            } />
            <Route path="/admin/settings" element={
              <ProtectedRoute min="MANAGER"><SystemSettings /></ProtectedRoute>
            } />
          </Route>
          <Route path="*" element={<Navigate to="/dashboard" replace />} />
        </Routes>
      </BrowserRouter>
    </QueryClientProvider>
  )
}
