"use client";

import { zodResolver } from "@hookform/resolvers/zod";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { Check, KeyRound, RotateCcw, Search, UserPlus2 } from "lucide-react";
import { useMemo, useState } from "react";
import { useForm } from "react-hook-form";
import { z } from "zod";
import { useAuth } from "@/components/auth";
import { apiDelete, apiFetch, apiGet, Paginated, User } from "@/lib/api";

const schema = z.object({
  first_name: z.string().min(1, "Vorname erforderlich"),
  last_name: z.string().min(1, "Nachname erforderlich"),
  email: z.string().email("Gueltige E-Mail erforderlich"),
  role: z.enum(["admin", "pruefer", "mitarbeiter", "leser"]),
  is_active: z.boolean().default(true),
  must_change_password: z.boolean().default(true),
  temporary_password: z.string().min(12, "Mindestens 12 Zeichen")
});

type UserForm = z.infer<typeof schema>;

const roleLabels: Record<User["role"], string> = {
  admin: "ADMIN",
  pruefer: "PRUEFER",
  mitarbeiter: "MITARBEITER",
  leser: "LESER"
};

export default function UsersPage() {
  const { token, user } = useAuth();
  const client = useQueryClient();
  const [search, setSearch] = useState("");
  const [role, setRole] = useState("");
  const [active, setActive] = useState("");
  const [page, setPage] = useState(1);
  const [sortBy, setSortBy] = useState("created_at");
  const [sortOrder, setSortOrder] = useState<"asc" | "desc">("desc");
  const [editing, setEditing] = useState<User | null>(null);
  const [temporaryPassword, setTemporaryPassword] = useState("");
  const [message, setMessage] = useState("");
  const [error, setError] = useState("");

  const form = useForm<UserForm>({
    resolver: zodResolver(schema),
    defaultValues: {
      first_name: "",
      last_name: "",
      email: "",
      role: "mitarbeiter",
      is_active: true,
      must_change_password: true,
      temporary_password: ""
    }
  });

  const queryKey = useMemo(
    () => ["users", token, page, search, role, active, sortBy, sortOrder],
    [token, page, search, role, active, sortBy, sortOrder]
  );

  const users = useQuery({
    queryKey,
    queryFn: () =>
      apiGet<Paginated<User>>(
        `/users?page=${page}&page_size=10&search=${encodeURIComponent(search)}&role=${encodeURIComponent(role)}&active=${encodeURIComponent(active)}&sort_by=${encodeURIComponent(sortBy)}&sort_order=${sortOrder}`,
        token ?? ""
      ),
    enabled: Boolean(token)
  });

  const saveMutation = useMutation({
    mutationFn: async (values: UserForm) => {
      const payload = {
        ...values,
        email: values.email.toLowerCase()
      };
      if (editing) {
        return apiFetch<User>(`/users/${editing.id}`, token ?? "", { method: "PUT", body: JSON.stringify(payload) });
      }
      return apiFetch<{ temporary_password: string; user: User }>(`/users`, token ?? "", {
        method: "POST",
        body: JSON.stringify(payload)
      });
    },
    onSuccess: async (created) => {
      if (!editing) {
        setTemporaryPassword((created as { temporary_password?: string }).temporary_password ?? "");
      }
      setMessage(editing ? "Benutzer gespeichert" : "Benutzer angelegt");
      setError("");
      setEditing(null);
      form.reset();
      await client.invalidateQueries({ queryKey: ["users"] });
    },
    onError: (err: Error) => setError(err.message)
  });

  const deactivateMutation = useMutation({
    mutationFn: (item: User) => apiFetch<User>(`/users/${item.id}/deactivate`, token ?? "", { method: "POST" }),
    onSuccess: () => client.invalidateQueries({ queryKey: ["users"] })
  });
  const activateMutation = useMutation({
    mutationFn: (item: User) => apiFetch<User>(`/users/${item.id}/activate`, token ?? "", { method: "POST" }),
    onSuccess: () => client.invalidateQueries({ queryKey: ["users"] })
  });
  const resetMutation = useMutation({
    mutationFn: (item: User) => apiFetch<{ temporary_password: string }>(`/users/${item.id}/reset-password`, token ?? "", { method: "POST" }),
    onSuccess: (result) => {
      setTemporaryPassword(result.temporary_password);
      setMessage("Temporäres Passwort erzeugt");
    }
  });

  if (user?.role !== "admin") {
    return <div className="rounded-lg border border-border bg-surface p-6 text-text-light">Nur fuer Administratoren verfuegbar.</div>;
  }

  function startCreate() {
    setEditing(null);
    form.reset({ first_name: "", last_name: "", email: "", role: "mitarbeiter", is_active: true, must_change_password: true, temporary_password: "" });
  }

  function startEdit(item: User) {
    setEditing(item);
    form.reset({
      first_name: item.first_name,
      last_name: item.last_name,
      email: item.email,
      role: item.role,
      is_active: item.is_active,
      must_change_password: item.must_change_password,
      temporary_password: ""
    });
  }

  return (
    <div className="space-y-6">
      <header className="flex flex-col gap-3 md:flex-row md:items-center md:justify-between">
        <div>
          <h1 className="text-3xl font-semibold text-text">Benutzer</h1>
          <p className="mt-2 text-text-light">Verwaltung von Rollen, Aktivstatus und Passworten.</p>
        </div>
        <button type="button" className="btn btn-primary h-12" onClick={startCreate}><UserPlus2 className="h-4 w-4" /> Benutzer anlegen</button>
      </header>
      <section className="rounded-lg border border-border bg-surface p-4 shadow-soft">
        <div className="grid gap-3 md:grid-cols-4">
          <label className="flex h-12 items-center gap-2 rounded-lg border border-border px-3 md:col-span-2">
            <Search className="h-4 w-4 text-primary" />
            <input value={search} onChange={(e) => setSearch(e.target.value)} className="flex-1 outline-none" placeholder="Suchen" />
          </label>
          <select value={role} onChange={(e) => setRole(e.target.value)} className="h-12 rounded-lg border border-border px-3">
            <option value="">Alle Rollen</option>
            {Object.entries(roleLabels).map(([value, label]) => <option key={value} value={value}>{label}</option>)}
          </select>
          <select value={active} onChange={(e) => setActive(e.target.value)} className="h-12 rounded-lg border border-border px-3">
            <option value="">Alle Status</option>
            <option value="true">Aktiv</option>
            <option value="false">Inaktiv</option>
          </select>
          <select value={sortBy} onChange={(e) => setSortBy(e.target.value)} className="h-12 rounded-lg border border-border px-3">
            <option value="created_at">Erstellt am</option>
            <option value="name">Name</option>
            <option value="email">E-Mail</option>
            <option value="role">Rolle</option>
            <option value="last_login_at">Letzter Login</option>
            <option value="password_changed_at">Passwort geaendert</option>
          </select>
          <button type="button" className="btn btn-secondary h-12" onClick={() => setSortOrder((value) => (value === "asc" ? "desc" : "asc"))}>
            Sortierung: {sortOrder.toUpperCase()}
          </button>
        </div>
      </section>
      <section className="overflow-hidden rounded-lg border border-border bg-surface shadow-soft">
        <div className="overflow-x-auto">
          <table className="min-w-full text-left text-sm">
            <thead className="bg-background text-xs uppercase text-text-light">
              <tr>
                <th className="px-5 py-4">Name</th>
                <th className="px-5 py-4">E-Mail</th>
                <th className="px-5 py-4">Rolle</th>
                <th className="px-5 py-4">Status</th>
                <th className="px-5 py-4">Letzter Login</th>
                <th className="px-5 py-4">Passwort zuletzt geaendert</th>
                <th className="px-5 py-4 text-right">Aktionen</th>
              </tr>
            </thead>
            <tbody className="divide-y divide-border">
              {(users.data?.items ?? []).map((item) => (
                <tr key={item.id}>
                  <td className="px-5 py-4">{item.first_name} {item.last_name}</td>
                  <td className="px-5 py-4">{item.email}</td>
                  <td className="px-5 py-4">{roleLabels[item.role]}</td>
                  <td className="px-5 py-4">{item.is_active ? "Aktiv" : "Inaktiv"}</td>
                  <td className="px-5 py-4">{item.last_login_at ?? "nicht erfasst"}</td>
                  <td className="px-5 py-4">{item.password_changed_at ?? "nicht erfasst"}</td>
                  <td className="px-5 py-4">
                    <div className="flex justify-end gap-2">
                      <button type="button" className="btn btn-secondary h-10 px-3" onClick={() => startEdit(item)}>Bearbeiten</button>
                      <button type="button" className="btn btn-secondary h-10 px-3" onClick={() => resetMutation.mutate(item)}>{resetMutation.isPending ? <span className="spinner" /> : <RotateCcw className="h-4 w-4" />} Temporäres Passwort setzen</button>
                      {item.is_active ? (
                        <button type="button" className="btn btn-danger h-10 px-3" onClick={() => deactivateMutation.mutate(item)}>Deaktivieren</button>
                      ) : (
                        <button type="button" className="btn btn-success h-10 px-3" onClick={() => activateMutation.mutate(item)}><Check className="h-4 w-4" /> Reaktivieren</button>
                      )}
                    </div>
                  </td>
                </tr>
              ))}
            </tbody>
          </table>
        </div>
      </section>
      <footer className="flex items-center justify-between text-sm text-text-light">
        <span>{users.data?.total ?? 0} Datensätze</span>
        <div className="flex items-center gap-3">
          <button type="button" className="btn btn-secondary" disabled={page <= 1} onClick={() => setPage((v) => v - 1)}>Zurück</button>
          <span>Seite {page}</span>
          <button type="button" className="btn btn-secondary" disabled={(users.data?.items.length ?? 0) < 10} onClick={() => setPage((v) => v + 1)}>Weiter</button>
        </div>
      </footer>
      {temporaryPassword && <div className="fixed right-4 top-4 z-50 rounded-lg border border-success/30 bg-white p-4 shadow-soft"><p className="text-sm text-text-light">Temporäres Passwort</p><p className="mt-1 font-mono text-sm">{temporaryPassword}</p></div>}
      {message && <div className="fixed right-4 top-4 z-50 rounded-lg border border-success/30 bg-white p-4 text-sm text-success shadow-soft">{message}</div>}
      {error && <div className="fixed right-4 top-4 z-50 rounded-lg border border-danger/30 bg-white p-4 text-sm text-danger shadow-soft">{error}</div>}
      <form onSubmit={form.handleSubmit((values) => saveMutation.mutate(values))} className="rounded-lg border border-border bg-surface p-5 shadow-soft">
        <h2 className="text-xl font-semibold">{editing ? "Benutzer bearbeiten" : "Benutzer anlegen"}</h2>
        <div className="mt-4 grid gap-4 md:grid-cols-2">
          {(["first_name", "last_name", "email", "temporary_password"] as const).map((field) => (
            <label key={field} className="block">
              <span className="text-sm font-medium">{field === "first_name" ? "Vorname" : field === "last_name" ? "Nachname" : field === "email" ? "E-Mail" : "Temporäres Passwort"}</span>
              <input type={field === "email" ? "email" : "text"} {...form.register(field)} className="mt-2 h-12 w-full rounded-lg border border-border px-4" />
            </label>
          ))}
          <label className="block">
            <span className="text-sm font-medium">Rolle</span>
            <select {...form.register("role")} className="mt-2 h-12 w-full rounded-lg border border-border px-4">
              {Object.entries(roleLabels).map(([value, label]) => <option key={value} value={value}>{label}</option>)}
            </select>
          </label>
          <label className="flex items-center gap-3 pt-7"><input type="checkbox" {...form.register("is_active")} /> Aktiv</label>
          <label className="flex items-center gap-3 pt-7"><input type="checkbox" {...form.register("must_change_password")} /> Passwortwechsel erzwingen</label>
        </div>
        <div className="mt-5 flex justify-end gap-3">
          <button type="button" className="btn btn-secondary" onClick={() => form.reset()}>Zurücksetzen</button>
          <button type="submit" className="btn btn-primary">{saveMutation.isPending ? <span className="spinner" /> : null} Speichern</button>
        </div>
      </form>
    </div>
  );
}
