<?php

namespace App\Sms\Provider;

use Symfony\Contracts\HttpClient\HttpClientInterface;

class RanginehProvider implements SmsProviderInterface
{
    private const BASE = 'https://rest.payamresan.com/api/v1';

    public function __construct(
        private readonly HttpClientInterface $httpClient,
        private readonly string $apiKey,
        private readonly string $sender,
    ) {}

    public function getName(): string { return 'rangineh'; }

    public function send(string $mobile, string $message): bool
    {
        try {
            $resp = $this->httpClient->request('POST', self::BASE . '/send', [
                'json' => ['from' => $this->sender, 'to' => [$mobile], 'text' => $message],
                'headers' => ['ApiKey' => $this->apiKey],
                'timeout' => 10,
            ]);
            return $resp->getStatusCode() === 200;
        } catch (\Throwable) {
            return false;
        }
    }

    public function sendTemplate(string $mobile, string $templateCode, array $vars): bool
    {
        try {
            $resp = $this->httpClient->request('POST', self::BASE . '/send/verify', [
                'json' => [
                    'mobile'   => $mobile,
                    'template' => $templateCode,
                    'params'   => $vars,
                ],
                'headers' => ['ApiKey' => $this->apiKey],
                'timeout' => 10,
            ]);
            return $resp->getStatusCode() === 200;
        } catch (\Throwable) {
            return false;
        }
    }
}
