import { useEffect, useState } from "react";
import { useAuth } from "../../context/AuthContext";
import Container from "../../components/shared/Container";
import { Box, Button, TextField, Typography } from "@mui/material";
import { Link, useNavigate } from "react-router-dom";
import { RegisterLink } from "./LoginPage.styles";

const LoginPage = () => {
  const { login, user } = useAuth();
  const [email, setEmail] = useState("");
  const [password, setPassword] = useState("");
  const navigate = useNavigate();

  useEffect(() => {
    if (user) {
      navigate("/");
    }
  }, [user, navigate]);

  const handleSubmit = async (e: React.FormEvent) => {
    e.preventDefault();
    await login({ email, password });
  };

  return (
    <Container>
      <Box sx={{ padding: 4, maxWidth: 360, margin: "auto" }}>
        <Typography variant="h4" mb={3} textAlign="center">
          Login
        </Typography>
        <form onSubmit={handleSubmit}>
          <TextField
            label="Email"
            type="email"
            value={email}
            onChange={(e) => setEmail(e.target.value)}
            fullWidth
            margin="normal"
            required
          />
          <TextField
            label="Password"
            type="password"
            value={password}
            onChange={(e) => setPassword(e.target.value)}
            fullWidth
            margin="normal"
            required
          />
          <Link to="/reset-password">Forgot Password?</Link>
          <Button
            type="submit"
            variant="contained"
            color="primary"
            fullWidth
            sx={{ mt: "10px" }}
          >
            Login
          </Button>
        </form>
      </Box>
      <Box sx={{ display: "flex", alignItems: "center" }}>
        <RegisterLink to={"/register"}>
          Don't have and account? Register now!
        </RegisterLink>
      </Box>
    </Container>
  );
};

export default LoginPage;
