# whatsapp_client.py
import os
import requests
import json
import config
#from dotenv import load_dotenv

#load_dotenv()

WHATSAPP_API_TOKEN="EAAOxDZCZC9LXEBO3dtEw472YziD8LIST9rOSDHtoBOZBf1YZCR6ZA9gzcsSmhIvRCpRhrS9YFiGyIIzi3qZAaefZCw6WrJkPg5DCXZBc2GXvu9kCYx0seiL1ypu0YhSCMvJ951SVMKfaHNZBtZB6TPHia7OO5A5FT47t0CQ0AmsaXFV8WT4qcAlSiZB8YwsroUft5V7PbMAMOnu7fO3yzUQURsZD"
WHATSAPP_CLOUD_NUMBER_ID="133024076564335"

class WhatsAppWrapper:

    API_URL = "https://graph.facebook.com/v18.0/"
    #WHATSAPP_API_TOKEN = os.environ.get("WHATSAPP_API_TOKEN")
    #WHATSAPP_CLOUD_NUMBER_ID = os.environ.get("WHATSAPP_CLOUD_NUMBER_ID")

    def __init__(self):
        self.headers = {
            "Authorization": f"Bearer {WHATSAPP_API_TOKEN}",
            "Content-Type": "application/json",
        }
        self.API_URL = self.API_URL + WHATSAPP_CLOUD_NUMBER_ID

    def send_template_message(self, template_name, language_code, phone_number):

        payload = {
            "messaging_product": "whatsapp",
            "to": phone_number,
            "type": "template",
            "template": {
                "name": template_name,
                "language": {
                    "code": language_code
                }
            }
        }

        response = requests.post(f"{self.API_URL}/messages", json=payload,headers=self.headers)

        assert response.status_code == 200, "Error sending message"

        return response.status_code

    def send_message(self, body, phone_number):
        print(phone_number)
        payload = {
          "messaging_product": "whatsapp",
          "recipient_type": "individual",
          "to": phone_number,
          "type": "text",
          "text": {
            "preview_url": False,
            "body": "hello"
            }
        }

        response = requests.post(f"{self.API_URL}/messages", json=payload,headers=self.headers)
        print(response.headers)
        assert response.status_code == 200, "Error sending message"

        return response.status_code

    def send_message_image(self, body, image,phone_number):

        payload = {
            "messaging_product": "whatsapp",
            "to": phone_number,
            "type": "image",
            "image" : {
                "link" : image,
                "caption" : body

             }

        }

        response = requests.post(f"{self.API_URL}/messages", json=payload,headers=self.headers)
        print(response.headers)
        assert response.status_code == 200, "Error sending message"

        return response.status_code

    def send_image(self,image,phone_number):

        payload = {
            "messaging_product": "whatsapp",
            "to": phone_number,
            "type": "image",
            "image" : {
                "link" : image

             }

        }

        response = requests.post(f"{self.API_URL}/messages", json=payload,headers=self.headers)

        assert response.status_code == 200, "Error sending message"

        return response.status_code


#if __name__ == "__main__":
    #client = WhatsAppWrapper()
    # send a template message
    #client.send_template_message("hello_world", "en_US", "13042395146")
