import prisma from "app/lib/db";
import { columns } from "app/submission/columns";
import GenreBadges from "app/ui/genreBadges";
import { PageHeader, PageSubHeader } from "app/ui/pageHeader";
import { DataTable } from "app/ui/tables/data-table";

async function getPubWithGenres(id: string) {
  return prisma.pub.findFirst({
    where: { id: Number(id) },
    include: { genres: true }
  })
}

async function getPubSubmissions(id: string) {
  return prisma.sub.findMany({
    where: { storyId: Number(id) },
    include: {
      story: true,
      pub: true,
      response: true
    }
  })
}


export default async function Page({ params }: { params: { id: string } }) {
  const pub = await getPubWithGenres(params.id)
  const pubSubs = await getPubSubmissions(params.id)
  return (
    <div className="container">
      <PageHeader>{pub.title}</PageHeader>
      <GenreBadges genres={pub.genres} className="my-6" />
      <PageSubHeader>Submissions:</PageSubHeader>
      <DataTable columns={columns} data={pubSubs} tableName="sub" />
    </div>
  )
}
