import { Navigate, Route, Routes } from 'react-router-dom';
import { usePartnerSession } from './contexts/PartnerSessionContext';
import { getPartnerNavKind, isSubAccount } from './lib/partnerAccess';
import TabLayout from './layouts/TabLayout';
import SubAccountLayout from './layouts/SubAccountLayout';
import HomePage from './pages/HomePage';
import StoreListPage from './pages/StoreListPage';
import StoreDetailPage from './pages/StoreDetailPage';
import StorePackagesPage from './pages/StorePackagesPage';
import StoreCreatePage from './pages/StoreCreatePage';
import OrderListPage from './pages/OrderListPage';
import OrderDetailPage from './pages/OrderDetailPage';
import CenterPage from './pages/CenterPage';
import PartnerMePage from './pages/PartnerMePage';
import BillsPage from './pages/BillsPage';
import SettlementPage from './pages/SettlementPage';
import WeeklyReportPage from './pages/WeeklyReportPage';
import ProxyOrderPage from './pages/ProxyOrderPage';
import ProxyOrderProductsPage from './pages/ProxyOrderProductsPage';
import ProxyOrderListPage from './pages/ProxyOrderListPage';
import ProxyOrderDetailPage from './pages/ProxyOrderDetailPage';
import StaffListPage from './pages/StaffListPage';
import StaffCreatePage from './pages/StaffCreatePage';
import LeaderboardPage from './pages/LeaderboardPage';
import UsersManagePage from './pages/UsersManagePage';
import AssocOrdersPage from './pages/AssocOrdersPage';
import CommissionOrdersPage from './pages/CommissionOrdersPage';
import ActivityPostersPage from './pages/ActivityPostersPage';

function PrimaryRoutes() {
  return (
    <Routes>
      <Route element={<TabLayout />}>
        <Route path="/" element={<HomePage />} />
        <Route path="/users" element={<UsersManagePage />} />
        <Route path="/stores" element={<StoreListPage />} />
        <Route path="/center" element={<CenterPage />} />
      </Route>
      <Route path="/stores/new" element={<StoreCreatePage />} />
      <Route path="/stores/:id/packages" element={<StorePackagesPage />} />
      <Route path="/center/bills" element={<BillsPage />} />
      <Route path="/center/settlement" element={<SettlementPage />} />
      <Route path="/center/staff" element={<StaffListPage />} />
      <Route path="/center/staff/new" element={<StaffCreatePage />} />
      <Route path="/center/activity-posters" element={<ActivityPostersPage />} />
      <Route path="/center/assoc" element={<Navigate to="/users" replace />} />
      <Route path="/center/assoc/users" element={<Navigate to="/users" replace />} />
      <Route path="/users/orders" element={<AssocOrdersPage />} />
      <Route path="/users/:userId/orders" element={<AssocOrdersPage />} />
      <Route path="/center/commissions" element={<CommissionOrdersPage />} />
      <Route path="/center/proxy-orders" element={<ProxyOrderListPage />} />
      <Route path="/center/proxy-orders/:id" element={<ProxyOrderDetailPage />} />
      <Route path="/proxy-order" element={<ProxyOrderPage />} />
      <Route path="/proxy-order/products" element={<ProxyOrderProductsPage />} />
      <Route path="/reports/weekly" element={<WeeklyReportPage />} />
      <Route path="/leaderboard" element={<LeaderboardPage />} />
      <Route path="/stores/:id" element={<StoreDetailPage />} />
      <Route path="/orders" element={<OrderListPage />} />
      <Route path="/orders/:id" element={<OrderDetailPage />} />
      <Route path="*" element={<Navigate to="/" replace />} />
    </Routes>
  );
}

function SubAccountRoutes() {
  const { account } = usePartnerSession();
  const navKind = getPartnerNavKind(account);
  const isWarehouse = navKind === 'warehouse_staff';

  return (
    <Routes>
      <Route element={<SubAccountLayout navKind={navKind} />}>
        <Route path="/" element={<HomePage />} />
        {isWarehouse ? (
          <Route path="/orders" element={<OrderListPage tabRoot />} />
        ) : (
          <Route path="/stores" element={<StoreListPage />} />
        )}
        <Route path="/me" element={<PartnerMePage />} />
      </Route>
      {!isWarehouse && (
        <>
          <Route path="/stores/new" element={<StoreCreatePage />} />
          <Route path="/stores/:id/packages" element={<StorePackagesPage />} />
          <Route path="/stores/:id" element={<StoreDetailPage />} />
        </>
      )}
      <Route path="/leaderboard" element={<LeaderboardPage />} />
      {isWarehouse && <Route path="/orders/:id" element={<OrderDetailPage />} />}
      <Route path="*" element={<Navigate to="/" replace />} />
    </Routes>
  );
}

export default function PartnerAppRoutes() {
  const { account, authenticated } = usePartnerSession();

  if (!authenticated) {
    return null;
  }

  if (isSubAccount(account)) {
    return <SubAccountRoutes />;
  }
  return <PrimaryRoutes />;
}
