<?php

namespace App\Services\Integration\Integration;

use App\Services\Integration\Interface\SMSInterface;
use Illuminate\Support\Facades\Http;

class SMS implements SMSInterface
{
    
    public static function sendSMS($phone,$message)
    {
        $app_id = "SVkSuJ5a5VfMOCiK1WlLLiZGbRmi8la5xmivraZd";
        $app_sec = "5yleIBACQMPSMsUF1cK1vY71dqvl4EqaogAXg8w8NYi7idWc7q3ahjn4Kd2NzA8gOhuEWGkALglhwEDgTu547sQ9UyInpOLYmqxH";
        $app_hash = base64_encode("{$app_id}:{$app_sec}");
        $messages = [
            "messages" => [
                [
                    "text" => $message,
                    "numbers" => [$phone],
                    "sender" => "The Chord"
                ]
            ]
        ];
        
        $url = "https://api-sms.4jawaly.com/api/v1/account/area/sms/send";
        $headers = [
            "Accept: application/json",
            "Content-Type: application/json",
            "Authorization: Basic {$app_hash}"
        ];
        
        $curl = curl_init($url);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($messages));
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        
        $response = curl_exec($curl);
        $status_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
        curl_close($curl);
        
        $response_json = json_decode($response, true);
        // dd($response_json);
        if ($status_code == 200) {
            if (isset($response_json["messages"][0]["err_text"])) {
                // echo $response_json["messages"][0]["err_text"];
            } else {
                // echo "تم الارسال بنجاح  " . " job id:" . $response_json["job_id"];
            }
        } elseif ($status_code == 400) {
            // echo $response_json["message"];
        } elseif ($status_code == 422) {
            // echo "نص الرسالة فارغ";
        } else {
            // echo "محظور بواسطة كلاودفلير. Status code: {$status_code}";
        }
    }
}