from django.core.management.base import BaseCommand

from apps.business.distributor.services.ambassador_service import refresh_brand_ambassadors


class Command(BaseCommand):
    help = 'Refresh per-opportunity_bundle brand ambassador eligibility snapshots based on distributor ID counts.'

    def add_arguments(self, parser):
        parser.add_argument('--opportunity_bundle-id', type=str, default=None, help='Optional opportunity_bundle UUID to refresh only one opportunity_bundle.')
        parser.add_argument('--threshold', type=int, default=100, help='Minimum distributor ID count per opportunity_bundle to qualify.')

    def handle(self, *args, **options):
        threshold = options.get('threshold') or 100
        opportunity_bundle_id = options.get('opportunity_bundle_id')

        result = refresh_brand_ambassadors(min_distributor_ids=threshold, opportunity_bundle_id=opportunity_bundle_id)
        self.stdout.write(self.style.SUCCESS('Brand ambassador refresh completed.'))
        self.stdout.write(
            f"qualified={result['qualified_count']} created={result['created']} "
            f"updated={result['updated']} deactivated={result['deactivated']}"
        )
