import { redirect } from "next/navigation";

import { getSession } from "@/lib/auth";
import { prisma } from "@/lib/prisma";
import { ShellPage } from "@/components/page-templates";
import { TablePlaceholder } from "@/components/placeholders";

export default async function TagsSettingsPage() {
  const session = await getSession();
  if (!session) {
    redirect("/login");
  }

  if (session.role === "agent") {
    redirect("/unauthorized");
  }

  const tags = await prisma.tag.findMany({
    where: { tenantId: session.tenantId },
    include: {
      _count: {
        select: {
          conversationTags: true,
          contactTags: true
        }
      }
    },
    orderBy: { name: "asc" }
  });

  const rows = tags.map((tag) => [
    tag.name,
    tag.color ?? "-",
    String(tag._count.conversationTags + tag._count.contactTags)
  ]);

  return (
    <ShellPage shell="admin" title="Chat Tags Management" description="Daftar tag conversation dan contact.">
      <TablePlaceholder
        title="Tags"
        columns={["Tag", "Color", "Usage"]}
        rows={rows}
      />
    </ShellPage>
  );
}
