"""
Quick script to register Main Branch (4086675) callback URLs with M-Pesa
Run this with: python manage.py shell
Then paste this code or run: exec(open('register_main_branch.py').read())
"""

import django
import os

# Setup Django if not already configured
if not os.environ.get('DJANGO_SETTINGS_MODULE'):
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings')
    django.setup()

from payments.branch_mpesa_service import BranchMpesaService
from users.models import Branch

# Get Main Branch
try:
    main_branch = Branch.objects.get(mpesa_shortcode='4086675')
    print(f"Found branch: {main_branch.name}")
    print(f"Shortcode: {main_branch.mpesa_shortcode}")
    
    # Initialize service
    service = BranchMpesaService(main_branch)
    
    # Register URLs
    print("\nRegistering callback URLs...")
    print(f"Validation URL: https://branchbusinessadvance.co.ke/payments/callback/main/validation/")
    print(f"Confirmation URL: https://branchbusinessadvance.co.ke/payments/callback/main/confirmation/")
    
    result = service.register_c2b_urls()
    print("\n✓ SUCCESS!")
    print(f"Response: {result}")
except Branch.DoesNotExist:
    print("✗ ERROR: Main Branch with shortcode 4086675 not found")
except Exception as e:
    print(f"\n✗ ERROR: {e}")
    import traceback
    traceback.print_exc()
