<?php

use Illuminate\Support\Facades\Http;
use Modules\Shared\Sms\SmsService;

$config = [
    'enabled' => true,
    'sms_poh' => [
        'server' => 'https://sms.test/send',
        'token' => 'test-token',
        'sender' => 'FamousLY4',
    ],
];

test('send posts to the configured server with a bearer token and returns true on success', function () use ($config) {
    Http::fake(['sms.test/*' => Http::response(['status' => 'ok'])]);

    $result = (new SmsService($config))->send('+959111222333', 'Your driver is here.');

    expect($result)->toBeTrue();
    Http::assertSent(function ($request) {
        return $request->url() === 'https://sms.test/send'
            && $request->hasHeader('Authorization', 'Bearer test-token')
            && $request['to'] === '+959111222333'
            && $request['message'] === 'Your driver is here.'
            && $request['from'] === 'FamousLY4';
    });
});

test('send returns false and does not call the gateway when disabled', function () use ($config) {
    Http::fake();
    $config['enabled'] = false;

    $result = (new SmsService($config))->send('+959111222333', 'Your driver is here.');

    expect($result)->toBeFalse();
    Http::assertNothingSent();
});

test('send returns false on a non-successful gateway response', function () use ($config) {
    Http::fake(['sms.test/*' => Http::response(['error' => 'invalid'], 422)]);

    $result = (new SmsService($config))->send('+959111222333', 'Your driver is here.');

    expect($result)->toBeFalse();
});

test('send uses an explicit from over the configured sender', function () use ($config) {
    Http::fake(['sms.test/*' => Http::response(['status' => 'ok'])]);

    (new SmsService($config))->send('+959111222333', 'Hello', 'OtherSender');

    Http::assertSent(fn ($request) => $request['from'] === 'OtherSender');
});
