<?php

declare(strict_types=1);

namespace SmsNotifier\Facade;

use SmsNotifier\Data\NotificationData;
use SmsNotifier\Factory\MessageTextFactory;
use SmsNotifier\Service\Logger;
use SmsNotifier\Service\SmsNumberProvider;
use Twilio\Exceptions\HttpException;
use SmsNotifier\Facade\ClientCallBellAPI;


/*
 * send message to client's number
 */

abstract class AbstractUpdateClientFacade
{
    /**
     * @var Logger
     */
    protected $logger;

    /**
     * @var MessageTextFactory
     */
    protected $messageTextFactory;

    /**
     * @var SmsNumberProvider
     */
    protected $smsNumberProvider;

    public function __construct(
        Logger $logger,
        MessageTextFactory $messageTextFactory,
        SmsNumberProvider $smsNumberProvider
    ) {
        $this->logger = $logger;
        $this->messageTextFactory = $messageTextFactory;
        $this->smsNumberProvider = $smsNumberProvider;
    }

    /*
     * sets up the body and uses the implementation's sendMessage() to send
     */
    public function notify(NotificationData $notificationData): void
    {
        $clientSmsNumber = $this->smsNumberProvider->getUcrmClientNumber($notificationData); //renombrar variables

        if (empty($clientSmsNumber)) {
            $this->logger->warning('No SMS number found for client: ' . $notificationData->clientId);
            return;
        }
        // $messageBody = $this->messageTextFactory->createBody($notificationData);
        // if (! $messageBody) {
        //     $this->logger->info('No text configured for event: ' . $notificationData->eventName);
        //     return;
        // }


        try {
            $this->logger->debug(sprintf('llego al llamado de sendwhatsapp con client_id: %s y número de celular: %s', $notificationData->clientId, $clientSmsNumber));
            //$this->sendMessage($notificationData, $clientSmsNumber, $messageBody);
            //$this->sendWhatsApp($notificationData, $clientSmsNumber);

            $client_callbell_api = new ClientCallBellAPI();
            $client_callbell_api->sendMessageWhatsApp($clientSmsNumber, $notificationData);
            $response_getContactCallBell=json_decode($client_callbell_api->getContactWhatsapp($clientSmsNumber));
            $client_callbell_api->patchWhatsapp($response_getContactCallBell->contact->uuid,$notificationData);

        } catch (HttpException $httpException) {
            //$this->logger->debug('Ocurrio un error en el try catch');
            $this->logger->error($httpException->getCode() . ' ' . $httpException->getMessage());
        }
    }

    /**
     * implement in subclass with the specific messaging provider
     * @see TwilioNotifierFacade::sendMessage()
     */
    abstract protected function sendMessage(
        NotificationData $notificationData,
        string $clientSmsNumber,
        string $messageBody
    ): void;

    //     /**
    //      * implement in subclass with the specific messaging provider
    //      * @see TwilioNotifierFacade::sendWhatsApp()
    //      */
    //     abstract protected function sendWhatsApp(
    //         NotificationData $notificationData,
    //         string $clientSmsNumber
    //     ): void;
}
