<?php
require_once __DIR__ . '/includes/mail-helper.php';

if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    gx_json(false, 'Invalid request method.');
}

$name    = $_POST['name']    ?? '';
$email   = $_POST['email']   ?? '';
$phone   = $_POST['phone']   ?? '';
$message = $_POST['message'] ?? '';
$recaptchaToken = $_POST['g-recaptcha-response'] ?? '';

if (trim($name) === '') {
    gx_json(false, 'Please enter your full name.');
}
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    gx_json(false, 'Please enter a valid email address.');
}

if (!gx_verify_recaptcha($recaptchaToken)) {
    gx_json(false, 'reCAPTCHA verification failed. Please try again.');
}

$subject = 'New Proposal Request — ' . $name;
$body = '
<table style="font-family: Arial, sans-serif; font-size:14px; color:#111;" cellpadding="6">
  <tr><td><b>Full Name</b></td><td>' . gx_e($name) . '</td></tr>
  <tr><td><b>Email</b></td><td>' . gx_e($email) . '</td></tr>
  <tr><td><b>Phone</b></td><td>' . gx_e($phone) . '</td></tr>
  <tr><td><b>Comments / Message</b></td><td>' . nl2br(gx_e($message)) . '</td></tr>
</table>';

$sent = gx_send_mail(GX_PROPOSAL_TO_EMAIL, $subject, $body, $email, $name);

if ($sent) {
    gx_json(true, 'Thanks — your request has been sent. We will be in touch soon.');
} else {
    gx_json(false, 'Something went wrong while sending your request. Please try again later.');
}
