from django.core.management.base import BaseCommand


class Command(BaseCommand):
    help = "Fix duplicate empty imdb_id values in Video model"

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

        empty_imdb = Video.objects.filter(imdb_id__isnull=False, imdb_id="")
        if not empty_imdb.exists():
            self.stdout.write("No videos with empty imdb_id found")
            return

        self.stdout.write(f"Found {empty_imdb.count()} videos with empty imdb_id")

        for video in empty_imdb:
            self.stdout.write(
                f"  Setting imdb_id to NULL for video id={video.id}: {video.title}"
            )
            video.imdb_id = None
            video.save(update_fields=["imdb_id"])

        self.stdout.write(self.style.SUCCESS("Done!"))
