import { NextResponse } from 'next/server';
import { EligibilityEmail } from '../../../../../lib/mailer';

export async function POST(request) {
  try {
    const body = await request.json();
    const { 
      eligibilityResults, 
      userData,
      calculationInputs,
      symbol
    } = body;

    // Validate required data
    if (!userData?.email) {
      return NextResponse.json(
        { success: false, message: 'User email is required' },
        { status: 400 }
      );
    }

    if (!eligibilityResults) {
      return NextResponse.json(
        { success: false, message: 'No eligibility results provided' },
        { status: 400 }
      );
    }

    const info = await EligibilityEmail(eligibilityResults, userData, calculationInputs, symbol)

    return NextResponse.json({
      success: true,
      message: 'Eligibility report sent successfully',
      messageId: info.messageId
    });

  } catch (error) {
    console.error('Error sending eligibility email:', error);
    
    return NextResponse.json(
      { 
        success: false, 
        message: 'Failed to send email',
        error: process.env.NODE_ENV === 'development' ? error.message : undefined 
      },
      { status: 500 }
    );
  }
}