import { Navigate, Outlet, Route, Routes } from 'react-router-dom'
import {
  App as AntApp,
  ConfigProvider,
  theme as antTheme,
} from 'antd'
import koKR from 'antd/locale/ko_KR'
import AgentShell from './components/AgentShell'
import { useTheme } from './themeContext'
import { THEME_PRIMARY } from './theme'
import { formRequiredMark } from './formRequiredMark'
import './styles/app.css'

import LoginPage from './pages/LoginPage'
import HomeDashboardPage from './pages/care/HomeDashboardPage'
import ProductInternetPage from './pages/care/ProductInternetPage'
import ProductHomePage from './pages/care/ProductHomePage'
import ProductLogPage from './pages/care/ProductLogPage'
import ContractInternetPage from './pages/care/ContractInternetPage'
import ContractHomePage from './pages/care/ContractHomePage'
import ContractLogPage from './pages/care/ContractLogPage'
import ContractAdjustPage from './pages/care/ContractAdjustPage'
import ContractBalancePage from './pages/care/ContractBalancePage'
import ContractWritePage from './pages/care/ContractWritePage'
import ReceptionListPage from './pages/care/ReceptionListPage'
import InvoicePublishPage from './pages/care/InvoicePublishPage'
import InvoiceMyPage from './pages/care/InvoiceMyPage'
import InvoiceLogPage from './pages/care/InvoiceLogPage'
import PendingCalendarPage from './pages/care/PendingCalendarPage'
import PendingPage from './pages/care/PendingPage'
import PendingLogPage from './pages/care/PendingLogPage'
import BookingPage from './pages/care/BookingPage'
import BookingLogPage from './pages/care/BookingLogPage'
import CustomerPage from './pages/care/CustomerPage'
import CustomerLogPage from './pages/care/CustomerLogPage'
import AbooksPage from './pages/care/AbooksPage'
import AbooksLogPage from './pages/care/AbooksLogPage'
import ReportDailyPage from './pages/care/ReportDailyPage'
import ReportMonthlyPage from './pages/care/ReportMonthlyPage'
import ReportInternetPage from './pages/care/ReportInternetPage'
import ReportHomePage from './pages/care/ReportHomePage'
import ReportMemberPage from './pages/care/ReportMemberPage'
import ReportAgencyPage from './pages/care/ReportAgencyPage'
import ReportIncentivePage from './pages/care/ReportIncentivePage'
import SettingAgencyPage from './pages/care/SettingAgencyPage'
import SettingMemberPage from './pages/care/SettingMemberPage'
import SettingPreferencePage from './pages/care/SettingPreferencePage'
import SettingLevelPage from './pages/care/SettingLevelPage'
import SettingPartner1Page from './pages/care/SettingPartner1Page'
import SettingPartner2Page from './pages/care/SettingPartner2Page'
import CsNoticePage from './pages/care/CsNoticePage'
import CsQuestionPage from './pages/care/CsQuestionPage'
import BbsPage from './pages/care/BbsPage'
import SitemapPage from './pages/care/SitemapPage'

function ProtectedRoute() {
  const token = localStorage.getItem('token')
  if (!token) return <Navigate to="/login" replace />
  return <Outlet />
}

