"use server";

import { redirect } from "next/navigation";
import { isRedirectError } from "next/dist/client/components/redirect-error";
import { z } from "zod";

import { getSiteSettings } from "@/lib/app-config";
import { getLocalizedPath, resolveLocale } from "@/lib/locale";
import { sendContactMessage } from "@/lib/mail";

const contactFormSchema = z.object({
  locale: z.string().trim().min(1),
  name: z.string().trim().min(2).max(120),
  email: z.string().trim().email(),
  phone: z.string().trim().max(40).optional(),
  company: z.string().trim().max(120).optional(),
  message: z.string().trim().min(10).max(5000),
});

const contactErrorMessages = {
  ar: "تعذّر إرسال الرسالة. تأكد من تعبئة الحقول بشكل صحيح وحاول مجدداً.",
  en: "Your message could not be sent. Please check the fields and try again.",
  de: "Nachricht konnte nicht gesendet werden. Bitte Eingaben pruefen und erneut versuchen.",
} as const;

function getStringValue(formData: FormData, key: string) {
  const value = formData.get(key);
  return typeof value === "string" ? value : "";
}

function withContactError(pathname: string, message: string) {
  const params = new URLSearchParams();
  params.set("error", message);

  return `${pathname}?${params.toString()}`;
}

export async function submitContactFormAction(formData: FormData) {
  const siteSettings = await getSiteSettings();
  const locale = resolveLocale(String(formData.get("locale") ?? ""), siteSettings.defaultLocale);
  const contactPath = getLocalizedPath(locale, "/contact", siteSettings.defaultLocale);

  try {
    const values = contactFormSchema.parse({
      locale,
      name: getStringValue(formData, "name"),
      email: getStringValue(formData, "email"),
      phone: getStringValue(formData, "phone"),
      company: getStringValue(formData, "company"),
      message: getStringValue(formData, "message"),
    });

    await sendContactMessage({
      locale,
      name: values.name,
      email: values.email,
      phone: values.phone,
      company: values.company,
      message: values.message,
    });

    redirect(getLocalizedPath(locale, "/success", siteSettings.defaultLocale));
  } catch (error) {
    if (isRedirectError(error)) {
      throw error;
    }

    console.error("Contact form delivery failed.", error);
    redirect(withContactError(contactPath, contactErrorMessages[locale]));
  }
}
