import { useEffect } from 'react';
import { useNavigate, useSearchParams } from 'react-router-dom';
import { useAuth } from '../context/AuthContext';

const AuthCallback = (): JSX.Element => {
    const [searchParams] = useSearchParams();
    const { handleCallback } = useAuth();
    const navigate = useNavigate();

    useEffect(() => {
        const token = searchParams.get('token');
        const error = searchParams.get('error');

        if (error) {
            navigate(`/login?error=${error}`);
            return;
        }

        if (token) {
            handleCallback(token);
            navigate('/dashboard', { replace: true });
        } else {
            navigate('/login?error=no_token');
        }
    }, []);

    return (
        <div style={{
            minHeight: '100vh',
            display: 'flex',
            alignItems: 'center',
            justifyContent: 'center',
            fontFamily: 'sans-serif',
            fontSize: '18px',
           
        }}>
            ⏳ Connexion en cours...
        </div>
    );
};

export default AuthCallback;