import { NextRequest, NextResponse } from 'next/server';
import { pool } from '@/backend/utils/db';
import type { OkPacket } from 'mysql2';

export async function POST(req: NextRequest) {
  const body = await req.json();
  const { yourName, yourMobile, yourEmail, product, friendName, friendMobile, preferredTime } = body;

 
  try {
    await pool.query<OkPacket>(
      `INSERT INTO referrals (your_name, your_mobile, your_email, product, friend_name, friend_mobile, preferred_time)
       VALUES (?, ?, ?, ?, ?, ?, ?)`,
      [yourName, yourMobile, yourEmail, product, friendName, friendMobile, preferredTime]
    );

    return NextResponse.json({ success: true });
  } catch (error) {
    console.error('[DB ERROR]', error);
    return NextResponse.json({ success: false, error: 'Failed to submit referral.' }, { status: 500 });
  } 
}
