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 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 />} />
            <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="/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>
          <Route path="*" element={<Navigate to="/dashboard" replace />} />
        </Routes>
      </BrowserRouter>
    </QueryClientProvider>
  )
}
