import PubForm from "app/ui/forms/pub";
import { getGenres } from "app/lib/get";
import prisma from "app/lib/db";
import { CreateContainer, CreateContainerContent, CreateContainerDescription, CreateContainerHeader } from "app/ui/createContainer";
import { revalidatePath } from "next/cache";
import { redirect } from "next/navigation";
export default async function Page() {
	async function createPub(data) {
		"use server"
		const genresArray = data.genres.map(e => { return { id: e } })
		const res = await prisma.pub.create({
			data: {
				title: data.title,
				link: data.link,
				query_after_days: data.query_after_days
			}
		})
		console.log(res)
		const genresRes = await prisma.pub.update({
			where: { id: res.id },
			data:
				{ genres: { set: genresArray } }
		})
		console.log(genresRes)
		revalidatePath("/publication")
		redirect("/publication")
	}
	const genres = await getGenres()
	return (
		<CreateContainer>
			<CreateContainerHeader>Create Publication</CreateContainerHeader>
			<CreateContainerContent>
				<CreateContainerDescription>
					Create a new entry for a publication i.e. a place you intend to submit to.
				</CreateContainerDescription>
				<PubForm genres={genres} createPub={createPub} className="mt-6" />
			</CreateContainerContent>
		</CreateContainer>
	)
}
