import os
import sys
import django
import africastalking

# Set up Django environment
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings')
django.setup()

from django.conf import settings

username = getattr(settings, 'AFRICASTALKING_USERNAME', 'sandbox')
api_key = getattr(settings, 'AFRICASTALKING_API_KEY', '')

print(f"Initializing with username='{username}'")
africastalking.initialize(username, api_key)
sms = africastalking.SMS

print("\n--- SENDING TEST MESSAGE ---")
message = "Test message from Lofty Creek local server"
recipients = ["+254722119828"] # Brian Orandi's phone number from DB
test_sender_id = "LoftyCreek"

try:
    print(f"Sending to {recipients} with sender_id='{test_sender_id}'...")
    response = sms.send(message, recipients, test_sender_id)
    print("\nResponse with sender_id:")
    print(response)
except Exception as e:
    print("\nFailed to send with sender_id:")
    print(e)
