"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,
	FormItem,
	FormLabel,
	FormField,
	FormControl,
	FormDescription,
	FormMessage
} from "@/components/ui/form"
import {
	Select,
	SelectContent,
	SelectItem,
	SelectTrigger,
	SelectValue,
} from "@/components/ui/select"
import { ItemText, SelectItemIndicator, SelectItemText } from "@radix-ui/react-select"

const formSchema = z.object({
	storyId: z.coerce.number(),
	pubId: z.number(),
	submitted: z.date(),
	responded: z.date(),
	responseId: z.number()
})


export default function SubmissionForm({ stories, pubs, responses }) {

	const storiesSelectItems = stories.map(e => (
		<SelectItem value={e.id} textValue={e.title}>
			{e.title}
		</SelectItem>
	))
	const pubsSelectItems = pubs.map(e => (
		<SelectItem value={e.id}>
			{e.title}
		</SelectItem>
	))

	const reponsesSelectItems = responses.map(e => (
		<SelectItem value={e.id}>
			{e.response}
		</SelectItem>
	))

	// 1. Define your form.
	const form = useForm<z.infer<typeof formSchema>>({
		resolver: zodResolver(formSchema),
		defaultValues: {
			storyId: stories[0].id,
			pubId: pubs[0].id,
			submitted: new Date(),
			responded: null,
			responseId: responses[0].id
		},
	})
	// 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.
		console.log(values)
	}
	return (
		<Form {...form}>
			<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-8">
				<FormField
					control={form.control}
					name="storyId"
					render={({ field }) => (
						<FormItem>
							<FormLabel>Story</FormLabel>
							<Select name="storyId" onValueChange={field.onChange}>
								<FormControl>
									<SelectTrigger>
										<SelectValue placeholder="Select the story you have submitted" />
									</SelectTrigger>
								</FormControl>
								<SelectContent>
									{storiesSelectItems}
								</SelectContent>
							</Select>
							<FormDescription>This is the story you submitted, yeah?</FormDescription>
							<FormMessage />
						</FormItem>
					)}
				/>


				<Button type="submit">Submit</Button>
			</form>
		</Form>
	)
}