export default function App() {
  const { theme } = useTheme()
  return (
    <ConfigProvider
      locale={koKR}
      form={{ requiredMark: formRequiredMark }}
      theme={{
        algorithm: theme === 'light' ? antTheme.defaultAlgorithm : antTheme.darkAlgorithm,
        token: {
          colorPrimary: THEME_PRIMARY[theme] || '#1677ff',
          colorBgContainer: theme === 'light' ? '#ffffff' : theme === 'blue' ? '#15273d' : '#24283b',
          colorTextLightSolid: '#ffffff',
          colorBorder: theme === 'light' ? undefined : theme === 'blue' ? 'rgba(143, 187, 224, 0.55)' : 'rgba(192, 202, 245, 0.5)',
          colorBorderSecondary: theme === 'light' ? undefined : theme === 'blue' ? 'rgba(143, 187, 224, 0.35)' : 'rgba(192, 202, 245, 0.32)',
        },
        components: {
          Button: {
            primaryShadow: 'none',
            defaultShadow: 'none',
            dangerShadow: 'none',
          },
          Checkbox: {
            colorBorder: theme === 'light' ? undefined : theme === 'blue' ? 'rgba(143, 187, 224, 0.65)' : 'rgba(192, 202, 245, 0.58)',
          },
          Select: {
            selectorBg: theme === 'light' ? '#ffffff' : theme === 'blue' ? '#15273d' : '#24283b',
            optionSelectedBg:
              theme === 'light' ? '#dbeafe' : theme === 'blue' ? 'rgba(30, 138, 212, 0.28)' : 'rgba(122, 162, 247, 0.22)',
            optionActiveBg:
              theme === 'light' ? '#f1f5f9' : theme === 'blue' ? 'rgba(30, 138, 212, 0.16)' : 'rgba(122, 162, 247, 0.12)',
          },
        },
      }}
    >
      <AntApp>
        <Routes>
          <Route path="/login" element={<LoginPage />} />
          <Route element={<ProtectedRoute />}>
            <Route path="/care" element={<AgentShell />}>
              <Route path="main" element={<HomeDashboardPage />} />
              <Route path="main/sitemap" element={<SitemapPage />} />

              <Route path="product/internet" element={<ProductInternetPage />} />
              <Route path="product/home" element={<ProductHomePage />} />
              <Route path="product/log" element={<ProductLogPage />} />

              <Route path="contract/internet" element={<ContractInternetPage />} />
              <Route path="contract/home" element={<ContractHomePage />} />
              <Route path="contract/log" element={<ContractLogPage />} />
              <Route path="contract/adjust" element={<ContractAdjustPage />} />
              <Route path="contract/balance" element={<ContractBalancePage />} />
              <Route path="contract/write" element={<ContractWritePage />} />

              <Route path="reception/list" element={<ReceptionListPage />} />

              <Route path="invoice/publish" element={<InvoicePublishPage />} />
              <Route path="invoice/my" element={<InvoiceMyPage />} />
              <Route path="invoice/log" element={<InvoiceLogPage />} />

              <Route path="pending/calendar" element={<PendingCalendarPage />} />
              <Route path="pending/pending" element={<PendingPage />} />
              <Route path="pending/log" element={<PendingLogPage />} />

              <Route path="booking/booking" element={<BookingPage />} />
              <Route path="booking/log" element={<BookingLogPage />} />

              <Route path="customer/customer" element={<CustomerPage />} />
              <Route path="customer/log" element={<CustomerLogPage />} />

              <Route path="abooks/abooks-mon" element={<AbooksPage />} />
              <Route path="abooks/log" element={<AbooksLogPage />} />

              <Route path="report/daily" element={<ReportDailyPage />} />
              <Route path="report/monthly" element={<ReportMonthlyPage />} />
              <Route path="report/internet" element={<ReportInternetPage />} />
              <Route path="report/home" element={<ReportHomePage />} />
              <Route path="report/member" element={<ReportMemberPage />} />
              <Route path="report/agency" element={<ReportAgencyPage />} />
              <Route path="report/incentive" element={<ReportIncentivePage />} />

              <Route path="setting/agency" element={<SettingAgencyPage />} />
              <Route path="setting/member" element={<SettingMemberPage />} />
              <Route path="setting/preference" element={<SettingPreferencePage />} />
              <Route path="setting/level" element={<SettingLevelPage />} />
              <Route path="setting/partner1" element={<SettingPartner1Page />} />
              <Route path="setting/partner2" element={<SettingPartner2Page />} />

              <Route path="cs/notice" element={<CsNoticePage />} />
              <Route path="cs/question" element={<CsQuestionPage />} />
              <Route path="bbs/bbs" element={<BbsPage />} />

              <Route path="sitemap" element={<Navigate to="/care/main/sitemap" replace />} />
              <Route path="*" element={<Navigate to="/care/main" replace />} />
            </Route>
          </Route>
          <Route path="/" element={<Navigate to="/care/main" replace />} />
          <Route path="*" element={<Navigate to="/login" replace />} />
        </Routes>
      </AntApp>
    </ConfigProvider>
  )
}
