"use client"

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

const schema = z.object({
  firstName: z.string().min(1, "First name is required"),
  lastName: z.string().min(1, "Last name is required"),
  email: z.string().email("Please enter a valid email"),
  country: z.string().min(1, "Country is required"),
  phone: z.string().min(1, "Phone number is required"),
  company: z.string().min(1, "Company is required"),
  companySize: z.string().min(1, "Please select company size"),
  message: z.string().min(10, "Message must be at least 10 characters"),
})

type FormData = z.infer<typeof schema>

export default function ContactForm() {
  const [status, setStatus] = useState<"idle" | "success" | "error">("idle")

  const {
    register,
    handleSubmit,
    formState: { errors, isSubmitting },
    reset,
  } = useForm<FormData>({
    resolver: zodResolver(schema),
  })

  const onSubmit = async (data: FormData) => {
    try {
      await new Promise((resolve) => setTimeout(resolve, 1000))
      console.log("Contact form:", data)
      setStatus("success")
      reset()
    } catch {
      setStatus("error")
    }
  }

  if (status === "success") {
    return (
      <div className="contact-form-block w-form">
        <div className="success-message-wrapper w-form-done" style={{ display: "block" }}>
          <div className="contact-success-message">
            <svg xmlns="http://www.w3.org/2000/svg" width="100%" viewBox="0 0 20 20" fill="none" className="contact-success-icon">
              <path d="M7.20658 10.9311L9.24116 12.1209L12.7928 7.20696M1 10C1 5.02944 5.02944 1 10 1C14.9706 1 19 5.02944 19 10C19 14.9706 14.9706 19 10 19C5.02944 19 1 14.9706 1 10Z" stroke="currentColor" strokeWidth="1.5" strokeLinecap="square" />
            </svg>
            <h2 className="display-5">Thank you! We&apos;ll get back to you soon</h2>
            <div className="mg-top-4x-extra-small">
              <p>We have received your message and will get back to you as soon as possible.</p>
            </div>
          </div>
        </div>
      </div>
    )
  }

  return (
    <div className="contact-form-block w-form">
      <form onSubmit={handleSubmit(onSubmit)} className="grid-2-columns contact-form-grid">
        <input className={`input contact-form w-input`} maxLength={256} placeholder="First name" type="text" {...register("firstName")} />
        <input className={`input contact-form w-input`} maxLength={256} placeholder="Last name" type="text" {...register("lastName")} />
        <input className={`input contact-form w-input`} maxLength={256} placeholder="Email address" type="email" {...register("email")} />
        <input className={`input contact-form w-input`} maxLength={256} placeholder="Country" type="text" {...register("country")} />
        <input className={`input contact-form w-input`} maxLength={256} placeholder="(123) 456 - 7890" type="tel" {...register("phone")} />
        <input className={`input contact-form w-input`} maxLength={256} placeholder="Company" type="text" {...register("company")} />
        <div className="select-wrapper">
          <select className="input select-form w-select" {...register("companySize")}>
            <option value="">Company size</option>
            <option value="1-20 Employees">1-20 Employees</option>
            <option value="20-100 Employees">20-100 Employees</option>
            <option value="100+ Employees">100+ Employees</option>
          </select>
          <div className="select-icon-wrapper">
            <svg xmlns="http://www.w3.org/2000/svg" width="100%" viewBox="0 0 20 20" fill="none" className="select-icon">
              <path d="M15.5 7.03847L10 12.9615L4.5 7.03847" stroke="currentColor" strokeWidth="1.5" strokeLinecap="square" />
            </svg>
          </div>
        </div>
        <textarea maxLength={5000} placeholder="Enter your message" className="text-area contact-form w-input" {...register("message")} />
        <div className="contact-form-button-wrapper">
          <button type="submit" className="form-button w-button" disabled={isSubmitting}>
            {isSubmitting ? "Please wait..." : "Send message"}
          </button>
        </div>
      </form>
      {Object.keys(errors).length > 0 && (
        <div className="error-message contact-form-error w-form-fail" style={{ display: "block" }}>
          <div>Please fill in all required fields correctly.</div>
        </div>
      )}
      {status === "error" && (
        <div className="error-message contact-form-error w-form-fail" style={{ display: "block" }}>
          <div>Oops! Something went wrong. Please try again.</div>
        </div>
      )}
    </div>
  )
}
