<?php

// =====================================
// HubSpot Configuration
// =====================================

$portalId = "21989526";
$formGuid = "9e5fecbd-998e-451b-aeda-f54d5de15010";

$hubspot_url = "https://api.hsforms.com/submissions/v3/integration/submit/$portalId/$formGuid";

// =====================================
// Get Form Data
// =====================================

$firstname = trim($_POST['firstname'] ?? '');
$email     = trim($_POST['email'] ?? '');
$phone     = trim($_POST['phone'] ?? '');
$project   = trim($_POST['project'] ?? '');
$source    = trim($_POST['source'] ?? 'GOOGLE LEAD');

// =====================================
// Capture UTM Parameters Automatically
// =====================================

$utm_source   = trim($_REQUEST['utm_source'] ?? '');
$utm_medium   = trim($_REQUEST['utm_medium'] ?? '');
$utm_campaign = trim($_REQUEST['utm_campaign'] ?? '');

// =====================================
// Get REAL User IP
// =====================================

function getUserIP()
{
    if (!empty($_SERVER['HTTP_CF_CONNECTING_IP'])) {
        return $_SERVER['HTTP_CF_CONNECTING_IP'];
    }

    if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $ipList = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
        return trim($ipList[0]);
    }

    return $_SERVER['REMOTE_ADDR'] ?? '0.0.0.0';
}

$ip_address = getUserIP();

// =====================================
// Current Page URL
// =====================================

$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off')
    ? "https://"
    : "http://";

$currentUrl = $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];

// =====================================
// Prepare HubSpot Payload
// =====================================

$data = [
    "fields" => [

        [
            "name"  => "firstname",
            "value" => $firstname
        ],
        [
            "name"  => "email",
            "value" => $email
        ],
        [
            "name"  => "phone",
            "value" => $phone
        ],
        [
            "name"  => "project",
            "value" => $project
        ],
        [
            "name"  => "source",
            "value" => $source
        ],
        [
            "name"  => "utm_source",
            "value" => $utm_source
        ],
        [
            "name"  => "utm_medium",
            "value" => $utm_medium
        ],
        [
            "name"  => "utm_campaign",
            "value" => $utm_campaign
        ],
        [
            "name"  => "ip_address",
            "value" => $ip_address
        ]

    ],

    "context" => [
        "pageUri"  => $currentUrl,
        "pageName" => "Godrej Kukatpally DGen"
    ]
];

// =====================================
// Send to HubSpot
// =====================================

$ch = curl_init($hubspot_url);

curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Content-Type: application/json'
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

curl_close($ch);

// =====================================
// Response Handling
// =====================================

if ($httpCode == 200 || $httpCode == 204) {

    header("Location: thank-you.html");
    exit();

} else {

    echo "<pre>";
    echo "ERROR: FORM NOT SUBMITTED\n\n";
    echo "HTTP CODE: $httpCode\n\n";
    echo "RESPONSE:\n";
    print_r($response);

    echo "\n\nUTM DATA:\n";
    echo "utm_source   = " . $utm_source . "\n";
    echo "utm_medium   = " . $utm_medium . "\n";
    echo "utm_campaign = " . $utm_campaign . "\n";

    echo "\n\nIP DETECTED:\n";
    echo $ip_address;
    echo "</pre>";
}
?>