<?php

if (! function_exists('sendSMS')) {
    function sendSMS(string $destination, string $message): bool
    {
        $key = env('SMS_API') ?: '';
        $sender = env('SMS_SENDER') ?: 'SMSBit';
        $url = env('SMS_URL') ?: 'https://sms.bibhutisolutions.com/smsapi/index';

        $params = http_build_query([
            'key' => $key,
            'contacts' => $destination,
            'senderid' => $sender,
            'msg' => $message,
            'responsetype' => 'json',
        ]);

        $separator = parse_url($url, PHP_URL_QUERY) ? '&' : '?';
        $url_final = $url . $separator . $params;

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url_final);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        $response = curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);

        if ($response === false || $httpCode >= 400) {
            return false;
        }

        return true;
    }
}
