import Link from "next/link";
import { db } from "@/db";
import { vehicleTemplates } from "@/db/schema";
import { requirePlatformAdmin } from "@/lib/auth/guards";
import { de } from "@/lib/i18n/de";
import { allradCode } from "@/lib/admin/codes";
import { DataTable } from "@/components/admin/DataTable";
import { TemplateCreateForm } from "./TemplateCreateForm";

export const metadata = { title: "Fahrzeug-Vorlagen — Administration" };

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

  const rows = await db
    .select()
    .from(vehicleTemplates)
    .orderBy(vehicleTemplates.reihenfolge, vehicleTemplates.code);

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