import React from "react";
import { Routes, Route, Navigate } from "react-router-dom";
import { useSelector } from "react-redux";
import Layout from "../layouts/Layout";
import Dashboard from "../pages/Dashboard/DashboardPage";
import LoginPage from "../pages/Auth/LoginPage";
import Products from "../pages/Products/ProductList";
import ProductDetails from "../pages/Products/ProductDetails";
import Users from "../pages/Users/UserList";
import UserDetails from "../pages/Users/UserDetails";
import OrderList from "../pages/Orders/OrderList";
import OrderDetails from "../pages/Orders/OrderDetails";
import ProductAdd from "../pages/Products/ProductAdd";
import CategoryList from "../pages/Categories/CategoryList";
import CategoryForm from "../pages/Categories/CategoryForm";
import ReportsDashboard from "../pages/Reports/ReportsDashboard";
import CategoryDetails from "../pages/Categories/CategoryDetails";
import CategoryEdit from "../pages/Categories/CategoryEdit";
import ReorderCategories from "../pages/Categories/ReorderCategories";
// import ReturnsPage from "../pages/Returns/ReturnsPage";
import ReturnDetails from "../pages/Returns/components/ReturnDetails";
import { ReturnList } from "../pages/Returns";
import ReturnHistory from "../pages/Returns/components/ReturnHistory";
import CouponsPage from "../pages/Coupons/CouponsPage";
import CreateCoupon from "../pages/Coupons/CreateCoupon";
import CouponDetails from "../pages/Coupons/CouponDetails";
import CouponStats from "../pages/Dashboard/CouponStats";
import OrdersByStatusPage from "../pages/Dashboard/components/OrdersByStatusPage";

const ProtectedRoute = ({ token, children }) => {
  return token ? children : <Navigate to="/login" replace />;
};

const AdminRoutes = () => {
  const { token } = useSelector((state) => state.auth || {});

  return (
    <Routes>
      {/* Public Route */}
      <Route path="/login" element={<LoginPage />} />

      {/* Protected Routes */}
      <Route
        path="/"
        element={
          <ProtectedRoute token={token}>
            <Layout>
              <Dashboard />
            </Layout>
          </ProtectedRoute>
        }
      />

      <Route
        path="/dashboard"
        element={
          <ProtectedRoute token={token}>
            <Layout>
              <Dashboard />
            </Layout>
          </ProtectedRoute>
        }
      />

       <Route
        path="/orders-by-status"
        element={
          <ProtectedRoute token={token}>
            <Layout>
              <OrdersByStatusPage />
            </Layout>
          </ProtectedRoute>
        }
      />

{/* <Route path="/orders-by-status" element={<OrdersByStatusPage />} /> */}
      <Route
        path="/coupon-stats"
        element={
          <ProtectedRoute token={token}>
            <Layout>
              <CouponStats />
            </Layout>
          </ProtectedRoute>
        }
      />

      <Route
        path="/orders"
        element={
          <ProtectedRoute token={token}>
            <Layout>
              <OrderList />
            </Layout>
          </ProtectedRoute>
        }
      />

      <Route
        path="/orders/:id"
        element={
          <ProtectedRoute token={token}>
            <Layout>
              <OrderDetails />
            </Layout>
          </ProtectedRoute>
        }
      />

      <Route
        path="/users"
        element={
          <ProtectedRoute token={token}>
            <Layout>
              <Users />
            </Layout>
          </ProtectedRoute>
        }
      />
      {/* <Route path="/users/:id" element={<UserDetails />} /> */}
      <Route
        path="/users/:id"
        element={
          <ProtectedRoute token={token}>
            <Layout>
              <UserDetails />
            </Layout>
          </ProtectedRoute>
        }
      />

      <Route
        path="/products"
        element={
          <ProtectedRoute token={token}>
            <Layout>
              <Products />
            </Layout>
          </ProtectedRoute>
        }
      />

      <Route
        path="/products/:slug"
        element={
          <ProtectedRoute token={token}>
            <Layout>
              <ProductDetails />
            </Layout>
          </ProtectedRoute>
        }
      />

      <Route
        path="/products/add"
        element={
          <ProtectedRoute token={token}>
            <Layout>
              <ProductAdd />
            </Layout>
          </ProtectedRoute>
        }
      />

      <Route
        path="/categories"
        element={
          <ProtectedRoute token={token}>
            <Layout>
              <CategoryList />
            </Layout>
          </ProtectedRoute>
        }
      />

      <Route
        path="/categories/add"
        element={
          <ProtectedRoute token={token}>
            <Layout>
              <CategoryForm />
            </Layout>
          </ProtectedRoute>
        }
      />

      <Route
        path="/categories/:id"
        element={
          <ProtectedRoute token={token}>
            <Layout>
              <CategoryDetails />
            </Layout>
          </ProtectedRoute>
        }
      />

      <Route
        path="/categories/edit/:id"
        element={
          <ProtectedRoute token={token}>
            <Layout>
              <CategoryEdit />
            </Layout>
          </ProtectedRoute>
        }
      />

      <Route
        path="/categories/reorder"
        element={
          <ProtectedRoute token={token}>
            <Layout>
              <ReorderCategories />
            </Layout>
          </ProtectedRoute>
        }
      />

      <Route
        path="/reports"
        element={
          <ProtectedRoute token={token}>
            <Layout>
              <ReportsDashboard />
            </Layout>
          </ProtectedRoute>
        }
      />

      <Route
        path="/returns"
        element={
          <ProtectedRoute token={token}>
            <Layout>
              <ReturnList />
            </Layout>
          </ProtectedRoute>
        }
      />

      <Route
        path="/returns/:id"
        element={
          <ProtectedRoute token={token}>
            <Layout>
              <ReturnDetails />
            </Layout>
          </ProtectedRoute>
        }
      />

      <Route
        path="/returns/history"
        element={
          <ProtectedRoute token={token}>
            <Layout>
              <ReturnHistory />
            </Layout>
          </ProtectedRoute>
        }
      />

      <Route
        path="/coupons"
        element={
          <ProtectedRoute token={token}>
            <Layout>
              <CouponsPage />
            </Layout>
          </ProtectedRoute>
        }
      />

      <Route
        path="/create"
        element={
          <ProtectedRoute token={token}>
            <Layout>
              <CreateCoupon />
            </Layout>
          </ProtectedRoute>
        }
      />

      <Route
        path="/coupons/:id"
        element={
          <ProtectedRoute token={token}>
            <Layout>
              <CouponDetails />
            </Layout>
          </ProtectedRoute>
        }
      />

      {/* Catch-all redirect */}
      <Route
        path="*"
        element={
          token ? (
            <Navigate to="/dashboard" replace />
          ) : (
            <Navigate to="/login" replace />
          )
        }
      />
    </Routes>
  );
};

export default AdminRoutes;
