import hmac
import hashlib
import json
import time
import requests
import sys

META_APP_SECRET = "skeen_meta_app_secret_2026_demo"
PHONE_NUMBER_ID = "PENDING_PHONE_ID"
WABA_ID = "PENDING_WABA_ID"

payload = {
    "object": "whatsapp_business_account",
    "entry": [
        {
            "id": WABA_ID,
            "changes": [
                {
                    "value": {
                        "messaging_product": "whatsapp",
                        "metadata": {
                            "display_phone_number": "1234567890",
                            "phone_number_id": PHONE_NUMBER_ID,
                        },
                        "contacts": [
                            {
                                "profile": {"name": "Paciente Test"},
                                "wa_id": "526641234568",
                            }
                        ],
                        "messages": [
                            {
                                "from": "526641234568",
                                "id": f"wamid.test_{int(time.time())}",
                                "timestamp": str(int(time.time())),
                                "text": {"body": "Hola Sofia, que servicios tienen?"},
                                "type": "text",
                            }
                        ],
                    },
                    "field": "messages",
                }
            ],
        }
    ],
}

raw_body = json.dumps(payload, separators=(",", ":"))
signature = "sha256=" + hmac.new(
    META_APP_SECRET.encode(), raw_body.encode(), hashlib.sha256
).hexdigest()

url = "http://localhost:3000/api/whatsapp/webhook"
headers = {
    "Content-Type": "application/json",
    "x-hub-signature-256": signature,
}

print("Enviando webhook simulado a", url)
print("Body:", raw_body[:200], "...")
response = requests.post(url, data=raw_body, headers=headers)
print("Status:", response.status_code)
print("Response:", response.text[:500])
