import { Routes, Route, Navigate } from 'react-router-dom';
import LoginPage from '@/pages/LoginPage';
import MainLayout from '@/layouts/MainLayout';
import Dashboard from '@/pages/Dashboard';

import OrgTree from '@/pages/org/OrgTree';
import AgentList from '@/pages/org/AgentList';
import AgentDetail from '@/pages/org/AgentDetail';
import AgentForm from '@/pages/org/AgentForm';

import CompanyList from '@/pages/product/CompanyList';
import ProductList from '@/pages/product/ProductList';
import ContractList from '@/pages/product/ContractList';

import CommissionRateList from '@/pages/rule/CommissionRateList';
import PayoutRuleList from '@/pages/rule/PayoutRuleList';
import OverrideRuleList from '@/pages/rule/OverrideRuleList';
import ChargebackRuleList from '@/pages/rule/ChargebackRuleList';
import ExceptionCodeList from '@/pages/rule/ExceptionCodeList';

import ReceiveData from '@/pages/receive/ReceiveData';
import ReceiveMapping from '@/pages/receive/ReceiveMapping';

import RecruitLedger from '@/pages/ledger/RecruitLedger';
import MaintainLedger from '@/pages/ledger/MaintainLedger';
import ExceptionLedger from '@/pages/ledger/ExceptionLedger';

import SettleList from '@/pages/settle/SettleList';
import BatchRun from '@/pages/settle/BatchRun';
import PaymentList from '@/pages/settle/PaymentList';

import UserList from '@/pages/system/UserList';
import RoleList from '@/pages/system/RoleList';
import MenuManage from '@/pages/system/MenuManage';
import CodeList from '@/pages/system/CodeList';
import SystemConfig from '@/pages/system/SystemConfig';
import SystemLog from '@/pages/system/SystemLog';

function RequireAuth({ children }: { children: React.ReactNode }) {
  const token = localStorage.getItem('accessToken');
  return token ? <>{children}</> : <Navigate to="/login" replace />;
}

export default function App() {
  return (
    <Routes>
      <Route path="/login" element={<LoginPage />} />
      <Route path="/" element={<RequireAuth><MainLayout /></RequireAuth>}>
        <Route index element={<Dashboard />} />

        {/* 조직 / 인사 */}
        <Route path="org/tree"              element={<OrgTree />} />
        <Route path="org/agents"            element={<AgentList />} />
        <Route path="org/agents/new"        element={<AgentForm />} />
        <Route path="org/agents/:id"        element={<AgentDetail />} />
        <Route path="org/agents/:id/edit"   element={<AgentForm />} />

        {/* 상품 / 계약 */}
        <Route path="companies"             element={<CompanyList />} />
        <Route path="products"              element={<ProductList />} />
        <Route path="contracts"             element={<ContractList />} />

        {/* 수수료 규정 */}
        <Route path="rules/commission"      element={<CommissionRateList />} />
        <Route path="rules/payout"          element={<PayoutRuleList />} />
        <Route path="rules/override"        element={<OverrideRuleList />} />
        <Route path="rules/chargeback"      element={<ChargebackRuleList />} />
        <Route path="rules/exceptions"      element={<ExceptionCodeList />} />

        {/* 데이터 수신 */}
        <Route path="receive/data"          element={<ReceiveData />} />
        <Route path="receive/mapping"       element={<ReceiveMapping />} />

        {/* 원장 */}
        <Route path="ledger/recruit"        element={<RecruitLedger />} />
        <Route path="ledger/maintain"       element={<MaintainLedger />} />
        <Route path="ledger/exception"      element={<ExceptionLedger />} />

        {/* 정산 / 지급 */}
        <Route path="settle"                element={<SettleList />} />
        <Route path="settle/batch"          element={<BatchRun />} />
        <Route path="payments"              element={<PaymentList />} />

        {/* 시스템관리 */}
        <Route path="system/users"          element={<UserList />} />
        <Route path="system/roles"          element={<RoleList />} />
        <Route path="system/menus"          element={<MenuManage />} />
        <Route path="system/codes"          element={<CodeList />} />
        <Route path="system/config"         element={<SystemConfig />} />
        <Route path="system/logs"           element={<SystemLog />} />
      </Route>
    </Routes>
  );
}
