"use server";

import { prisma } from "@/app/lib/db";
import {
  ContactSchema,
  AdmissionSchema,
  type ActionState,
} from "@/app/lib/definitions";

function honeypotFilled(formData: FormData) {
  const website = String(formData.get("website") ?? "");
  return website.length > 0;
}

export async function submitContact(
  _prevState: ActionState | undefined,
  formData: FormData,
): Promise<ActionState> {
  if (honeypotFilled(formData)) {
    return { success: true, message: "Message sent successfully." };
  }

  const validated = ContactSchema.safeParse({
    name: formData.get("name"),
    email: formData.get("email"),
    phone: formData.get("phone") ?? "",
    subject: formData.get("subject"),
    message: formData.get("message"),
    website: formData.get("website") ?? "",
  });

  if (!validated.success) {
    return {
      errors: validated.error.flatten().fieldErrors,
      message: "Please fix the highlighted fields.",
    };
  }

  try {
    await prisma.contactSubmission.create({
      data: {
        name: validated.data.name,
        email: validated.data.email,
        phone: validated.data.phone || "",
        subject: validated.data.subject,
        message: validated.data.message,
        status: "new",
      },
    });
  } catch {
    return { message: "Unable to send your message right now. Please try again." };
  }

  return { success: true, message: "Message sent successfully." };
}

export async function submitAdmission(
  _prevState: ActionState | undefined,
  formData: FormData,
): Promise<ActionState> {
  if (honeypotFilled(formData)) {
    return { success: true, message: "Application received." };
  }

  const consentRaw = formData.get("consent");
  const consent = consentRaw === "on" || consentRaw === "true";

  const validated = AdmissionSchema.safeParse({
    studentFirstName: formData.get("studentFirstName"),
    studentLastName: formData.get("studentLastName"),
    studentDob: formData.get("studentDob"),
    studentGender: formData.get("studentGender") ?? "",
    currentSchool: formData.get("currentSchool") ?? "",
    gradeLevel: formData.get("gradeLevel"),
    program: formData.get("program"),
    startDate: formData.get("startDate"),
    parentName: formData.get("parentName"),
    relationship: formData.get("relationship"),
    email: formData.get("email"),
    phone: formData.get("phone"),
    notes: formData.get("notes") ?? "",
    consent,
    website: formData.get("website") ?? "",
  });

  if (!validated.success) {
    return {
      errors: validated.error.flatten().fieldErrors,
      message: "Please fix the highlighted fields.",
    };
  }

  const data = validated.data;

  try {
    await prisma.admissionSubmission.create({
      data: {
        studentFirstName: data.studentFirstName,
        studentLastName: data.studentLastName,
        studentDob: data.studentDob,
        studentGender: data.studentGender || "",
        currentSchool: data.currentSchool || "",
        gradeLevel: data.gradeLevel,
        program: data.program,
        startDate: data.startDate,
        parentName: data.parentName,
        relationship: data.relationship,
        email: data.email,
        phone: data.phone,
        notes: data.notes || "",
        consent: data.consent,
        status: "new",
      },
    });
  } catch {
    return {
      message: "Unable to submit your application right now. Please try again.",
    };
  }

  return { success: true, message: "Application received." };
}
