import logging

from django.core.management.base import BaseCommand
from django.db import models, transaction

logger = logging.getLogger(__name__)


class Command(BaseCommand):
    help = "Enrich YouTube and Twitch channel metadata from upstream APIs"

    def add_arguments(self, parser):
        parser.add_argument(
            "--force",
            action="store_true",
            help="Overwrite existing channel name and cover image",
        )
        parser.add_argument(
            "--dry-run",
            action="store_true",
            help="Show what would be done without making changes",
        )
        parser.add_argument(
            "--youtube-only",
            action="store_true",
            help="Only process channels with a youtube_id",
        )
        parser.add_argument(
            "--twitch-only",
            action="store_true",
            help="Only process channels with a twitch_id",
        )

    def handle(self, *args, **options):
        from videos.models import Channel

        force = options["force"]
        dry_run = options["dry_run"]
        youtube_only = options["youtube_only"]
        twitch_only = options["twitch_only"]

        qs = Channel.objects.all()

        if youtube_only:
            qs = qs.exclude(youtube_id__isnull=True).exclude(youtube_id="")
        elif twitch_only:
            qs = qs.exclude(twitch_id__isnull=True).exclude(twitch_id="")
        else:
            qs = qs.filter(
                models.Q(youtube_id__isnull=False) | models.Q(twitch_id__isnull=False)
            ).exclude(youtube_id="", twitch_id="")

        total = qs.count()
        self.stdout.write(f"Processing {total} channels")

        if dry_run:
            for channel in qs.iterator():
                source = "youtube" if channel.youtube_id else "twitch"
                identifier = channel.youtube_id or channel.twitch_id
                self.stdout.write(
                    f"  [DRY RUN] Would fix {channel.name} ({source}: {identifier})"
                )
            return

        updated = 0
        errors = 0
        for channel in qs.iterator():
            try:
                with transaction.atomic():
                    channel.fix_metadata(force=force)
                updated += 1
                source = "youtube" if channel.youtube_id else "twitch"
                self.stdout.write(f"  [{updated}/{total}] {channel.name} ({source})")
            except Exception as e:
                errors += 1
                self.stdout.write(
                    self.style.ERROR(f"  Error updating channel {channel.name}: {e}")
                )

        self.stdout.write(
            self.style.SUCCESS(f"\nDone! {updated} channels updated, {errors} errors")
        )
