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

export const metadata: Metadata = { title: "Project Codes" };

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

  const projectCodes = await db.projectCode.findMany({
    orderBy: [{ isActive: "desc" }, { code: "asc" }],
  });

  return (
    <ProjectCodesTable
      projectCodes={projectCodes.map((c) => ({
        id: c.id,
        code: c.code,
        isActive: c.isActive,
      }))}
    />
  );
}
