#!/usr/bin/env python3
"""
Test script to send WhatsApp message via template
Usage: python test_whatsapp_send.py
"""

import asyncio
import os
import sys
import logging
from dotenv import load_dotenv

# Load environment variables
load_dotenv()

# Configure logging
logging.basicConfig(
    level=logging.DEBUG,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)

# Import after logging is configured
from database import SessionLocal
from whatsapp import WhatsAppService


async def test_send_whatsapp():
    """Test sending a WhatsApp message using hina_invitation template"""
    
    db = SessionLocal()
    
    try:
        # Initialize WhatsApp service
        service = WhatsAppService(db=db)
        
        # Phone number to test (Israeli format)
        phone = "0504370045"
        
        # Template parameters for hina_invitation
        params = {
            "contact_name": "דוד",
            "event_date": "17/05",
            "event_date_day": "17",
            "venue": "אולם הגן",
            "location": "ירושלים",
            "reception_time": "18:30",
            "ceremony_time": "19:00",
            "dinner_time": "20:00",
            "bride_name": "ורד",
            "groom_name": "דביר",
            "event_id": "f3122a7d-1d7c-4cc1-955d-1c6b7358bd25"
        }
        
        print("\n" + "="*80)
        print("TESTING WHATSAPP MESSAGE SEND")
        print("="*80)
        print(f"Phone: {phone}")
        print(f"Template: hina_invitation")
        print(f"Parameters: {params}")
        print("="*80 + "\n")
        
        # Send the message
        logger.info(f"Attempting to send WhatsApp message to {phone}")
        result = await service.send_by_template_key(
            template_key="wedding_invitation_by_vered",
            to_phone=phone,
            params=params
        )
        
        print("\n" + "="*80)
        print("SUCCESS!")
        print("="*80)
        print(f"Result: {result}")
        print("="*80 + "\n")
        
        return result
        
    except Exception as e:
        print("\n" + "="*80)
        print("ERROR!")
        print("="*80)
        print(f"Error: {str(e)}")
        import traceback
        traceback.print_exc()
        print("="*80 + "\n")
        raise
    
    finally:
        db.close()


if __name__ == "__main__":
    asyncio.run(test_send_whatsapp())
