import React, {useEffect} from 'react'
import { Routes, Route, Navigate, useNavigate } from "react-router-dom";

import { Dashboard } from "./Dashboard";
import { Login } from '../Pages/Login'
import { Home } from '../Pages/Home'
import { Puestos } from '../Pages/Puestos'
import { Contras  } from '../Pages/Contras'
import { Expedientes } from '../Pages/Expedientes'
import { Resultados } from '../Pages/Resultados'
import { Configuracion } from '../Pages/Configuracion'
import { Historial } from '../Pages/Historial'
import { Pruebas } from '../Pages/Pruebas'
import { PruebaNueva } from '../Pages/PruebaNueva'
import { PruebaAsignar } from '../Pages/PruebaAsignar'
import { NotFound } from '../Pages/404'
import { Profile } from '../Pages/Profile'

import useAuth from '../Auth/useAuth'

export default function MyRoutes () {

    let auth = useAuth();
    let navigate = useNavigate()

    useEffect(() => {
        if(!auth.isLogged(0)){
            return navigate('/')
        }
    },[auth,navigate])
    

    return(
        <Routes>

            <Route path="/" element={auth.isLogged() ? <Navigate to='dashboard/home'/> : <Login/>}/>

            {
                auth.isLogged() ? 
                    (
                        <Route path="dashboard" element={<Dashboard/>}>
                            <Route path="home" element={<Home/>} />
                            <Route path="puestos" element={<Puestos/>} />
                            <Route path="perfil" element={<Profile/>} />
                            <Route path="contrasenas" element={<Contras/>} />
                            <Route path="expedientes" element={<Expedientes/>} />
                            <Route path="resultados" element={<Resultados/>} />
                            <Route path="configuraciones" element={<Configuracion/>} />
                            <Route path="historial" element={<Historial/>} />
                            <Route path="pruebas/listar" element={<Pruebas/>} />
                            <Route path="pruebas/crear" element={<PruebaNueva/>} />
                            <Route path="pruebas/aplicar" element={<PruebaAsignar/>} />
                        </Route>
                    )
                : ( <Route path="dashboard" element={<Navigate to='/'/>} />)
            }



            <Route path="*" element={<NotFound/>}/>

        </Routes>
    )
}

