from decimal import Decimal

from django.core.management.base import BaseCommand, CommandError

from apps.business.distributor.services.system_validation_service import FullSystemValidationService


class Command(BaseCommand):
    help = 'Run full system validation, simulation, and business acceptance testing for the strict Hybrid Global Assisted Binary system.'

    def add_arguments(self, parser):
        parser.add_argument('--small', type=int, default=100, help='Small network size (default: 100)')
        parser.add_argument('--medium', type=int, default=1000, help='Medium network size (default: 1000)')
        parser.add_argument('--large', type=int, default=10000, help='Large network size (default: 10000)')
        parser.add_argument('--special-mobile', type=str, default='8668192080', help='Special test mobile number')
        parser.add_argument('--special-main-balance', type=str, default='150000.00', help='Minimum main wallet test balance for the special user')
        parser.add_argument('--special-super-balance', type=str, default='150000.00', help='Minimum super coin balance for the special user')
        parser.add_argument('--task-mode', type=str, default='sync', choices=['sync', 'async', 'auto'], help='Task execution mode: sync (deterministic), async (background queue), auto (sync on sqlite / async otherwise)')

    def handle(self, *args, **options):
        try:
            service = FullSystemValidationService(
                scales=[
                    ('small', int(options['small'])),
                    ('medium', int(options['medium'])),
                    ('large', int(options['large'])),
                ],
                output_root='c:/Users/DN761673/Downloads/TWM_New/truewave-platform',
                special_mobile=options['special_mobile'],
                special_main_balance=Decimal(str(options['special_main_balance'])),
                special_super_balance=Decimal(str(options['special_super_balance'])),
                task_mode=options['task_mode'],
            )
            result = service.run()
        except Exception as exc:
            raise CommandError(f'Full system validation failed: {exc}') from exc

        self.stdout.write(self.style.SUCCESS('Full system validation completed.'))
        self.stdout.write(f"Run tag: {result['run_tag']}")
        self.stdout.write(f"Report dir: {result['report_dir']}")
        self.stdout.write(f"Production readiness score: {result['production_readiness_score']}/100")