'use client';

import { useEffect } from 'react';
import { useRouter } from 'next/navigation';
import { useAuth } from '../../hooks/useAuth';

interface RedirectToSignInProps {
  returnUrl?: string;
  signInUrl?: string;
}

export function RedirectToSignIn({ 
  returnUrl,
  signInUrl = '/sign-in' 
}: RedirectToSignInProps) {
  const router = useRouter();
  const { isAuthenticated, loading } = useAuth();

  useEffect(() => {
    if (!loading && !isAuthenticated) {
      const url = new URL(signInUrl, window.location.origin);
      if (returnUrl) {
        url.searchParams.set('returnUrl', returnUrl);
      }
      router.push(url.toString());
    }
  }, [isAuthenticated, loading, router, signInUrl, returnUrl]);

  // Show nothing while checking auth
  return null;
}