"use client"

import { z } from "zod"
import { zodResolver } from "@hookform/resolvers/zod"
import { useForm } from "react-hook-form"
import { Button } from "@/components/ui/button"
import {
	Form,
	FormControl,
	FormDescription,
	FormField,
	FormItem,
	FormLabel,
	FormMessage,
} from "@/components/ui/form"
import { Input } from "@/components/ui/input"
import { toast } from "@/components/ui/use-toast"

import {
	Popover,
	PopoverContent,
} from "@/components/ui/popover"
import GenresTrigger from "./genresTrigger"
import GenreCheckbox from "./genreCheckbox"
import { ComponentProps } from "react"
import { Genre } from "@prisma/client"
import { randomStoryTitle } from "app/lib/shortStoryTitleGenerator"
import { usePathname } from "next/navigation"

const formSchema = z.object({
	title: z.string().min(2).max(50),
	word_count: z.coerce.number().min(100),
	genres: z.array(z.number())
})

export default function StoryForm({ genres, createStory, className }: ComponentProps<"div"> & { genres: Array<Genre>, createStory: (data: any) => void }) {
	// 1. Define your form.
	const form = useForm<z.infer<typeof formSchema>>({
		resolver: zodResolver(formSchema),
		defaultValues: {
			title: "",
			word_count: 0,
			genres: []
		},
	})
	// 2. Define a submit handler.
	function onSubmit(values: z.infer<typeof formSchema>) {
		// Do something with the form values.
		// ✅ This will be type-safe and validated.
		toast({
			title: "You submitted the following values:",
			description: (
				<pre className="mt-2 w-[340px] rounded-md bg-slate-950 p-4">
					<code className="text-white">{JSON.stringify(values, null, 2)}</code>
				</pre>
			),
		})
		createStory(values)
		console.log(values)
	}



	function onErrors(errors) {
		toast({
			title: "You have errors",
			description: (
				<pre className="mt-2 w-[340px] rounded-md bg-slate-950 p-4">
					<code className="text-white">{JSON.stringify(errors, null, 2)}</code>
				</pre>
			),
		})
		console.log(JSON.stringify(errors))
	}

	const pathname = usePathname()

	return (
		<div className={className}>
			<Form {...form}>
				<form onSubmit={form.handleSubmit(onSubmit, onErrors)} className="space-y-8">
					<FormField
						control={form.control}
						name="title"
						render={({ field }) => (
							<FormItem>
								<FormLabel>Title</FormLabel>
								<FormControl>
									<Input placeholder={randomStoryTitle()} {...field} />
								</FormControl>
								<FormMessage />
							</FormItem>
						)}
					/>

					<div className="inline-flex flex-wrap w-full gap-x-16 gap-y-8 items-baseline max-w-full">


						<FormField
							control={form.control}
							name="genres"
							render={({ field }) => (
								<FormItem className="flex flex-col">
									<FormLabel className="h-5">Genres</FormLabel>
									<Popover>
										<GenresTrigger value={field.value} genres={genres} />
										<PopoverContent align="start">
											{genres.map((item) => (
												<FormField

													key={item.id}
													control={form.control}
													name="genres"
													render={({ field }) => {
														return (
															<GenreCheckbox field={field} item={item} />
														)
													}}
												/>
											))}
											<Button variant="link" className="p-0" onClick={() => form.setValue("genres", [])}>Clear</Button>
										</PopoverContent>
									</Popover>
									<FormMessage />
								</FormItem>
							)}
						/>

						<FormField
							control={form.control}
							name="word_count"
							render={({ field }) => (
								<FormItem className="flex flex-col">
									<FormLabel className="h-5">Word count</FormLabel>
									<FormControl>
										<Input className=" w-24" type="number" step={500} min={0} {...field}></Input>
									</FormControl>
									<FormMessage />
								</FormItem>
							)}
						/>

					</div>

					<Button type="submit" className="">Submit</Button>


				</form>
			</Form>
		</div>
	)
}
