import requests
import logging

from django.conf import settings
from django.contrib.contenttypes.models import ContentType

from .models import Video, Playlist, TagToObject, READY, DYNAMIC, FAIL
from .s3_uploader import upload_file
from .utils import asset_upload

from cp_video.celery import app


video_content_type = ContentType.objects.get_for_model(Video)
playlist_content_type = ContentType.objects.get_for_model(Playlist)

# video_content_type = 'core | Video'
# playlist_content_type = 'core | Playlist'

log = logging.getLogger('send_video_to_s3')


@app.task(soft_time_limit=600)
def send_video_to_s3(video_id=None):
    local_video = Video.objects.get(id=video_id)

    try:
        log.info(f'Uploading video id {local_video.id} to S3...')

        video_name = local_video.local_file.name.split('/')[-1]
        key = asset_upload(local_video, video_name)
        file_path = settings.DOMAIN_NAME + settings.MEDIA_URL + key
        log.info(f'File path: {file_path}')

        response = requests.get(file_path)
        content = response.content

        local_video.s3_file = upload_file(content, key, 'video/mp4')
        local_video.status = READY
        local_video.local_file = None

        local_video.save()

        log.info(f'Video id {local_video.id} uploaded to S3...')

        tag_ids = list(TagToObject.objects.filter(
            content_type=video_content_type,
            object_id=video_id,
        ).values_list('tag_id', flat=True))

        if tag_ids:
            playlist_ids = list(TagToObject.objects.filter(
                content_type=playlist_content_type,
                tag_id__in=tag_ids,
            ).values_list('object_id', flat=True))

            if playlist_ids:
                playlists = Playlist.objects.filter(
                    id__in=playlist_ids,
                    type=DYNAMIC,
                )

                for playlist in playlists:
                    playlist.videos.add(local_video)
    except Exception as e:
        log.error(e)
        local_video.status = FAIL
        local_video.save(update_fields=['status'])
        log.info(f'Video {video_id} upload failed!')