#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Test different parameter counts
"""
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_counts():
    """Test different parameter counts"""
    
    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
    
    headers = {
        "Authorization": f"Bearer {access_token}",
        "Content-Type": "application/json"
    }
    
    url = f"https://graph.facebook.com/v20.0/{phone_id}/messages"
    
    # Test different parameter counts
    test_params = [
        (5, ["דביר", "דביר", "שרה", "אולם", "15/06"]),
        (6, ["דביר", "דביר", "שרה", "אולם", "15/06", "18:30"]),
        (7, ["דביר", "דביר", "שרה", "אולם", "15/06", "18:30", "https://link"]),
        (8, ["דביר", "דביר", "שרה", "אולם", "15/06", "18:30", "https://link", "extra"]),
    ]
    
    print("Testing different parameter counts...")
    print("=" * 80 + "\n")
    
    for count, params in test_params:
        payload = {
            "messaging_product": "whatsapp",
            "to": "+972504370045",
            "type": "template",
            "template": {
                "name": "wedding_invitation",
                "language": {"code": "he"},
                "components": [
                    {
                        "type": "body",
                        "parameters": [{"type": "text", "text": p} for p in params]
                    }
                ]
            }
        }
        
        print(f"[Test] Parameter count: {count}")
        
        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"  [SUCCESS] Message sent!")
                data = response.json()
                msg_id = data.get('messages', [{}])[0].get('id')
                print(f"  Message ID: {msg_id}")
                return
            else:
                error = response.json().get('error', {})
                msg = error.get('message', 'Unknown')
                code = error.get('code', 'N/A')
                print(f"  [FAILED] Code {code}: {msg}")
        
        except Exception as e:
            print(f"  [ERROR] {e}")
        
        print()

import asyncio
asyncio.run(test_counts())
