#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Test template with 0 parameters
"""
import sys
import os
from dotenv import load_dotenv

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
load_dotenv()

import httpx

async def test_zero_params():
    """Test template with no parameters"""
    
    access_token = os.getenv("WHATSAPP_ACCESS_TOKEN")
    phone_id = os.getenv("WHATSAPP_PHONE_NUMBER_ID")
    
    if not access_token or not phone_id:
        print("[ERROR] Missing credentials")
        return
    
    print("Testing template with 0 parameters...")
    print("=" * 80)
    
    headers = {
        "Authorization": f"Bearer {access_token}",
        "Content-Type": "application/json"
    }
    
    url = f"https://graph.facebook.com/v20.0/{phone_id}/messages"
    
    # Template with NO parameters
    payload = {
        "messaging_product": "whatsapp",
        "to": "+972504370045",
        "type": "template",
        "template": {
            "name": "wedding_invitation",
            "language": {"code": "he"}
        }
    }
    
    print(f"\nSending template 'wedding_invitation' with NO parameters...")
    
    try:
        async with httpx.AsyncClient() as client:
            response = await client.post(url, json=payload, headers=headers, timeout=10)
        
        if response.status_code in (200, 201):
            print(f"\n[SUCCESS] Template sent with 0 params!")
            data = response.json()
            msg_id = data.get('messages', [{}])[0].get('id')
            print(f"Message ID: {msg_id}")
            print("\n==> Template EXISTS but requires 0 parameters (it's a static template)")
        else:
            error = response.json().get('error', {})
            error_msg = error.get('message', error)
            error_code = error.get('code', 'N/A')
            print(f"\n[FAILED] {error_code}: {error_msg}")
            
            if "does not exist" in str(error_msg):
                print("\n==> Template NOT FOUND in Meta!")
                print("Please check:")
                print("  1. Template name is exactly: 'wedding_invitation'")
                print("  2. Language is: 'Hebrew' (he)")
                print("  3. Status is: 'APPROVED'")
            elif "parameters" in str(error_msg):
                print("\n==> Template EXISTS but parameter count is wrong")
    
    except Exception as e:
        print(f"\n[ERROR] {e}")

import asyncio
asyncio.run(test_zero_params())
