"use client";

import { useState } from "react";
import Link from "next/link";
import { useRouter } from "next/navigation";
import { LogIn } from "lucide-react";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { supabase } from "@/lib/supabase";
import { config } from "@/lib/config";

export default function KundenLoginPage() {
  const router = useRouter();
  const [email, setEmail] = useState("");
  const [passwort, setPasswort] = useState("");
  const [fehler, setFehler] = useState("");
  const [loading, setLoading] = useState(false);

  async function handleLogin(e: React.FormEvent) {
    e.preventDefault();
    setFehler("");
    setLoading(true);

    const { error } = await supabase.auth.signInWithPassword({
      email,
      password: passwort,
    });

    if (error) {
      if (error.message.toLowerCase().includes("email not confirmed")) {
        setFehler("Bitte bestätigen Sie zuerst Ihre E-Mail-Adresse. Schauen Sie in Ihren Posteingang.");
      } else {
        setFehler("E-Mail oder Passwort ungültig.");
      }
    } else {
      router.push("/kunden/dashboard");
    }
    setLoading(false);
  }

  return (
    <div className="min-h-[80dvh] flex items-center justify-center px-4">
      <div className="w-full max-w-md">
        {/* Header */}
        <div className="text-center mb-8">
          <div className="inline-flex items-center justify-center w-12 h-12 bg-[#1c1917] rounded-md mb-4">
            <LogIn className="w-6 h-6 text-[#f7d334]" />
          </div>
          <h1 className="text-2xl font-bold text-slate-900 tracking-tight">Kunden-Login</h1>
          <p className="text-slate-500 text-sm mt-2">
            Melden Sie sich an, um Ihre Mietanfragen einzusehen.
          </p>
        </div>

        <div className="border border-slate-200 bg-white p-6 space-y-4">
          <form onSubmit={handleLogin} className="space-y-4">
            <div>
              <Label htmlFor="kl-email">E-Mail-Adresse</Label>
              <Input
                id="kl-email"
                type="email"
                value={email}
                onChange={(e) => setEmail(e.target.value)}
                placeholder="ihre@email.de"
                required
                className="mt-1 rounded-md"
                autoComplete="email"
              />
            </div>
            <div>
              <Label htmlFor="kl-passwort">Passwort</Label>
              <Input
                id="kl-passwort"
                type="password"
                value={passwort}
                onChange={(e) => setPasswort(e.target.value)}
                placeholder="••••••••"
                required
                className="mt-1 rounded-md"
                autoComplete="current-password"
              />
            </div>

            {fehler && (
              <p className="text-sm text-red-500 bg-red-50 border border-red-200 px-3 py-2 rounded-md">
                {fehler}
              </p>
            )}

            <Button
              type="submit"
              disabled={loading}
              className="w-full bg-[#f7d334] hover:bg-[#fcd34d] text-[#1c1917] rounded-md font-semibold border-transparent py-2.5"
            >
              {loading ? "Wird angemeldet…" : "Anmelden"}
            </Button>
          </form>

          <div className="border-t border-slate-100 pt-4 text-center space-y-2">
            <p className="text-sm text-slate-500">
              Noch kein Konto?{" "}
              <Link href="/kunden/registrieren" className="text-[#1c1917] font-medium hover:underline underline-offset-2">
                Jetzt registrieren
              </Link>
            </p>
          </div>
        </div>

        <p className="text-center text-xs text-slate-400 mt-4">
          Fragen?{" "}
          <a href={`tel:${config.company.phonePlain}`} data-source-element="kunden-login" className="hover:underline">
            {config.company.phone}
          </a>
        </p>
      </div>
    </div>
  );
}
