"use client";

import { Check } from "lucide-react";
import { useTranslations } from "next-intl";
import { useState } from "react";

interface FormData {
  firstName: string;
  lastName: string;
  email: string;
  subject: string;
  message: string;
  agreeToPolicy: boolean;
}

interface FormErrors {
  firstName?: string;
  lastName?: string;
  email?: string;
  subject?: string;
  message?: string;
  agreeToPolicy?: string;
}

export default function Form() {
  const t = useTranslations();
  const [formData, setFormData] = useState<FormData>({
    firstName: "",
    lastName: "",
    email: "",
    subject: "",
    message: "",
    agreeToPolicy: false,
  });
  const [errors, setErrors] = useState<FormErrors>({});
  const [isSubmitting, setIsSubmitting] = useState(false);
  const [submitStatus, setSubmitStatus] = useState<
    "idle" | "success" | "error"
  >("idle");

  const validateForm = (): boolean => {
    const newErrors: FormErrors = {};

    if (!formData.firstName.trim()) {
      newErrors.firstName = "First name is required";
    }
    if (!formData.lastName.trim()) {
      newErrors.lastName = "Last name is required";
    }
    if (!formData.email.trim()) {
      newErrors.email = "Email is required";
    } else if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(formData.email)) {
      newErrors.email = "Please enter a valid email";
    }
    if (!formData.subject.trim()) {
      newErrors.subject = "Subject is required";
    }
    if (!formData.message.trim()) {
      newErrors.message = "Message is required";
    }
    if (!formData.agreeToPolicy) {
      newErrors.agreeToPolicy = "You must agree to the privacy policy";
    }

    setErrors(newErrors);
    return Object.keys(newErrors).length === 0;
  };

  const handleChange = (
    e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>,
  ) => {
    const { name, value } = e.target;
    setFormData((prev) => ({ ...prev, [name]: value }));
    if (errors[name as keyof FormErrors]) {
      setErrors((prev) => ({ ...prev, [name]: undefined }));
    }
  };

  const handleCheckboxChange = () => {
    setFormData((prev) => ({ ...prev, agreeToPolicy: !prev.agreeToPolicy }));
    if (errors.agreeToPolicy) {
      setErrors((prev) => ({ ...prev, agreeToPolicy: undefined }));
    }
  };

  const handleSubmit = async (e: React.FormEvent) => {
    e.preventDefault();

    if (!validateForm()) return;

    setIsSubmitting(true);
    setSubmitStatus("idle");

    try {
      const response = await fetch("/api/contact", {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify(formData),
      });

      if (response.ok) {
        setSubmitStatus("success");
        setFormData({
          firstName: "",
          lastName: "",
          email: "",
          subject: "",
          message: "",
          agreeToPolicy: false,
        });
      } else {
        setSubmitStatus("error");
      }
    } catch {
      setSubmitStatus("error");
    } finally {
      setIsSubmitting(false);
    }
  };

  return (
    <form onSubmit={handleSubmit} className="space-y-4">
      {/* First Row - Names */}
      <div className="grid grid-cols-1 gap-4 md:grid-cols-2">
        <div>
          <input
            type="text"
            name="firstName"
            placeholder={t("contact.form.placeholders.firstName")}
            value={formData.firstName}
            onChange={handleChange}
            className={`w-full rounded-3xl border bg-white px-4 py-3 text-sm text-gray-700 placeholder-gray-400 outline-none transition focus:ring-2 focus:ring-red-500 ${
              errors.firstName ? "border-red-500" : "border-transparent"
            }`}
          />
          {errors.firstName && (
            <p className="mt-1 text-xs text-red-500">{errors.firstName}</p>
          )}
        </div>
        <div>
          <input
            type="text"
            name="lastName"
            placeholder={t("contact.form.placeholders.lastName")}
            value={formData.lastName}
            onChange={handleChange}
            className={`w-full rounded-3xl border bg-white px-4 py-3 text-sm text-gray-700 placeholder-gray-400 outline-none transition focus:ring-2 focus:ring-red-500 ${
              errors.lastName ? "border-red-500" : "border-transparent"
            }`}
          />
          {errors.lastName && (
            <p className="mt-1 text-xs text-red-500">{errors.lastName}</p>
          )}
        </div>
      </div>

      {/* Second Row - Email & Subject */}
      <div className="grid grid-cols-1 gap-4 md:grid-cols-2">
        <div>
          <input
            type="email"
            name="email"
            placeholder={t("contact.form.placeholders.email")}
            value={formData.email}
            onChange={handleChange}
            className={`w-full rounded-3xl border bg-white px-4 py-3 text-sm text-gray-700 placeholder-gray-400 outline-none transition focus:ring-2 focus:ring-red-500 ${
              errors.email ? "border-red-500" : "border-transparent"
            }`}
          />
          {errors.email && (
            <p className="mt-1 text-xs text-red-500">{errors.email}</p>
          )}
        </div>
        <div>
          <input
            type="text"
            name="subject"
            placeholder={t("contact.form.placeholders.subject")}
            value={formData.subject}
            onChange={handleChange}
            className={`w-full rounded-3xl border bg-white px-4 py-3 text-sm text-gray-700 placeholder-gray-400 outline-none transition focus:ring-2 focus:ring-red-500 ${
              errors.subject ? "border-red-500" : "border-transparent"
            }`}
          />
          {errors.subject && (
            <p className="mt-1 text-xs text-red-500">{errors.subject}</p>
          )}
        </div>
      </div>

      {/* Message Textarea */}
      <div>
        <textarea
          name="message"
          placeholder={t("contact.form.placeholders.message")}
          rows={8}
          value={formData.message}
          onChange={handleChange}
          className={`w-full resize-none rounded-md border bg-white px-4 py-3 text-sm text-gray-700 placeholder-gray-400 outline-none transition focus:ring-2 focus:ring-red-500 ${
            errors.message ? "border-red-500" : "border-transparent"
          }`}
        />
        {errors.message && (
          <p className="mt-1 text-xs text-red-500">{errors.message}</p>
        )}
      </div>

      {/* Checkbox and Submit */}
      <div className="flex flex-col gap-4 sm:flex-row sm:items-center sm:justify-between">
        <div className="flex items-center gap-3">
          <button
            type="button"
            onClick={handleCheckboxChange}
            className={` flex h-5 w-5 items-center justify-center rounded border-2 transition ${
              formData.agreeToPolicy
                ? "border-red-600 bg-red-600"
                : "border-gray-400 bg-transparent"
            }`}
            aria-label="Agree to privacy policy"
          >
            {formData.agreeToPolicy && (
              <Check className="h-3 w-3 text-white" strokeWidth={3} />
            )}
          </button>
          <span className="text-sm text-gray-300">
            {t("contact.form.privacy")}
          </span>
        </div>
        <button
          type="submit"
          disabled={isSubmitting}
          className="shadow-[0px_0px_2px_8px_#ff01015c] rounded-full bg-red-600 px-8 py-3 text-sm font-semibold uppercase tracking-wider text-white transition hover:cursor-pointer hover:scale-90 disabled:cursor-not-allowed disabled:opacity-50"
        >
          {isSubmitting ? "Sending..." : t("contact.form.send")}
        </button>
      </div>
      {errors.agreeToPolicy && (
        <p className="text-xs text-red-500">{errors.agreeToPolicy}</p>
      )}

      {/* Status Messages */}
      {submitStatus === "success" && (
        <p className="text-center text-sm text-green-400">
          Message sent successfully!
        </p>
      )}
      {submitStatus === "error" && (
        <p className="text-center text-sm text-red-400">
          Failed to send message. Please try again.
        </p>
      )}
    </form>
  );
}
