import { auth } from "@/auth";
import { db } from "@/lib/db";
import { hasPermission } from "@/lib/permissions";
import { redirect } from "next/navigation";
import { TermsTable } from "./terms-table";
import type { Metadata } from "next";

export const metadata: Metadata = { title: "Terms & Conditions" };

export default async function TermsPage() {
  const session = await auth();
  if (!session?.user) redirect("/login");
  if (!hasPermission(session.user.role, "manage_terms")) redirect("/dashboard");

  const terms = await db.termsCondition.findMany({
    orderBy: [{ category: "asc" }, { isActive: "desc" }, { createdAt: "asc" }],
  });

  return (
    <TermsTable
      terms={terms.map((t) => ({
        id: t.id,
        category: t.category,
        text: t.text,
        isActive: t.isActive,
      }))}
    />
  );
}
