import { pool } from '@/backend/utils/db';
import type { OkPacket } from 'mysql2';

export interface ApplicationData {
  id?: number;
  cardId: number;
  name: string;
  email: string;
  phone: string;
    salary: number;
  agreeTerms: boolean;
}

export async function saveApplication(data: ApplicationData) {
  try {
    const { cardId, name, email, phone,  salary, agreeTerms } = data;

    const [result] = await pool.query<OkPacket>(
      `
      INSERT INTO creditcard_applications
        (card_id, name, email, phone,  salary, agree, submitted_at)
      VALUES (?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP)
    `,
      [cardId, name, email, phone,  salary, agreeTerms ? 1 : 0]
    );

    return { success: true, insertId: result.insertId };
  } catch (error) {
    console.error('saveApplication error:', error);
    return { success: false, error: (error as Error).message };
  }
}
