<?php

// sms_api.php

// Hardcode API credentials as requested
define('BEEM_API_KEY', '4d2495c90cbee253');
define('BEEM_SECRET_KEY', 'MWVkOWQzYWM5Yjg1MmNiNDA2ZGRiODZkODI3ZDQ0OTk4ODBmNzhmYTU5Mzg3YmM5Y2M5Y2ZkN2Q0OGI3ZTJlMw==');

/**
 * Checks the remaining SMS balance from the Beem API.
 * This is now a reference function only, as per new requirements.
 * @return float|false The remaining balance or false on failure.
 */
function getBeemBalanceFromApi() {
    $api_key = BEEM_API_KEY;
    $secret_key = BEEM_SECRET_KEY;
    $Url = 'https://apisms.beem.africa/public/v1/vendors/balance';

    $ch = curl_init($Url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt_array($ch, array(
        CURLOPT_HTTPGET => TRUE,
        CURLOPT_RETURNTRANSFER => TRUE,
        CURLOPT_HTTPHEADER => array(
            'Authorization:Basic ' . base64_encode("$api_key:$secret_key"),
            'Content-Type: application/json'
        ),
    ));

    $response = curl_exec($ch);
    if ($response === FALSE) {
        error_log("cURL Error: " . curl_error($ch));
        curl_close($ch);
        return false;
    }

    curl_close($ch);
    $responseData = json_decode($response, true);

    if (isset($responseData['data']['attributes']['balance'])) {
        return (float) $responseData['data']['attributes']['balance'];
    }

    return false;
}

/**
 * Sends an SMS via the Beem API.
 * @param string $recipient_phone The recipient's phone number.
 * @param string $message The message content.
 * @return array The API response data.
 */
function sendBeemSms($recipient_phone, $message) {
    $api_key = BEEM_API_KEY;
    $secret_key = BEEM_SECRET_KEY;

    $postData = array(
        'source_addr' => 'OCOAN',
        'encoding' => 0,
        'schedule_time' => '',
        'message' => $message,
        'recipients' => [
            array('recipient_id' => '1', 'dest_addr' => $recipient_phone)
        ]
    );

    $Url = 'https://apisms.beem.africa/v1/send';

    $ch = curl_init($Url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt_array($ch, array(
        CURLOPT_POST => true,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_HTTPHEADER => array(
            'Authorization: Basic ' . base64_encode("$api_key:$secret_key"),
            'Content-Type: application/json'
        ),
        CURLOPT_POSTFIELDS => json_encode($postData)
    ));

    $response = curl_exec($ch);
    if ($response === FALSE) {
        $error = curl_error($ch);
        curl_close($ch);
        return [
            'status' => 'Failed',
            'beem_request_id' => null,
            'api_response' => '{"error_message": "cURL Error: ' . $error . '"}',
            'sent_count' => 0,
            'error_message' => "cURL Error: " . $error
        ];
    }

    curl_close($ch);
    $responseData = json_decode($response, true);

    $status = 'Failed';
    $beem_request_id = null;
    $sent_count = 0;
    $error_message = 'Unknown error.';

    if (isset($responseData['successful']) && $responseData['successful'] > 0) {
        $status = 'Sent';
        $sent_count = $responseData['successful'];
        $beem_request_id = $responseData['request_id'] ?? null;
        $error_message = null;
    } else {
        $status = 'Failed';
        $error_message = isset($responseData['message']) ? $responseData['message'] : (isset($responseData['error_message']) ? $responseData['error_message'] : 'Unknown API error.');
    }

    return [
        'status' => $status,
        'beem_request_id' => $beem_request_id,
        'api_response' => $response,
        'sent_count' => $sent_count,
        'error_message' => $error_message
    ];
}

?>