import logging

from videos.models import Video
from django.db import IntegrityError

# from videos.skatevideosite import lookup_video_from_skatevideosite

logger = logging.getLogger(__name__)


def clean_up_videos():
    videos = Video.objects.filter(imdb_id__isnull=False).exclude(
        imdb_id__icontains="tt"
    )

    for video in videos:
        logger.info(f"Fixing imdb_id for {video}")
        video.imdb_id = "tt" + video.imdb_id
        try:
            video.save(update_fields=["imdb_id"])
        except IntegrityError:
            new_video = Video.objects.filter(imdb_id="tt" + video.imdb_id).first()
            video.scrobble_set.all().update(video=new_video)
            video.delete()

    videos = Video.objects.filter(scrobble__isnull=True)
    videos.delete()
