

// 'use server';

// import { db } from '@/lib/db';
// import { registrations } from '@/lib/db/schema';
// import { registrationSchema, type RegistrationFormData } from '@/lib/validations/registration';

// export type ActionResult =
//   | { success: true; message: string }
//   | { success: false; error: string };

// export async function submitRegistration(data: RegistrationFormData): Promise<ActionResult> {
//   try {
//     const validated = registrationSchema.safeParse(data);
//     if (!validated.success) {
//       return {
//         success: false,
//         error: validated.error.errors[0]?.message || 'Invalid form data',
//       };
//     }

//     await db.insert(registrations).values({
//       fullName: validated.data.fullName,
//       email: validated.data.email,
//       phone: validated.data.phone,
//       gender: validated.data.gender,
//       dateOfBirth: validated.data.dateOfBirth,
//       state: validated.data.state,
//       educationLevel: validated.data.educationLevel,
//       institution: validated.data.institution,
//       stack: validated.data.stack,
//       motivation: validated.data.motivation,
//       experience: validated.data.experience || null,
//       heardFrom: validated.data.heardFrom,
//     });

//     return {
//       success: true,
//       message: "You've been successfully registered for Ignite-100!",
//     };
//   } catch (err: unknown) {
//     // postgres.js wraps the DB error inside a `cause` property
//     const code =
//       (err as { code?: string })?.code ||
//       (err as { cause?: { code?: string } })?.cause?.code;

//     if (code === '23505') {
//       return {
//         success: false,
//         error: 'This email address has already been registered.',
//       };
//     }
//     console.error('Registration error:', err);
//     return {
//       success: false,
//       error: 'Something went wrong. Please try again later.',
//     };
//   }
// }


'use server';

import { db } from '@/lib/db';
import { registrations } from '@/lib/db/schema';
import { registrationSchema, type RegistrationFormData } from '@/lib/validations/registration';

export type ActionResult =
  | { success: true; message: string }
  | { success: false; error: string };

export async function submitRegistration(data: RegistrationFormData): Promise<ActionResult> {
  try {
    const validated = registrationSchema.safeParse(data);
    if (!validated.success) {
      return {
        success: false,
        error: validated.error.errors[0]?.message || 'Invalid form data',
      };
    }

    await db.insert(registrations).values({
      fullName: validated.data.fullName,
      email: validated.data.email,
      phone: validated.data.phone,
      gender: validated.data.gender,
      ageRange: validated.data.ageRange,
      educationLevel: validated.data.educationLevel,
      institution: validated.data.institution,
      stack: validated.data.stack,
      motivation: validated.data.motivation,
      heardFrom: validated.data.heardFrom,
    });

    return {
      success: true,
      message: "You've been successfully registered for Ignite-100!",
    };
  } catch (err: unknown) {
    const code =
      (err as { code?: string })?.code ||
      (err as { cause?: { code?: string } })?.cause?.code;

    if (code === '23505') {
      return {
        success: false,
        error: 'This email address has already been registered.',
      };
    }
    console.error('Registration error:', err);
    return {
      success: false,
      error: 'Something went wrong. Please try again later.',
    };
  }
}