import { NextRequest } from 'next/server';
import { createResponse } from '@/lib/api';
import { EmailService } from '@/src/services/EmailService';

export async function POST(req: NextRequest) {
  const { name, email, message, recaptchaToken } = await req.json();

  if (process.env.NODE_ENV === 'production') {
    if (!recaptchaToken) {
      return createResponse(400, 'Recaptcha token is required');
    }

    const res = await fetch('https://www.google.com/recaptcha/api/siteverify', {
      method: 'POST',
      headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
      body: `secret=${process.env.RECAPTCHA_SECRET_KEY}&response=${recaptchaToken}`,
    });

    const data = await res.json();

    if (!data.success || data.score < 0.5) {
      return createResponse(400, 'Recaptcha verification failed');
    }


    await EmailService.sendContactMessage(name, email, message);

    return createResponse(200, 'Email sent successfully', { data: true });
    
  }

  return createResponse(200, 'Email sent successfully', { data: true });
}
