import Link from "next/link";
import { db } from "@/db";
import { equipmentCategories } from "@/db/schema";
import { requirePlatformAdmin } from "@/lib/auth/guards";
import { de } from "@/lib/i18n/de";
import { DataTable } from "@/components/admin/DataTable";
import { CategoryCreateForm } from "./CategoryCreateForm";

export const metadata = { title: "Geräte-Kategorien — Administration" };

export default async function KategorienPage() {
  await requirePlatformAdmin();

  const rows = await db
    .select()
    .from(equipmentCategories)
    .orderBy(equipmentCategories.reihenfolge, equipmentCategories.name);

  return (
    <div className="space-y-4">
      <div className="flex items-center justify-between">
        <h1 className="font-display text-2xl text-navy">
          {de.admin.navKategorien}
        </h1>
        <CategoryCreateForm />
      </div>
      <DataTable
        rows={rows}
        getRowKey={(r) => r.id}
        columns={[
          {
            key: "name",
            header: de.admin.name,
            render: (r) => (
              <span className="font-medium text-anthrazit">{r.name}</span>
            ),
          },
          {
            key: "aktion",
            header: "",
            render: (r) => (
              <Link
                href={`/admin/geraete-kategorien/${r.id}`}
                className="text-navy underline-offset-2 hover:underline"
              >
                {de.admin.bearbeiten}
              </Link>
            ),
          },
        ]}
      />
    </div>
  );
}
