"use client";

import { useTranslations } from "../i18n/utils";
import { Button } from "./ui/button";
import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogHeader,
  DialogTitle,
  DialogTrigger,
} from "./ui/dialog";
import {
  Form,
  FormControl,
  FormDescription,
  FormField,
  FormItem,
  FormLabel,
  FormMessage,
} from "./ui/form";
import { Input } from "./ui/input";
import { z } from "astro/zod";
import { zodResolver } from "@hookform/resolvers/zod";
import { useForm } from "react-hook-form";
import { Textarea } from "./ui/textarea"


const formSchema = z.object({
  username: z.string().min(2),
  email: z.string().email(),
  message: z.string(),
});

export function ContactForm({ currentLocale }) {
  const t = useTranslations(currentLocale);

  const form = useForm<z.infer<typeof formSchema>>({
    resolver: zodResolver(formSchema),
    defaultValues: {
      username: "",
      email: "",
      message: "",
      /*datetime: "2020-01-01T00:00:00Z",*/
    },
  });

  async function onSubmit(values: z.infer<typeof formSchema>) {
    const formData = new FormData();
    
    for (const value of Object.keys(values)) {
      formData.append(value, values[value])
    }
    console.log(formData)

    const response = await fetch("/api", {
        method: "POST",
        body: formData
    });

    const data = await response.json();
    console.log(data.message)
  }

  return (
    <Dialog>
      <DialogTrigger asChild>
        <button
          type="button"
          className="text-2xl m-2 bg-secondary/60 rounded-md p-1 hover:bg-secondary text-content"
        >
          {t("hero").buttons[1]}  
        </button>
      </DialogTrigger>
      <DialogContent className="sm:max-w-[425px]">
        <DialogHeader>
          <DialogTitle><p className="text-foreground">{t("contact").title}</p></DialogTitle>
          <DialogDescription>{t("contact").description}</DialogDescription>
        </DialogHeader>

        <Form {...form}>
          <form onSubmit={form.handleSubmit(onSubmit)}>
            <FormField
              control={form.control}
              name="username"
              render={({ field }) => (
                <FormItem>
                  <FormLabel>{t("contact").name}</FormLabel>
                  <FormControl>
                    <Input
                      placeholder={t("contact").nameDefaultValue}
                      className="col-span-3 text-foreground"
                      {...field}
                    />
                  </FormControl>
                  <FormDescription>
                    {t("contact").nameDescription}
                  </FormDescription>
                  <FormMessage />
                </FormItem>
              )}
            />

            <FormField
              control={form.control}
              name="email"
              render={({ field }) => (
                <FormItem>
                  <FormLabel>{t("contact").email}</FormLabel>
                  <FormControl>
                    <Input
                      placeholder={t("contact").emailDefaultValue}
                      className="col-span-3 text-foreground"
                      type="email"
                      {...field}
                    />
                  </FormControl>
                  <FormDescription>
                    {t("contact").emailDescription}
                  </FormDescription>
                  <FormMessage />
                </FormItem>
              )}
            />

            <FormField
              control={form.control}
              name="message"
              render={({ field }) => (
                <FormItem>
                  <FormLabel>{t("contact").message}</FormLabel>
                  <FormControl>
                    <Textarea
                      placeholder={t("contact").messageDefaultValue}
                      className="col-span-3 text-foreground"
                      {...field}
                    />
                  </FormControl>
                  <FormDescription>
                    {t("contact").messageDescription}
                  </FormDescription>
                  <FormMessage />
                </FormItem>
              )}
            />

            <DialogFooter>
              <Button type="submit">{t("contact").button}</Button>
            </DialogFooter>
          </form>
        </Form>
      </DialogContent>
    </Dialog>
  );
}
