import { useState, useEffect } from "react";
import { useLocation } from "wouter";
import { useLanguage } from "../contexts/LanguageContext";
import { useAuth } from "../contexts/AuthContext";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { Input } from "@/components/ui/input";
import { Button } from "@/components/ui/button";
import { Label } from "@/components/ui/label";
import { Lock } from "lucide-react";
import ehsanLogo from "../assets/ehsan-logo.png";

export default function Login() {
  const { t } = useLanguage();
  const { login, isAuthenticated } = useAuth();
  const [, setLocation] = useLocation();
  const [username, setUsername] = useState("");
  const [password, setPassword] = useState("");
  const [error, setError] = useState(false);

  useEffect(() => {
    if (isAuthenticated) setLocation("/admin");
  }, [isAuthenticated, setLocation]);

  const handleSubmit = (e: React.FormEvent) => {
    e.preventDefault();
    const ok = login(username, password);
    if (ok) {
      setLocation("/admin");
    } else {
      setError(true);
    }
  };

  return (
    <div className="container mx-auto px-4 py-16 flex justify-center">
      <Card className="w-full max-w-md">
        <CardHeader className="text-center space-y-3">
          <img src={ehsanLogo} alt="EHSAN" className="h-12 w-auto mx-auto object-contain" />
          <div className="mx-auto w-12 h-12 rounded-full bg-primary/10 flex items-center justify-center">
            <Lock className="w-6 h-6 text-primary" />
          </div>
          <CardTitle className="text-xl">{t.auth.title}</CardTitle>
          <p className="text-sm text-muted-foreground">{t.auth.subtitle}</p>
        </CardHeader>
        <CardContent>
          <form onSubmit={handleSubmit} className="space-y-4">
            <div className="space-y-2">
              <Label htmlFor="username">{t.auth.username}</Label>
              <Input
                id="username"
                value={username}
                onChange={(e) => { setUsername(e.target.value); setError(false); }}
                autoComplete="username"
                data-testid="input-username"
              />
            </div>
            <div className="space-y-2">
              <Label htmlFor="password">{t.auth.password}</Label>
              <Input
                id="password"
                type="password"
                value={password}
                onChange={(e) => { setPassword(e.target.value); setError(false); }}
                autoComplete="current-password"
                data-testid="input-password"
              />
            </div>
            {error && (
              <p className="text-sm text-destructive" data-testid="text-loginError">
                {t.auth.error}
              </p>
            )}
            <Button type="submit" className="w-full" data-testid="button-signIn">
              {t.auth.signIn}
            </Button>
            <p className="text-xs text-center text-muted-foreground bg-muted/40 rounded-md py-2 px-3">
              {t.auth.demoHint}
            </p>
          </form>
        </CardContent>
      </Card>
    </div>
  );
}
