<?php
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST');

if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    echo json_encode(['status' => 'error', 'message' => 'Invalid request']);
    exit;
}

// Get POST data
$name    = isset($_POST['name'])    ? htmlspecialchars(strip_tags(trim($_POST['name'])))    : '';
$email   = isset($_POST['email'])   ? htmlspecialchars(strip_tags(trim($_POST['email'])))   : '';
$mobile  = isset($_POST['mobile'])  ? htmlspecialchars(strip_tags(trim($_POST['mobile'])))  : '';
$service = isset($_POST['service']) ? htmlspecialchars(strip_tags(trim($_POST['service']))) : '';

// Validation
if (empty($name) || empty($email) || empty($mobile) || empty($service)) {
    echo json_encode(['status' => 'error', 'message' => 'All fields are required.']);
    exit;
}

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    echo json_encode(['status' => 'error', 'message' => 'Invalid email address.']);
    exit;
}

if (!preg_match('/^[0-9]{10}$/', $mobile)) {
    echo json_encode(['status' => 'error', 'message' => 'Invalid mobile number.']);
    exit;
}

// Mail configuration
$to      = 'office@magmafinlink.com';
$subject = 'New Enquiry from ' . $name . ' - ' . $service;

$message = '
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: Arial, sans-serif; background: #f5f7fa; margin: 0; padding: 20px; }
  .email-wrapper { max-width: 600px; margin: 0 auto; background: #fff; border-radius: 10px; overflow: hidden; box-shadow: 0 4px 20px rgba(0,0,0,0.08); }
  .email-header { background: #0d2c54; padding: 30px; text-align: center; }
  .email-header h2 { color: #fff; margin: 0; font-size: 22px; }
  .email-header p { color: rgba(255,255,255,0.7); margin: 6px 0 0; font-size: 13px; }
  .email-body { padding: 32px; }
  .email-body h3 { color: #0d2c54; font-size: 16px; margin: 0 0 20px; }
  .field-row { display: flex; margin-bottom: 16px; border-bottom: 1px solid #f0f0f0; padding-bottom: 16px; }
  .field-label { width: 140px; font-size: 13px; font-weight: 700; color: #666; flex-shrink: 0; }
  .field-value { font-size: 14px; color: #0d2c54; font-weight: 600; }
  .service-badge { display: inline-block; background: #e8604c; color: #fff; padding: 4px 14px; border-radius: 20px; font-size: 13px; font-weight: 600; }
  .email-footer { background: #f5f7fa; padding: 20px 32px; text-align: center; font-size: 12px; color: #999; border-top: 1px solid #eee; }
</style>
</head>
<body>
<div class="email-wrapper">
  <div class="email-header">
    <h2>New Enquiry Received</h2>
    <p>Magma Capital Finlink Private Limited</p>
  </div>
  <div class="email-body">
    <h3>Enquiry Details</h3>
    <div class="field-row">
      <div class="field-label">Full Name</div>
      <div class="field-value">' . $name . '</div>
    </div>
    <div class="field-row">
      <div class="field-label">Email Address</div>
      <div class="field-value">' . $email . '</div>
    </div>
    <div class="field-row">
      <div class="field-label">Mobile Number</div>
      <div class="field-value">' . $mobile . '</div>
    </div>
    <div class="field-row" style="border-bottom:none; padding-bottom:0;">
      <div class="field-label">Service</div>
      <div class="field-value"><span class="service-badge">' . $service . '</span></div>
    </div>
  </div>
  <div class="email-footer">
    This enquiry was submitted via the website enquiry form on ' . date('d M Y, h:i A') . '
  </div>
</div>
</body>
</html>
';

$headers  = "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
$headers .= "From: Magma Capital Finlink <noreply@magmacapitals.in>\r\n";
$headers .= "Reply-To: " . $email . "\r\n";
$headers .= "X-Mailer: PHP/" . phpversion();

// Send mail
$sent = mail($to, $subject, $message, $headers);

if ($sent) {
    // Send auto-reply to user
    $auto_subject = 'Thank you for your Enquiry - Magma Capital Finlink';
    $auto_message = '
    <!DOCTYPE html>
    <html>
    <head>
    <style>
      body { font-family: Arial, sans-serif; background: #f5f7fa; margin: 0; padding: 20px; }
      .email-wrapper { max-width: 600px; margin: 0 auto; background: #fff; border-radius: 10px; overflow: hidden; box-shadow: 0 4px 20px rgba(0,0,0,0.08); }
      .email-header { background: #0d2c54; padding: 30px; text-align: center; }
      .email-header h2 { color: #fff; margin: 0; font-size: 22px; }
      .email-header p { color: rgba(255,255,255,0.7); margin: 6px 0 0; font-size: 13px; }
      .email-body { padding: 32px; color: #333; line-height: 1.7; font-size: 15px; }
      .highlight { color: #0d2c54; font-weight: 700; }
      .badge { display: inline-block; background: #e8604c; color: #fff; padding: 4px 14px; border-radius: 20px; font-size: 13px; font-weight: 600; }
      .email-footer { background: #f5f7fa; padding: 20px 32px; text-align: center; font-size: 12px; color: #999; border-top: 1px solid #eee; }
    </style>
    </head>
    <body>
    <div class="email-wrapper">
      <div class="email-header">
        <h2>Thank You for Your Enquiry!</h2>
        <p>Magma Capital Finlink Private Limited</p>
      </div>
      <div class="email-body">
        <p>Dear <span class="highlight">' . $name . '</span>,</p>
        <p>Thank you for reaching out to <strong>Magma Capital Finlink Private Limited</strong>. We have received your enquiry regarding <span class="badge">' . $service . '</span>.</p>
        <p>Our team will review your enquiry and get back to you within <strong>1–2 business days</strong>.</p>
        <p>If you need immediate assistance, feel free to call us at <strong>+91 9363531965</strong> or email us at <strong>office@magmafinlink.com</strong>.</p>
        <p>Warm regards,<br><strong>Magma Capital Finlink Private Limited</strong></p>
      </div>
      <div class="email-footer">
        Mon - Sat | 9:00 AM - 6:00 PM | office@magmafinlink.com | +91 9363531965
      </div>
    </div>
    </body>
    </html>
    ';

    $auto_headers  = "MIME-Version: 1.0\r\n";
    $auto_headers .= "Content-Type: text/html; charset=UTF-8\r\n";
    $auto_headers .= "From: Magma Capital Finlink <noreply@magmacapitals.in>\r\n";

    mail($email, $auto_subject, $auto_message, $auto_headers);

    echo json_encode(['status' => 'success', 'message' => 'Enquiry sent successfully!']);
} else {
    echo json_encode(['status' => 'error', 'message' => 'Failed to send email. Please try again.']);
}
?>