import React from 'react';
import { Routes, Route, Navigate, useLocation } from 'react-router-dom';
import Container from '@/components/Layout/Container';
import { AccountLayout } from './components/AccountLayout';
import Dashboard from './Dashboard';
import Orders from './Orders';
import OrderDetails from './OrderDetails';
import Downloads from './Downloads';
import Addresses from './Addresses';
import Wishlist from './Wishlist';
import AccountDetails from './AccountDetails';
import Licenses from './Licenses';

export default function Account() {
  const user = (window as any).woonoowCustomer?.user;
  const location = useLocation();

  // Redirect to login if not authenticated
  if (!user?.isLoggedIn) {
    const currentPath = location.pathname;
    return <Navigate to={`/login?redirect=${encodeURIComponent(currentPath)}`} replace />;
  }

  return (
    <Container>
      <AccountLayout>
        <Routes>
          <Route index element={<Dashboard />} />
          <Route path="orders" element={<Orders />} />
          <Route path="orders/:orderId" element={<OrderDetails />} />
          <Route path="downloads" element={<Downloads />} />
          <Route path="addresses" element={<Addresses />} />
          <Route path="wishlist" element={<Wishlist />} />
          <Route path="licenses" element={<Licenses />} />
          <Route path="account-details" element={<AccountDetails />} />
          <Route path="*" element={<Navigate to="/my-account" replace />} />
        </Routes>
      </AccountLayout>
    </Container>
  );
}

