"use client";

import React from "react";
import Heading from "./heading";
import { z } from "zod";
import { zodResolver } from "@hookform/resolvers/zod";
import { useForm } from "react-hook-form";
import { Button } from "@/ui/button";
import { Textarea } from "@/ui/textarea";
import {
  Form,
  FormControl,
  FormField,
  FormItem,
  FormLabel,
  FormMessage,
} from "@/ui/form";
import { Input } from "@/ui/input";
import {
  Select,
  SelectContent,
  SelectItem,
  SelectTrigger,
  SelectValue,
} from "@/components/ui/select";
import { cn } from "@/lib/utils";
import { useTranslations } from "next-intl";
import { sendContactMail } from "@/lib/server-actions";
import { contactFormSchema } from "@/lib/zod";

function ContactForm() {
  // 1. Define your form.
  const form = useForm<z.infer<typeof contactFormSchema>>({
    resolver: zodResolver(contactFormSchema),
  });

  // 2. Define a submit handler.
  async function onSubmit(values: z.infer<typeof contactFormSchema>) {
    const result = await sendContactMail(values);
    console.log(result);
  }
  const t = useTranslations();

  return (
    <Form {...form}>
      <form onSubmit={form.handleSubmit(onSubmit)} className="space-y-8">
        <div className="flex flex-col md:flex-row gap-4 w-full">
          <FormField
            control={form.control}
            name="name"
            render={({ field }) => (
              <FormItem className="w-full ">
                <FormLabel>{t("contact.form.name.label")}</FormLabel>
                <FormControl>
                  <Input
                    placeholder={t("contact.form.name.placeholder")}
                    {...field}
                  />
                </FormControl>
                <FormMessage />
              </FormItem>
            )}
          />
          <FormField
            control={form.control}
            name="email"
            render={({ field }) => (
              <FormItem className="w-full">
                <FormLabel>{t("contact.form.email.label")}</FormLabel>
                <FormControl>
                  <Input
                    placeholder={t("contact.form.email.placeholder")}
                    {...field}
                  />
                </FormControl>
                <FormMessage />
              </FormItem>
            )}
          />
        </div>
        <FormField
          control={form.control}
          name="budget"
          render={({ field }) => (
            <FormItem>
              <FormLabel>{t("contact.form.budget.label")}</FormLabel>
              <FormControl>
                <Select
                  onValueChange={field.onChange}
                  defaultValue={field.value}
                >
                  <SelectTrigger
                    className={cn(
                      "w-full",
                      field.value?.length
                        ? "text-foreground"
                        : "text-muted-foreground"
                    )}
                  >
                    <SelectValue
                      placeholder={t("contact.form.budget.placeholder")}
                    />
                  </SelectTrigger>
                  <SelectContent>
                    <SelectItem value="< €2k">
                      {t("contact.form.budget.options.1")}
                    </SelectItem>
                    <SelectItem value="> €4k">
                      {t("contact.form.budget.options.2")}
                    </SelectItem>
                    <SelectItem value="> €6k">
                      {t("contact.form.budget.options.3")}
                    </SelectItem>
                  </SelectContent>
                </Select>
              </FormControl>
              <FormMessage />
            </FormItem>
          )}
        />
        <FormField
          control={form.control}
          name="message"
          render={({ field }) => (
            <FormItem>
              <FormLabel> {t("contact.form.message.label")}</FormLabel>
              <FormControl>
                <Textarea
                  rows={4}
                  placeholder={t("contact.form.message.placeholder")}
                  {...field}
                />
              </FormControl>
              <FormMessage />
            </FormItem>
          )}
        />
        <Button type="submit" className="w-full">
          {t("contact.form.submit")}
        </Button>
      </form>
    </Form>
  );
}

export default ContactForm;
