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 MyPage from './pages/MyPage'
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 RagSettings from './pages/RagSettings'
import AiPlatformSettings from './pages/AiPlatformSettings'
import UserManagement from './pages/UserManagement'
import AuditLog from './pages/AuditLog'
import SystemSettings from './pages/SystemSettings'
import MobileApp from './pages/MobileApp'
// 업무 (UIWS 이식) — 업무일지/일정/쪽지/업무통계
import UiwsWorklog from './pages/uiws/WorklogList'
import UiwsSchedule from './pages/uiws/ScheduleCalendar'
import UiwsMessage from './pages/uiws/MessageBox'
import UiwsStats from './pages/uiws/StatsPivot'
// 시스템관리(권한) (UIWS 이식) — 역할/역할-메뉴권한/공통코드/메뉴/부서/거래처
import SysRoles from './pages/uiws/system/RoleManagement'
import SysRoleMenu from './pages/uiws/system/RoleMenuPermission'
import SysCodes from './pages/uiws/system/CodeManagement'
import SysMenus from './pages/uiws/system/MenuManagement'
import SysDepts from './pages/uiws/system/DeptManagement'
import SysCompanies from './pages/uiws/system/CompanyManagement'

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 />} />
            <Route path="/mypage" element={<MyPage />} />
            {/* 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="/ai-techniques" element={
              <ProtectedRoute min="MANAGER"><RagSettings /></ProtectedRoute>
            } />
            <Route path="/admin/ai-platform" element={
              <ProtectedRoute min="SUPERADMIN"><AiPlatformSettings /></ProtectedRoute>
            } />
            {/* 업무 (UIWS 이식) — 기존 라우트 보존, 추가만 */}
            <Route path="/uiws/worklog" element={<UiwsWorklog />} />
            <Route path="/uiws/schedule" element={<UiwsSchedule />} />
            <Route path="/uiws/message" element={<UiwsMessage />} />
            <Route path="/uiws/stats" element={<UiwsStats />} />
            {/* 시스템관리(권한) (UIWS 이식) — SUPERADMIN 전용, 기존 라우트 보존·추가만 */}
            <Route path="/uiws/system/roles" element={
              <ProtectedRoute min="SUPERADMIN"><SysRoles /></ProtectedRoute>
            } />
            <Route path="/uiws/system/role-menus" element={
              <ProtectedRoute min="SUPERADMIN"><SysRoleMenu /></ProtectedRoute>
            } />
            <Route path="/uiws/system/codes" element={
              <ProtectedRoute min="SUPERADMIN"><SysCodes /></ProtectedRoute>
            } />
            <Route path="/uiws/system/menus" element={
              <ProtectedRoute min="SUPERADMIN"><SysMenus /></ProtectedRoute>
            } />
            <Route path="/uiws/system/depts" element={
              <ProtectedRoute min="SUPERADMIN"><SysDepts /></ProtectedRoute>
            } />
            <Route path="/uiws/system/companies" element={
              <ProtectedRoute min="SUPERADMIN"><SysCompanies /></ProtectedRoute>
            } />
            {/* 관리자 */}
            <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 path="/admin/mobile-app" element={<MobileApp />} />
          </Route>
          <Route path="*" element={<Navigate to="/dashboard" replace />} />
        </Routes>
      </BrowserRouter>
    </QueryClientProvider>
  )
}
