import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { BrowserRouter, Routes, Route, Navigate } from 'react-router-dom'
import { ShopProvider } from './store/shop'

// 스토어프론트(고객)
import ZipGate from './shop/ZipGate'
import ShopLayout from './shop/ShopLayout'
import Home from './shop/Home'
import Category from './shop/Category'
import Search from './shop/Search'
import ProductDetail from './shop/ProductDetail'
import Cart from './shop/Cart'
import Checkout from './shop/Checkout'
import Subscription from './shop/Subscription'
import DailyStandard from './shop/DailyStandard'
import Events from './shop/Events'
import Wishlist from './shop/Wishlist'
import Mypage from './shop/Mypage'
import Orders from './shop/Orders'
import ReviewWrite from './shop/ReviewWrite'
import Account from './shop/Account'
import Cs from './shop/Cs'
import CustomerApp from './shop/CustomerApp'

// 관리자 웹
import AdminLogin from './admin/AdminLogin'
import AdminLayout from './admin/AdminLayout'
import Dashboard from './admin/Dashboard'
import Stores from './admin/Stores'
import Products from './admin/Products'
import Inventory from './admin/Inventory'
import AdminOrders from './admin/Orders'
import Transfers from './admin/Transfers'
import Members from './admin/Members'
import Loyalty from './admin/Loyalty'
import AdminEvents from './admin/Events'
import Subscriptions from './admin/Subscriptions'
import Schedule from './admin/Schedule'
import Analytics from './admin/Analytics'
import UserManagement from './admin/UserManagement'
import AuditLog from './admin/AuditLog'
import Settings from './admin/Settings'
import AdminApp from './admin/AdminApp'

// UIWS 이식 — 업무 모듈(관리자 영역 병합, 고객 쇼핑 화면 무영향)
import UiwsLayout from './pages/uiws/UiwsLayout'
import UiwsWorklog from './pages/uiws/WorklogList'
import UiwsSchedule from './pages/uiws/ScheduleCalendar'
import UiwsMessage from './pages/uiws/MessageBox'
import UiwsStats from './pages/uiws/StatsPivot'

const qc = new QueryClient({
  defaultOptions: { queries: { retry: 1, refetchOnWindowFocus: false, staleTime: 30_000 } },
})

export default function App() {
  return (
    <QueryClientProvider client={qc}>
      <ShopProvider>
        <BrowserRouter>
          <Routes>
            {/* 진입 ZIP 게이트 (Floom) */}
            <Route path="/" element={<ZipGate />} />
            <Route path="/account" element={<Account />} />

            {/* 스토어프론트(고객) */}
            <Route element={<ShopLayout />}>
              <Route path="/home" element={<Home />} />
              <Route path="/category" element={<Category />} />
              <Route path="/search" element={<Search />} />
              <Route path="/product/:id" element={<ProductDetail />} />
              <Route path="/cart" element={<Cart />} />
              <Route path="/checkout" element={<Checkout />} />
              <Route path="/subscription" element={<Subscription />} />
              <Route path="/daily-standard" element={<DailyStandard />} />
              <Route path="/events" element={<Events />} />
              <Route path="/wishlist" element={<Wishlist />} />
              <Route path="/mypage" element={<Mypage />} />
              <Route path="/orders" element={<Orders />} />
              <Route path="/review/:productId" element={<ReviewWrite />} />
              <Route path="/cs" element={<Cs />} />
              <Route path="/app" element={<CustomerApp />} />
            </Route>

            {/* 관리자 웹 (NCloud 다크) */}
            <Route path="/admin/login" element={<AdminLogin />} />
            <Route path="/admin" element={<AdminLayout />}>
              <Route index element={<Navigate to="/admin/dashboard" replace />} />
              <Route path="dashboard" element={<Dashboard />} />
              <Route path="stores" element={<Stores />} />
              <Route path="products" element={<Products />} />
              <Route path="inventory" element={<Inventory />} />
              <Route path="orders" element={<AdminOrders />} />
              <Route path="transfers" element={<Transfers />} />
              <Route path="members" element={<Members />} />
              <Route path="loyalty" element={<Loyalty />} />
              <Route path="events" element={<AdminEvents />} />
              <Route path="subscriptions" element={<Subscriptions />} />
              <Route path="schedule" element={<Schedule />} />
              <Route path="analytics" element={<Analytics />} />
              <Route path="users" element={<UserManagement />} />
              <Route path="audit" element={<AuditLog />} />
              <Route path="settings" element={<Settings />} />
              <Route path="app" element={<AdminApp />} />
              {/* UIWS 이식 업무 모듈 (관리자 영역 병합) */}
              <Route path="uiws/worklog" element={<UiwsLayout><UiwsWorklog /></UiwsLayout>} />
              <Route path="uiws/schedule" element={<UiwsLayout><UiwsSchedule /></UiwsLayout>} />
              <Route path="uiws/message" element={<UiwsLayout><UiwsMessage /></UiwsLayout>} />
              <Route path="uiws/stats" element={<UiwsLayout><UiwsStats /></UiwsLayout>} />
            </Route>

            <Route path="*" element={<Navigate to="/" replace />} />
          </Routes>
        </BrowserRouter>
      </ShopProvider>
    </QueryClientProvider>
  )
}
