<?php
namespace App\Helpers;

use App\Models\Settings;
use Illuminate\Support\Facades\Http;

class SmsHelper {
    public static function sendSms($mobile, $message) {
        $settings = Settings::first();

        $providers = json_decode($settings->sms_provider);
        
        $url = $providers->provider;

        if(is_array($mobile)) {
            $mobile = implode(",", $mobile);
        }
        
        $data = (array)$providers->extra;
        $data[$providers->phone] = $mobile;
        $data[$providers->message] = $message;
        
        $query = collect($data)->map(function ($value, $key) {
            return urlencode($key) . '=' . urlencode($value);
        })->join('&');
    
        try {
            $response = Http::get($url, $query);
            return $response->body();
        } catch (\Exception $e) {
            return $e->getMessage();
        }
    }
}