from django.db import models
import os
import uuid
import requests
from django.contrib.auth.models import User
import pprint
from django.db.models.signals import post_save
from django.dispatch import receiver

# # Create your models here.
class File(models.Model):
    id_file = models.UUIDField(primary_key=True,default=uuid.uuid4,editable=False)
    file_title =  models.CharField(max_length=500,null=True,blank=True)
    video_path=models.CharField(max_length=500,null=True,blank=True)
    language = models.CharField(max_length=10,null=True,blank=True)
    user = models.ForeignKey(User , models.DO_NOTHING,blank=True, null=True)
    duration=models.CharField(max_length=50,null=True,blank=True)
    extension=models.CharField(max_length=50,null=True)
    when_uploaded=models.DateTimeField(auto_now_add=True)
    size=models.CharField(max_length=50,null=True,blank=True)
    multi_speaker=models.BooleanField(default=False)
    file_s3_url = models.CharField(max_length=600,null=True,blank=True)

    def __str__(self):
        return str(self.video_path)


class Transcripts(models.Model):
    id_transcript = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    file_org      = models.ForeignKey(File, on_delete=models.CASCADE,null=True,blank=True)
    path_srt_fr   = models.CharField(max_length=600,blank=True,null=True)
    path_srt_es   = models.CharField(max_length=600,blank=True,null=True)
    path_srt_en   = models.CharField(max_length=600,blank=True,null=True)
    path_srt_po   = models.CharField(max_length=600,blank=True,null=True)
    # downloaded_at = models.DateTimeField(null=True,blank=True)

    def __str__(self):
        return self.file_org.file_title if self.file_org else 'Transcript {}'.format(self.id_transcript)

# @receiver(post_save, sender=Transcripts)
# def save_vtt(sender,instance,created,**kwargs):
#     """
#     download srt and vtt files
#     """
    
#     if not kwargs.get('created', False):
#         print("Updating Transcript object ...")

#         VTT_STATIC_FILES = 'static/subtitles/vtt'
#         SRT_STATIC_FILES = 'static/subtitles/srt'
#         VTT_RESPONSE = {'file': instance.file_org.file_s3_url}
#         # get vtt for Transcript instance
#         INSTANCE_VTT = {
#             'fr': (str(instance.path_srt_fr).replace('.srt','.vtt') if  instance.path_srt_fr else None),
#             'es': (str(instance.path_srt_es).replace('.srt','.vtt') if  instance.path_srt_es else None),
#             'en': (str(instance.path_srt_en).replace('.srt','.vtt') if  instance.path_srt_en else None),
#             'po': (str(instance.path_srt_po).replace('.srt','.vtt') if  instance.path_srt_po else None)
#         }
#         # check vtt is downloaded or not and download it
#         for k, vtt in INSTANCE_VTT.items():
#             if not vtt:
#                 print(f"VTT not found for {vtt} in creating")
#                 continue
#             vtt_path = os.path.join(VTT_STATIC_FILES, f"{os.path.splitext(os.path.basename(vtt))[0]}.{k}.vtt")
#             # check vtt is downloaded or not and download it
#             if os.path.exists(os.path.join(VTT_STATIC_FILES,vtt_path)):
#                 VTT_RESPONSE[k] = vtt_path
#                 print("VTT Path Exists: ",vtt_path)
#             else:
#                 import time
#                 srt = vtt.replace('.vtt','.srt')
#                 srt_path = os.path.join(SRT_STATIC_FILES, f"{os.path.splitext(os.path.basename(vtt))[0]}.srt")
#                 time.sleep(0.2)
#                 os.system(f'python3 cap_download.py --vtt={vtt} --dir={vtt_path}')
#                 time.sleep(0.4)
#                 os.system(f'python3 cap_download.py --vtt={srt} --dir={srt_path}')
#                 time.sleep(0.4)
                
#         # update instance to static files
#         # for k_1,caption in INSTANCE_VTT:
#             # instance.path_srt_fr = os.path.join(SRT_STATIC_FILES, f"{os.path.splitext(os.path.basename(caption))[0]}.srt")
#             # instance.path_srt_es = os.path.join(SRT_STATIC_FILES, f"{os.path.splitext(os.path.basename(caption))[0]}.srt")
#             # instance.path_srt_en = os.path.join(SRT_STATIC_FILES, f"{os.path.splitext(os.path.basename(caption))[0]}.srt")
#             # instance.path_srt_po = os.path.join(SRT_STATIC_FILES, f"{os.path.splitext(os.path.basename(caption))[0]}.srt")
        

        
        