<?php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\HttpClient\HttpClientInterface;

class SmsController extends AbstractController
{
    private $client;
    private $apiUrlSms = 'https://lamsms.lafricamobile.com/api'; // Remplacez par l'URL de votre service SMS

    public function __construct(HttpClientInterface $client)
    {
        $this->client = $client;
    }

    /**
     * @Route("/send-sms", name="send_sms", methods={"POST"})
     */
    public function sendSms(Request $request)
    {
        $data = json_decode($request->getContent(), true);

        $payload = [
            'accountid' => 'YAWI',
            'password' => 'yac7Eeph5cho',
            'sender' => 'YAWI SN',
            'ret_id' => 'Push_1',
            'ret_url' => 'https://mon-site.com/reception',
            'priority' => '2',
            'text' => $data['text'],
            'to' => '221' . $data['email']
        ];

        $response = $this->client->request('POST', $this->apiUrlSms, [
            'json' => $payload,
            'headers' => [
                'Content-Type' => 'application/json',
                'Accept' => 'application/json'
            ]
        ]);

        $statusCode = $response->getStatusCode();
        $content = $response->getContent();

        return new JsonResponse(['statusCode' => $statusCode, 'content' => $content]);
    }
}