import React from 'react';
import LoginForm from '@/components/auth/LoginForm';
import { useToast } from '@/hooks/use-toast';
import { useNavigate } from 'react-router-dom';
import { useAuth } from '@/contexts/AuthContext';

const LoginContainer: React.FC = () => {
  const { toast } = useToast();
  const navigate = useNavigate();
  const { login, isLoading } = useAuth();

  const handleSubmit = async (email: string, password: string) => {
    if (!email || !password) {
      toast({
        title: "Error",
        description: "Please fill in all fields",
        variant: "destructive",
      });
      return;
    }
    try {
      await login(email, password);
      navigate('/dashboard');
    } catch (err: any) {
      toast({
        title: "Login fehlgeschlagen",
        description: err?.message || "Unbekannter Fehler",
        variant: "destructive",
      });
    }
  };

  return (
    <LoginForm
      onSubmit={handleSubmit}
      isLoading={isLoading}
      defaultEmail={(import.meta as any).env.VITE_LOGIN_EMAIL as string}
      defaultPassword={(import.meta as any).env.VITE_LOGIN_PASSWORD as string}
    />
  );
};

export default LoginContainer;
