<?php
namespace App\Models;

use CodeIgniter\Model;
use GuzzleHttp\Client;

class WhatsappModel extends Model
{
    private $apiUrl = "https://graph.facebook.com/v19.0/";
    private $token = "EAAJXtKkdqnUBO6npf8XAuQGYKmZADQEhABTWWDxSrKf3ZClLViqeyD34QYac2PZAaWIct2wb6RQMHmVX2muivKyiqcjgnFfdupEZBBceB1wlyaeAdGMJkpS8Kj5gCroLunPndi11Gia4k4dUGC8OWcmr8Hx0qQDuQdfnutLI5QXNT9xWDzHbNmuVx2pCXOiLJVjUjPJmoDiXwE0ZB5fRBN03gkIw6RJAR8pyuJlC9CR1h"; // Reemplaza con tu token
    private $phoneId = "541509832384420"; // Reemplaza con tu Phone ID

    public function sendMessage($to, $message)
    {
        $client = new Client();
        $url = $this->apiUrl . $this->phoneId . "/messages";

        $data = [
            "messaging_product" => "whatsapp",
            "to" => $to, // Número destino en formato internacional (ej: 593987654321)
            "type" => "text",
            "text" => ["body" => $message]
        ];

        $headers = [
            "Authorization" => "Bearer " . $this->token,
            "Content-Type" => "application/json"
        ];

        $response = $client->post($url, [
            'headers' => $headers,
            'json' => $data
        ]);

        return json_decode($response->getBody(), true);
    }
}
