<?php

namespace App\Services;

use Illuminate\Support\Facades\Http;

class ClickSendSmsService
{
    private string $username;
    private string $apiKey;
    private string $baseUrl = 'https://rest.clicksend.com/v3';

    public function __construct()
    {
        $this->username = config('services.clicksend.username');
        $this->apiKey   = config('services.clicksend.api_key');
    }

    public function send(string $to, string $message, ?string $from = null): array
    {
        $payload = [
            'messages' => [
                array_filter([
                    'body' => $message,
                    'to'   => $to,
                    'from' => $from,
                ]),
            ],
        ];

        $response = Http::withBasicAuth($this->username, $this->apiKey)
            ->post("{$this->baseUrl}/sms/send", $payload);

        $response->throw();

        return $response->json();
    }
}
