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'
import Contents from './pages/Contents'
import ContentEditor from './pages/ContentEditor'
import Media from './pages/Media'
import Products from './pages/Products'
import Banners from './pages/Banners'
import Menus from './pages/Menus'
import Themes from './pages/Themes'
import I18n from './pages/I18n'
import Seo from './pages/Seo'
import Forms from './pages/Forms'
import Ugc from './pages/Ugc'
import Channels from './pages/Channels'
import Members from './pages/Members'
import Analytics from './pages/Analytics'
import AiTools from './pages/AiTools'
import AiTechniques from './pages/AiTechniques'
import UserManagement from './pages/UserManagement'
import AuditLog from './pages/AuditLog'
import SystemSettings from './pages/SystemSettings'
import AiPlatformSettings from './pages/AiPlatformSettings'
import MobileApp from './pages/MobileApp'
// UIWS 이식 업무 모듈(업무일지·일정·쪽지·통계) — 기존 라우트 보존, 신규 메뉴만 추가
import UiwsLayout from './components/uiws/UiwsLayout'
import WorklogList from './pages/uiws/WorklogList'
import ScheduleCalendar from './pages/uiws/ScheduleCalendar'
import MessageBox from './pages/uiws/MessageBox'
import StatsPivot from './pages/uiws/StatsPivot'
// UIWS 시스템관리(권한) — RBAC/공통코드/메뉴/부서/거래처 (SUPERADMIN 전용)
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="/contents" element={<Contents />} />
            <Route path="/contents/new" element={<ContentEditor />} />
            <Route path="/contents/:id" element={<ContentEditor />} />
            <Route path="/media" element={<Media />} />
            <Route path="/products" element={<Products />} />
            <Route path="/banners" element={<Banners />} />
            <Route path="/menus" element={<Menus />} />
            <Route path="/themes" element={<Themes />} />
            <Route path="/i18n" element={<I18n />} />
            <Route path="/seo" element={<Seo />} />
            <Route path="/forms" element={<Forms />} />
            <Route path="/ugc" element={<Ugc />} />
            <Route path="/channels" element={<Channels />} />
            <Route path="/members" element={<Members />} />
            <Route path="/analytics" element={<Analytics />} />
            <Route path="/ai-tools" element={<AiTools />} />
            <Route path="/ai-techniques" element={<AiTechniques />} />
            <Route path="/admin/users" element={
              <ProtectedRoute min="SUPERADMIN"><UserManagement /></ProtectedRoute>
            } />
            <Route path="/admin/audit" element={
              <ProtectedRoute min="EDITOR"><AuditLog /></ProtectedRoute>
            } />
            <Route path="/admin/settings" element={
              <ProtectedRoute min="EDITOR"><SystemSettings /></ProtectedRoute>
            } />
            <Route path="/admin/ai-platform" element={
              <ProtectedRoute min="SUPERADMIN"><AiPlatformSettings /></ProtectedRoute>
            } />
            <Route path="/admin/mobile-app" element={<MobileApp />} />

            {/* 업무(UIWS 이식) — 인증 사용자 전체(Viewer+). UiwsLayout = .uiws-scope 다크/라이트 토큰 */}
            <Route path="/uiws/worklog" element={<UiwsLayout><WorklogList /></UiwsLayout>} />
            <Route path="/uiws/schedule" element={<UiwsLayout><ScheduleCalendar /></UiwsLayout>} />
            <Route path="/uiws/message" element={<UiwsLayout><MessageBox /></UiwsLayout>} />
            <Route path="/uiws/stats" element={<UiwsLayout><StatsPivot /></UiwsLayout>} />

            {/* 시스템관리(권한) — UIWS RBAC/공통코드/메뉴/부서/거래처. /api/system/** = SUPERADMIN 전용 */}
            <Route path="/uiws/system/roles" element={
              <ProtectedRoute min="SUPERADMIN"><UiwsLayout><RoleManagement /></UiwsLayout></ProtectedRoute>
            } />
            <Route path="/uiws/system/role-menus" element={
              <ProtectedRoute min="SUPERADMIN"><UiwsLayout><RoleMenuPermission /></UiwsLayout></ProtectedRoute>
            } />
            <Route path="/uiws/system/codes" element={
              <ProtectedRoute min="SUPERADMIN"><UiwsLayout><CodeManagement /></UiwsLayout></ProtectedRoute>
            } />
            <Route path="/uiws/system/menus" element={
              <ProtectedRoute min="SUPERADMIN"><UiwsLayout><MenuManagement /></UiwsLayout></ProtectedRoute>
            } />
            <Route path="/uiws/system/depts" element={
              <ProtectedRoute min="SUPERADMIN"><UiwsLayout><DeptManagement /></UiwsLayout></ProtectedRoute>
            } />
            <Route path="/uiws/system/companies" element={
              <ProtectedRoute min="SUPERADMIN"><UiwsLayout><CompanyManagement /></UiwsLayout></ProtectedRoute>
            } />
          </Route>
          <Route path="*" element={<Navigate to="/dashboard" replace />} />
        </Routes>
      </BrowserRouter>
    </QueryClientProvider>
  )
}
