"use client";

import { zodResolver } from "@hookform/resolvers/zod";
import { useForm } from "react-hook-form";
import { z } from "zod";

import { Button } from "@/components/ui/button";
import {
  Form,
  FormControl,
  FormField,
  FormItem,
  FormLabel,
  FormMessage,
} from "@/components/ui/form";
import { toast } from "@/components/ui/use-toast";
import { Input } from "./ui/input";
import { Skill } from "@/types/skill";

const displayFormSchema = z.object({
  name: z.string().trim().min(1, "Name is required."),
});

type DisplayFormValues = z.infer<typeof displayFormSchema>;

// const defaultValues: Partial<DisplayFormValues> = {
//   name: "",
// };

type Props = {
  skill?: Skill;
  onSubmit: () => void;
  btn1_content: string;
  btn2_content: string;
};

export default function SkillForm({
  skill,
  onSubmit: onFormSubmit,
  btn1_content,
  btn2_content,
}: Props) {
  const form = useForm<DisplayFormValues>({
    resolver: zodResolver(displayFormSchema),
    defaultValues: {
      name: skill?.name || "",
    },
  });

  function onSubmit(data: DisplayFormValues) {
    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(data, null, 2)}</code>
        </pre>
      ),
    });
  }

  return (
    <Form {...form}>
      <form onSubmit={form.handleSubmit(onSubmit)} className="space-y-8">
        <FormField
          control={form.control}
          name="name"
          render={({ field }) => (
            <FormItem>
              <FormLabel>Name</FormLabel>
              <FormControl>
                <Input placeholder="Enter Name..." {...field} />
              </FormControl>
              <FormMessage />
            </FormItem>
          )}
        />

        <div className="flex items-center gap-4 flex-col sm:flex-row">
          <Button className="w-full" size="lg">
            {btn1_content}
          </Button>
          <Button variant="secondary" className="w-full" size="lg">
            {btn2_content}
          </Button>
        </div>
      </form>
    </Form>
  );
}
