from django.core.management.base import BaseCommand

from sports.models import League, Team
from sports.thesportsdb import enrich_league_logo, enrich_team_logo, has_logo


class Command(BaseCommand):
    help = "Fetch missing league and team logos from TheSportsDB"

    def add_arguments(self, parser):
        parser.add_argument(
            "--dry-run",
            action="store_true",
            help="Show what would be done without making changes",
        )

    def handle(self, *args, **options):
        dry_run = options["dry_run"]

        leagues = League.objects.filter(thesportsdb_id__isnull=False)
        for league in leagues:
            if has_logo(league):
                continue
            if dry_run:
                self.stdout.write(f"Would enrich logo for league: {league.name} ({league.thesportsdb_id})")
            else:
                enrich_league_logo(league)

        teams = Team.objects.filter(thesportsdb_id__isnull=False)
        for team in teams:
            if has_logo(team):
                continue
            if dry_run:
                self.stdout.write(f"Would enrich logo for team: {team.name} ({team.thesportsdb_id})")
            else:
                enrich_team_logo(team)
