import Link from "next/link";
import { db } from "@/db";
import { brigades } from "@/db/schema";
import { requirePlatformAdmin } from "@/lib/auth/guards";
import { de } from "@/lib/i18n/de";
import { Button } from "@/components/ui/button";
import { DataTable } from "@/components/admin/DataTable";

export const metadata = { title: "Wehren — Administration" };

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

  const rows = await db.select().from(brigades).orderBy(brigades.name);

  return (
    <div className="space-y-4">
      <div className="flex items-center justify-between">
        <h1 className="font-display text-2xl text-navy">{de.admin.navWehren}</h1>
        <Button asChild>
          <Link href="/admin/wehren/neu">{de.admin.wehrAnlegen}</Link>
        </Button>
      </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: "ort",
            header: de.admin.ort,
            render: (r) => `${r.plz ?? ""} ${r.ort ?? ""}`.trim() || "–",
          },
          {
            key: "geo",
            header: "Geo",
            render: (r) =>
              r.lat != null && r.lng != null ? (
                <span className="text-bereit">✓</span>
              ) : (
                <span className="text-wartung">{de.admin.geocodeFehler}</span>
              ),
          },
          {
            key: "aktion",
            header: "",
            render: (r) => (
              <Link
                href={`/admin/wehren/${r.id}`}
                className="text-navy underline-offset-2 hover:underline"
              >
                {de.admin.bearbeiten}
              </Link>
            ),
          },
        ]}
      />
    </div>
  );
}
