import React, { useContext } from 'react';
import { Routes, Route, Navigate } from 'react-router-dom';
import AuthContext from '../context/AuthContext';

// Components
import Login from '../components/auth/Login';
import Register from '../components/auth/Register';
import LandingPage from '../components/pages/LandingPage';
import Dashboard from '../components/dashboard/Dashboard';
import ProjectDetail from '../components/projects/ProjectDetail';
import ProjectForm from '../components/projects/ProjectForm';
import ProjectShare from '../components/projects/ProjectShare';
import ShareableProjects from '../components/projects/ShareableProjects';
import SharedProjectView from '../components/projects/SharedProjectView';
import UserManagement from '../components/users/UserManagement';
import NotFound from '../components/common/NotFound';

// Protected Route component
const ProtectedRoute = ({ children, requiredRoles = [] }) => {
  const { isAuthenticated, user, hasRole } = useContext(AuthContext);
  
  // Check if user is authenticated
  if (!isAuthenticated()) {
    return <Navigate to="/login" />;
  }
  
  // If roles are specified, check if user has required role
  if (requiredRoles.length > 0) {
    const hasRequiredRole = requiredRoles.some(role => hasRole(role));
    if (!hasRequiredRole) {
      return <Navigate to="/dashboard" />;
    }
  }
  
  return children;
};

const AppRoutes = () => {
  return (
    <Routes>
      {/* Public routes */}
      <Route path="/" element={<LandingPage />} />
      <Route path="/login" element={<Login />} />
      <Route path="/register" element={<Register />} />
      <Route path="/shared/:shareId" element={<SharedProjectView />} />
      
      {/* Protected routes */}
      <Route 
        path="/home" 
        element={
          <ProtectedRoute>
            <Navigate to="/dashboard" />
          </ProtectedRoute>
        } 
      />
      
      <Route 
        path="/dashboard" 
        element={
          <ProtectedRoute>
            <Dashboard />
          </ProtectedRoute>
        } 
      />
      
      <Route 
        path="/projects/:id" 
        element={
          <ProtectedRoute>
            <ProjectDetail />
          </ProtectedRoute>
        } 
      />
      
      <Route 
        path="/projects/new" 
        element={
          <ProtectedRoute requiredRoles={['super_admin', 'admin']}>
            <ProjectForm />
          </ProtectedRoute>
        } 
      />
      
      <Route 
        path="/projects/:id/edit" 
        element={
          <ProtectedRoute requiredRoles={['super_admin', 'admin']}>
            <ProjectForm />
          </ProtectedRoute>
        } 
      />
      
      <Route 
        path="/projects/:id/share" 
        element={
          <ProtectedRoute requiredRoles={['super_admin', 'admin']}>
            <ProjectShare />
          </ProtectedRoute>
        } 
      />
      
      <Route 
        path="/projects/shareable" 
        element={
          <ProtectedRoute requiredRoles={['super_admin', 'admin']}>
            <ShareableProjects />
          </ProtectedRoute>
        } 
      />
      
      <Route 
        path="/users" 
        element={
          <ProtectedRoute requiredRoles={['super_admin']}>
            <UserManagement />
          </ProtectedRoute>
        } 
      />
      
      {/* Not found route */}
      <Route path="*" element={<NotFound />} />
    </Routes>
  );
};

export default AppRoutes;
