'use server'

import { redirect } from 'next/navigation'
import { cms } from '@/lib/site'

const EMAIL = /^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/
const PHONE = /^[+\d][\d\s-]{6,19}$/
const clip = (v: FormDataEntryValue | null, n: number) => String(v ?? '').trim().slice(0, n)

/**
 * «سجّل اهتمامك»: validate, save to the Leads collection, and come back to the form with a message.
 * The address carries no #form anchor: a redirect with both a query and a hash arrives without the query,
 * and the page then has no way to know it should show the confirmation.
 */
export async function submitLead(form: FormData): Promise<void> {
  const locale = form.get('locale') === 'en' ? 'en' : 'ar'
  // the form lives on two pages now; it returns to the one it was sent from, never to an address it is handed
  const page = form.get('from') === 'contact' ? '/contact' : '/pricing'
  const base = locale === 'en' ? `/en${page}` : page
  const slug = clip(form.get('course'), 60)

  // a hidden field people never see; bots fill it
  if (clip(form.get('website'), 200)) redirect(`${base}?sent=1`)

  const name = clip(form.get('name'), 120)
  const email = clip(form.get('email'), 200).toLowerCase()
  const phone = clip(form.get('phone'), 20)
  if (name.length < 2 || !EMAIL.test(email) || (phone && !PHONE.test(phone))) {
    redirect(`${base}?error=1${slug ? `&course=${encodeURIComponent(slug)}` : ''}`)
  }

  const payload = await cms()
  const course = slug
    ? (await payload.find({ collection: 'courses', where: { slug: { equals: slug } }, limit: 1, depth: 0 })).docs[0]
    : undefined
  await payload.create({
    collection: 'leads',
    overrideAccess: true,
    data: {
      name,
      email,
      phone: phone || undefined,
      role: clip(form.get('role'), 80) || undefined,
      course: course?.id,
      courseSlug: slug || undefined,
      note: clip(form.get('note'), 2000) || undefined,
      locale,
      status: 'new',
    },
  })
  redirect(`${base}?sent=1`)
}
