<?php

namespace App\Modules\Sms\Repositories;

use Exception;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request as GuzzleRequest;
use Illuminate\Support\Facades\Log;

class SmsRepository
{
    /**
     * Send a sms
     */
    public static function sendSMS(string|int $phone, string|int $message): mixed
    {
        if (app()->environment('local')) {
            return true;
        }

        $client = new Client;
        $headers = [
            'Content-Type' => 'application/json;charset=utf-8;',
            'Charset' => 'UTF-8',
        ];
        $body = 'JSON={
            "SendRequest": {
              "TerminalID": "Online_PANEL",
              "Version": "1",
              "Lang": "EN",
              "MobilePhone": "993'.$phone.'",
              "Text": "'.$message.'"
            }
        }';
        // 10.3.158.103
        $request = new GuzzleRequest('POST', 'http://10.3.158.28:8080/kpsmsroute/online.request', $headers, $body);

        try {
            $res = $client->sendAsync($request)->wait();

            return $res->getBody(); // @phpstan-ignore-line
        } catch (Exception $e) {
            Log::error($e);

            return null;
        }
    }
}
