    <?php
    require_once __DIR__ . '/includes/redirect_helper.php';
    $name = trim((string)($_POST['name'] ?? ''));
    $email = trim((string)($_POST['email'] ?? ''));
    $song = trim((string)($_POST['song'] ?? ''));
    $artist = trim((string)($_POST['artist'] ?? ''));
    $dedication = trim((string)($_POST['dedication'] ?? ($_POST['message'] ?? '')));
    $genre = trim((string)($_POST['genre'] ?? ''));
    $djName = trim((string)($_POST['dj_name'] ?? ''));
    $djId = trim((string)($_POST['dj_id'] ?? ''));

    $clean = function ($value) {
        $value = str_replace(["
", "
"], "
", (string)$value);
        $value = preg_replace("/
{3,}/", "

", $value);
        return trim($value);
    };

    if ($name === '' || $song === '') {
        ah_redirect_back('/requests.php', ['error' => 'missing']);
        exit;
    }

    $saveFile = __DIR__ . '/data/requests.txt';
    if (!is_dir(dirname($saveFile))) @mkdir(dirname($saveFile), 0777, true);
    $entry = "Name: " . $clean($name) . "
"
           . "Email: " . $clean($email) . "
"
           . "Song: " . $clean($song) . "
"
           . "Artist: " . $clean($artist) . "
"
           . "Dedication: " . $clean($dedication) . "
---
";
    file_put_contents($saveFile, $entry, FILE_APPEND | LOCK_EX);

    $apiUrl = 'https://broadcasturself.com/request/insert_request.php';
    $payload = [
        'DJ_Name' => $djName !== '' ? $djName : 'Website Request',
        'Company' => 'Almost Heaven',
        'Message' => $dedication,
        'Song_Name' => $song,
        'Artist' => $artist,
        'Genre' => $genre,
        'Req_From' => 'Website',
        'DJ_ID' => $djId,
        'Req_User_ID' => $email !== '' ? $email : $name,
    ];

    $apiResponse = null;
    $apiError = null;

    if (function_exists('curl_init')) {
        $ch = curl_init($apiUrl);
        curl_setopt_array($ch, [
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_POST => true,
            CURLOPT_POSTFIELDS => http_build_query($payload),
            CURLOPT_HTTPHEADER => ['Content-Type: application/x-www-form-urlencoded'],
            CURLOPT_TIMEOUT => 10,
        ]);
        $apiResponse = curl_exec($ch);
        if ($apiResponse === false) {
            $apiError = 'cURL Error: ' . curl_error($ch);
        }
        curl_close($ch);
    } else {
        $context = stream_context_create([
            'http' => [
                'method' => 'POST',
                'header' => "Content-Type: application/x-www-form-urlencoded
",
                'content' => http_build_query($payload),
                'timeout' => 10,
            ],
        ]);
        $apiResponse = @file_get_contents($apiUrl, false, $context);
        if ($apiResponse === false) {
            $apiError = 'Request API call failed via stream fallback.';
        }
    }

    if ($apiError !== null) {
        error_log('Request API Error (Almost Heaven): ' . $apiError);
    } else {
        error_log('Request API Response (Almost Heaven): ' . trim((string)$apiResponse));
    }

    ah_redirect_back('/requests.php', ['sent' => 1]);
    exit;
