from django.core.management.base import BaseCommand
from django.conf import settings
from payments.models import MpesaConfiguration


class Command(BaseCommand):
    help = 'Set up initial M-Pesa configuration'

    def add_arguments(self, parser):
        parser.add_argument(
            '--environment',
            type=str,
            choices=['sandbox', 'production'],
            default='sandbox',
            help='M-Pesa environment (sandbox or production)'
        )
        parser.add_argument(
            '--consumer-key',
            type=str,
            required=True,
            help='M-Pesa consumer key'
        )
        parser.add_argument(
            '--consumer-secret',
            type=str,
            required=True,
            help='M-Pesa consumer secret'
        )
        parser.add_argument(
            '--business-short-code',
            type=str,
            required=True,
            help='M-Pesa business short code (paybill/till number)'
        )
        parser.add_argument(
            '--passkey',
            type=str,
            help='M-Pesa passkey (for STK push)'
        )
        parser.add_argument(
            '--base-url',
            type=str,
            help='Base URL for callbacks (e.g., https://yourdomain.com)'
        )

    def handle(self, *args, **options):
        # Deactivate existing configurations
        MpesaConfiguration.objects.update(is_active=False)
        
        # Determine base URL
        base_url = options.get('base_url')
        if not base_url:
            # Try to get from Django settings
            if hasattr(settings, 'SITE_URL'):
                base_url = settings.SITE_URL
            else:
                base_url = 'https://yourdomain.com'
                self.stdout.write(
                    self.style.WARNING(
                        f'No base URL provided, using default: {base_url}'
                    )
                )
        
        # Create new configuration
        config = MpesaConfiguration.objects.create(
            environment=options['environment'],
            consumer_key=options['consumer_key'],
            consumer_secret=options['consumer_secret'],
            business_short_code=options['business_short_code'],
            passkey=options.get('passkey', ''),
            validation_url=f"{base_url}/payments/mpesa/validation/",
            confirmation_url=f"{base_url}/payments/mpesa/confirmation/",
            response_type='Completed',
            is_active=True
        )
        
        self.stdout.write(
            self.style.SUCCESS(
                f'Successfully created M-Pesa configuration for {config.environment} environment'
            )
        )
        
        self.stdout.write(
            self.style.SUCCESS(
                f'Validation URL: {config.validation_url}'
            )
        )
        
        self.stdout.write(
            self.style.SUCCESS(
                f'Confirmation URL: {config.confirmation_url}'
            )
        )
        
        self.stdout.write(
            self.style.WARNING(
                'Remember to register these URLs with M-Pesa using the configuration page or API'
            )
        )