// AppRoutes.jsx
import React from 'react';
import { Routes, Route, Navigate, useLocation } from 'react-router-dom';
import { CSSTransition, SwitchTransition } from 'react-transition-group';

import Dashboard from '../pages/Dashboard';
import EditorPage from '../pages/EditorPage';
import LoginPage from '../pages/Login';
import NotFound from '../pages/NotFound';

const AppRoutes = ({ isLoggedIn, onLogin }) => {
    const location = useLocation(); // <-- get current location
    const nodeRef = React.useRef(null);

    return (
        <SwitchTransition>
        <CSSTransition
            key={location.pathname} // <-- must use location.pathname
            nodeRef={nodeRef}
            classNames="page"
            timeout={300}
        >
            <div ref={nodeRef}>
            <Routes location={location}>
                <Route
                path="/"
                element={isLoggedIn ? <Dashboard /> : <Navigate to="/login" replace />}
                />
                <Route
                path="/dashboard"
                element={isLoggedIn ? <Dashboard /> : <Navigate to="/login" replace />}
                />
                <Route
                path="/editor"
                element={isLoggedIn ? <EditorPage /> : <Navigate to="/login" replace />}
                />
                <Route
                path="/editor/:slug"
                element={isLoggedIn ? <EditorPage /> : <Navigate to="/login" replace />}
                />
                <Route
                path="/posts"
                element={isLoggedIn ? <Dashboard /> : <Navigate to="/login" replace />}
                />
                <Route
                path="/drafts"
                element={isLoggedIn ? <Dashboard /> : <Navigate to="/login" replace />}
                />
                <Route path="/login" element={<LoginPage onLogin={onLogin} />} />
                <Route path="*" element={<NotFound />} />
            </Routes>
            </div>
        </CSSTransition>
        </SwitchTransition>
    );
};

export default AppRoutes;
