import { BrowserRouter, Routes, Route, Navigate } from 'react-router-dom'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import Layout from './components/Layout'
import Login from './pages/Login'
import Dashboard from './pages/Dashboard'
import StoreList from './pages/StoreList'
import TemplateList from './pages/TemplateList'
import PosCvtList from './pages/PosCvtList'
import AlarmList from './pages/AlarmList'
import HCoreStatus from './pages/HCoreStatus'
import WorkHistory from './pages/WorkHistory'
import FirmwareList from './pages/FirmwareList'
import UserList from './pages/UserList'
import ProductList from './pages/ProductList'
import TenantAdmin from './pages/TenantAdmin'
import AiAnalysis from './pages/AiAnalysis'
import AiTechniqueSettings from './pages/AiTechniqueSettings'
import TagBindingList from './pages/TagBindingList'
import UpdateQueueList from './pages/UpdateQueueList'
import AuditLog from './pages/AuditLog'
import SystemSettings from './pages/SystemSettings'
import MobileApp from './pages/MobileApp'
import WorklogList from './pages/uiws/WorklogList'
import ScheduleCalendar from './pages/uiws/ScheduleCalendar'
import MessageBox from './pages/uiws/MessageBox'
import StatsPivot from './pages/uiws/StatsPivot'
import RoleManagement from './pages/uiws/system/RoleManagement'
import RoleMenuPermission from './pages/uiws/system/RoleMenuPermission'
import CodeManagement from './pages/uiws/system/CodeManagement'
import MenuManagement from './pages/uiws/system/MenuManagement'
import DeptManagement from './pages/uiws/system/DeptManagement'
import CompanyManagement 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="/stores"       element={<StoreList />} />
            <Route path="/templates"    element={<TemplateList />} />
            <Route path="/pos-cvt"      element={<PosCvtList />} />
            <Route path="/tag-binding"  element={<TagBindingList />} />
            <Route path="/update-queue" element={<UpdateQueueList />} />
            <Route path="/alarms"       element={<AlarmList />} />
            <Route path="/hcore"        element={<HCoreStatus />} />
            <Route path="/works"        element={<WorkHistory />} />
            <Route path="/firmware"     element={<FirmwareList />} />
            <Route path="/products"     element={<ProductList />} />
            <Route path="/users"        element={<UserList />} />
            <Route path="/tenants"      element={<TenantAdmin />} />
            <Route path="/audit"        element={<AuditLog />} />
            <Route path="/settings"     element={<SystemSettings />} />
            <Route path="/ai"           element={<AiAnalysis />} />
            <Route path="/ai-techniques" element={<AiTechniqueSettings />} />
            <Route path="/worklogs"     element={<WorklogList />} />
            <Route path="/schedules"    element={<ScheduleCalendar />} />
            <Route path="/messages"     element={<MessageBox />} />
            <Route path="/work-stats"   element={<StatsPivot />} />
            {/* 시스템관리(권한) — UIWS system 이식. 백엔드 RBAC 403 + 사이드바 가드 이중 방어. */}
            <Route path="/system/roles"      element={<RoleManagement />} />
            <Route path="/system/role-menus" element={<RoleMenuPermission />} />
            <Route path="/system/codes"      element={<CodeManagement />} />
            <Route path="/system/menus"      element={<MenuManagement />} />
            <Route path="/system/depts"      element={<DeptManagement />} />
            <Route path="/system/companies"  element={<CompanyManagement />} />
            <Route path="/mobile-app"   element={<MobileApp />} />
          </Route>
          <Route path="*" element={<Navigate to="/dashboard" replace />} />
        </Routes>
      </BrowserRouter>
    </QueryClientProvider>
  )
}
