<?php
header("Content-Type: application/json");
$data = json_decode(file_get_contents("php://input"), true);
if (isset($data["email"])) {
    // 1. Send the lead info to outreach@noobogroup.org (for internal notification)
    $to = "outreach@noobogroup.org";
    $subject = "New Hueble Lead: " . ($data["name"] ?? "Someone");
    $message = "A new lead captured from Hueble.com:\n\nName: " . ($data["name"] ?? "N/A") . "\nEmail: " . ($data["email"] ?? "N/A") . "\nInterest: " . ($data["interest"] ?? "N/A") . "\nMessage: " . ($data["message"] ?? "N/A");
    $headers = "From: system@noobogroup.org";
    mail($to, $subject, $message, $headers);

    // 2. Send the automated TEMPO welcome email to the lead
    $lead_email = $data["email"];
    $lead_name = $data["name"] ?? "there";
    $welcome_subject = "Hueble Network | Next Steps";
    $welcome_message = "Hi $lead_name,\n\nThank you for reaching out to Hueble today. I am following up on behalf of Joseph Fears.\n\nTo best align our resources, are you primarily interested in exploring our data intelligence (Kingmaker), utilizing our funding network (Hueble), or discussing a broader strategic partnership?\n\nLooking forward to speaking soon.\n\nBest,\nRodrigo\nBusiness Development | NOOBO Group LLC";
    
    $welcome_headers = "From: Rodrigo <rodrigo@noobogroup.org>\r\n";
    $welcome_headers .= "Reply-To: rodrigo@noobogroup.org\r\n";
    
    mail($lead_email, $welcome_subject, $welcome_message, $welcome_headers);

    echo json_encode(["status" => "success"]);
} else {
    http_response_code(400);
    echo json_encode(["status" => "error", "message" => "Invalid data"]);
}
?>
