"use client";

import { useState } from "react";
import Link from "next/link";
import { useRouter } from "next/navigation";
import { createBrowserSupabaseClient } from "@/lib/supabase";

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 supabase = createBrowserSupabaseClient();
    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.");
      } else {
        setFehler("E-Mail oder Passwort ungültig.");
      }
    } else {
      router.push("/kunden/dashboard");
    }
    setLoading(false);
  }

  return (
    <div className="min-h-screen bg-[#111925] flex items-center justify-center px-4">
      <div className="w-full max-w-sm">
        <div className="bg-[#18212f] border border-gray-800 rounded-2xl p-8">
          <div className="mb-8">
            <div className="w-10 h-10 rounded-xl bg-orange-500/10 border border-orange-500/30 flex items-center justify-center mb-4">
              <svg className="w-5 h-5 text-orange-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M16 7a4 4 0 11-8 0 4 4 0 018 0zM12 14a7 7 0 00-7 7h14a7 7 0 00-7-7z" />
              </svg>
            </div>
            <h1 className="text-xl font-bold text-white tracking-tight">Kunden-Login</h1>
            <p className="text-sm text-slate-500 mt-1">Melde dich an, um deine IT-Anfragen einzusehen</p>
          </div>

          <form onSubmit={handleLogin} className="space-y-4">
            <div>
              <label htmlFor="kl-email" className="block text-sm font-medium text-slate-400 mb-1.5">
                E-Mail-Adresse
              </label>
              <input
                id="kl-email"
                type="email"
                value={email}
                onChange={(e) => setEmail(e.target.value)}
                placeholder="ihre@email.de"
                required
                autoComplete="email"
                className="w-full px-4 py-3 rounded-xl bg-[#111925] border border-gray-700 text-white placeholder-slate-600 focus:outline-none focus:border-orange-500/60 focus:ring-1 focus:ring-orange-500/20 transition-colors"
              />
            </div>
            <div>
              <label htmlFor="kl-passwort" className="block text-sm font-medium text-slate-400 mb-1.5">
                Passwort
              </label>
              <input
                id="kl-passwort"
                type="password"
                value={passwort}
                onChange={(e) => setPasswort(e.target.value)}
                placeholder="••••••••"
                required
                autoComplete="current-password"
                className="w-full px-4 py-3 rounded-xl bg-[#111925] border border-gray-700 text-white placeholder-slate-600 focus:outline-none focus:border-orange-500/60 focus:ring-1 focus:ring-orange-500/20 transition-colors"
              />
            </div>

            {fehler && (
              <p className="text-sm text-red-400 bg-red-500/10 border border-red-500/20 px-3 py-2 rounded-lg">
                {fehler}
              </p>
            )}

            <button
              type="submit"
              disabled={loading}
              className="w-full py-3 px-4 bg-orange-500 hover:bg-orange-600 disabled:opacity-60 disabled:cursor-not-allowed text-white font-semibold rounded-xl transition-colors"
            >
              {loading ? "Wird angemeldet…" : "Anmelden"}
            </button>
          </form>

          <div className="mt-6 pt-5 border-t border-gray-800 text-center space-y-3">
            <p className="text-sm text-slate-500">
              Noch kein Konto?{" "}
              <Link href="/kunden/registrieren" className="text-orange-400 hover:text-orange-300 font-medium">
                Jetzt registrieren
              </Link>
            </p>
            <p className="text-xs text-slate-600">
              Fragen?{" "}
              <a href="tel:+4917193451093" data-source-element="kunden-login" className="text-slate-500 hover:text-orange-400 transition-colors">
                +49 171 9345193
              </a>
            </p>
          </div>
        </div>
      </div>
    </div>
  );
}
