<?php
use Twilio\Rest\Client;
class Eca_Socialnotify_Cf7_Twilio_Helper {
    private static $instance = null;

    public function sendWpMessage($recipient, $messageBody, &$error_logs = [])
    {
        try {
            $sid    = eca_socialnotify_cf7_general_helper()->get_plugin_options('twilio_account_sid');
            $token  = eca_socialnotify_cf7_general_helper()->get_plugin_options('twilio_auth_token');
            $phoneNumber = eca_socialnotify_cf7_general_helper()->get_plugin_options('twilio_phone_number');

            if ( empty($sid) || empty($token) ) {
                $error_logs[] = "SocialNotify: Twilio error, API SID or Token is empty.";
                return false;
            }

            $twilio = new Client($sid, $token);
            $twilio->messages
                ->create(
                    "whatsapp:$recipient", // to
                    [
                    "from" => "whatsapp:$phoneNumber",
                    "body" => $messageBody
                    ]
                );
            return true;
        } catch (\Throwable $th) {
            $error_logs[] = "SocialNotify: " . $th->getMessage();
            return false;
        }
    }

    public static function getInstance() {
        if (self::$instance == null) {
            self::$instance = new self();
        }
        return self::$instance;
    }
}

function eca_socialnotify_cf7_twilio_helper() {
    return Eca_Socialnotify_Cf7_Twilio_Helper::getInstance();
}
