import pytz,mimetypes,tempfile,requests
import pandas as pd
import boto3 , json , shutil, os
from io import BytesIO
from users.models import UserCredentialsForBlockchain
from Blockchain2.decryption import decryptionOfPrivate,decryptionOflocalUrl,hash_decrypation
from Blockchain2.DataStorage import uploadDataToIPFSNode
from Blockchain2.scriptAudit import getUserprojectIds,UploadScriptAuditData,getScriptAudit
from Blockchain2.web3User import mypublickey
from Blockchain2.blockchainsetting import OWNER_KEY
from django.core import exceptions as django_exception
from django.http import FileResponse, JsonResponse
from django.core.files.base import File as DjangoFile
from django.core.files.base import ContentFile
from django.shortcuts import redirect, render
from rest_framework import status
from rest_framework.authentication import BasicAuthentication, SessionAuthentication
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView
from .utils import update_audit_status
from utils.scripts_functions import script_id_generator
from centralisedFileSystem.models import Script,File, ScreenPlay
from scriptAudit.mnf_script_audit import NeutralAudit
from utils import filesystem,utilities
from datetime import datetime
from .forms import AuditForm
from .models import ScriptAuditModel, States
from scriptAudit import exceptions as spex
from django.contrib.sites.shortcuts import get_current_site
from lpp.certificate.createCertificate import certificateGenrate
from auto_email.views import sendmail
from MNF import settings
from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.auth.models import User



class Get_Counter(LoginRequiredMixin,APIView):
    login_url = "/personal-login"
    authentication_classes = [BasicAuthentication,SessionAuthentication]
    permission_classes = [IsAuthenticated]
    def get(self, request):
        return render(request, "audit/audit_AWS_home.html")
    
    def post(self, request):
        current_site = str(get_current_site(request).domain)
        print("CURREENT DOMAIN :-----")
        print(current_site)
        running_in_production = True
        testing_on_dev = False
        if current_site in ["http://1.6.141.108", 
                            "http://1.6.141.104", 
                            "http://1.6.141.103",
                            "http://1.6.141.106",
                            "https://taj.mynextfilm.in",
                            "1.6.141.108",
                            "1.6.141.104",
                            "1.6.141.103",
                            "1.6.141.106",
                            "taj.mynextfilm.in",
                            # "qa.mynextfilm.net",
                            # "https://qa.mynextfilm.net",
                            ]:
            running_in_production = False

        if current_site in ["qa.mynextfilm.net",
                            "https://qa.mynextfilm.net",
                            ]:
            testing_on_dev = True

        session = boto3.Session(
                    aws_access_key_id='AKIAQVLBBGCB45RMLKVW',
                    aws_secret_access_key='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',
                )   
        sqs = session.resource('sqs', region_name='ap-south-1')

        if testing_on_dev == True:
            print("#######\n\n\n")
            print("Sending files to Development server\n\n\n\n")
            queue = sqs.get_queue_by_name(QueueName="devqueue")
        else:
            queue = sqs.get_queue_by_name(QueueName="mnfqueue")



        user = str(request.user)
        screenplay_name = request.data.get('screenplay_name')
        author = request.data.get('author_name')
        language = request.data.get('language')
        script_file = request.data.get('script_file')
        script_ext = script_file.name.split(".")[-1]
        script_file_name = screenplay_name + "." + script_ext
        
        media_path = os.path.join(settings.MEDIA_ROOT,"audit_counter_files", script_file_name)
        with open(media_path, 'wb') as destination:
            for chunk in script_file.chunks():
                destination.write(chunk)
                
        object_name = "INPUT/" + script_file_name
        filee = media_path
        bucket = "conversion-kitchen"
        s3_client = boto3.client('s3',
                                 aws_access_key_id="AKIAQVLBBGCB45RMLKVW",
                                 aws_secret_access_key="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",
                                 region_name="ap-south-1"
                                 )
        if running_in_production == True:
            try:
                response = s3_client.upload_file(filee, bucket, object_name)
                print(response)
            except Exception as e:
                print("Error is", e)
            print("#######\n\n\n")
            print("Sending files to Production server\n\n\n\n")
            s3_url = f"https://{bucket}.s3.ap-south-1.amazonaws.com/{object_name}"   
        else:
            print("#######\n\n\n")
            print("Sending files to Local server\n\n\n\n")
            s3_url = media_path
        audit_parameters = {
            "service_type" : "audit",
            "user":user,
            "s3-file-path": s3_url,
            "screenplay_name": screenplay_name,
            "author": author,
            "language": language,
            "script_ext": script_ext,
            "script_file_name": script_file_name
        }
        key_value = {"script_name" : screenplay_name}
        to_email = [request.user.email]
        email_code = 'SB14'
        sendmail(to_email=to_email, email_code=email_code ,key_value = key_value)
        if running_in_production != True:
            print("RUNNING AUDIT LOCALLY")
            print(audit_parameters)
            run_audit_in_counter(audit_parameters)
            return JsonResponse({"response": audit_parameters})
        else:
            response = queue.send_message(MessageBody=json.dumps(audit_parameters),)
            return JsonResponse({"response": response})


def run_audit_in_counter(msg):
    user = msg["user"]
    s3_url = msg["s3-file-path"]
    screenplay_name = msg["screenplay_name"]
    author = msg["author"]
    language = msg["language"]
    script_ext = msg["script_ext"]
    script_file_name = msg["script_file_name"]
    language = "en"
    local_file_path = s3_url

    
    with open(local_file_path, 'rb') as file:
        file_content = file.read()
    
    file = ContentFile(
                file_content,
                script_file_name,
        )
    
    user = User.objects.get(username=user)
        
    result = filesystem.new_screenplay_without_audit_in_background(
        user,
        author,
        screenplay_name,
        file,
        "script-original",
        language,
    )
    

    script_id = result.get("script", {}).get("id")
        
    file_to_original = File.objects.get(
        script=script_id,
        type="script-original"

    )
    try:
        update_audit_status(script_id, States.STARTED)
    except:
        update_audit_status(script_id, States.FAILURE)
    #update_only_audit
    audit_only =  ScriptAuditModel.objects.get(
                script = Script.objects.get(
                            id = script_id
                        ))
    audit_only.only_audit = True
    audit_only.save()

    try:
        
        audit = NeutralAudit(script_id)
        audit.audit_in_background()
        ScriptAuditModel.objects.update_or_create(
        script = Script.objects.get(
            id = script_id
        ),
        defaults={"status" : "SUCCESS"}
    )
        
        
        
    except Exception as exp:
        ScriptAuditModel.objects.update_or_create(script = Script.objects.get(id = script_id),
        defaults={"status" : "FAILURE",
                  "results" : exp})
        key_value = {"script_name" : screenplay_name,}
        to_email = [user.email]
        email_code = 'SB2'
        sendmail(to_email=to_email, email_code=email_code ,key_value = key_value)
        
    status =  ScriptAuditModel.objects.get(
                    script = Script.objects.get(
                                id = script_id
                            ))

    blockchain_obj = UserCredentialsForBlockchain.objects.get(user=user)
    script_original= {}
    audit_data={}
    script_original["status"] = "STARTED"
    script_original["script_id"] = script_id
    with open(file_to_original.file.path, 'rb') as file:
        hash = uploadDataToIPFSNode(file)
        script_original["script_file"] = hash
    script_original["type"] = "script-original"
    script_original["script_file_path"] = file_to_original.file.path
    audit_data["script-original"] = script_original
    userkey= decryptionOfPrivate(blockchain_obj.privateKey)
    print("userkey = ", str(userkey))
    print("blockchain_obj.publicKey",blockchain_obj.publicKey)
    print("blockchain_obj.privateKey",blockchain_obj.privateKey)
    if status.status == "SUCCESS":

        if script_ext == "fdx":

            file_to_audit = File.objects.get(
                script=script_id,
                type="script-csv"
            )
            hash2 = ""
            hash2_docx = ""
            try:
                file_to_audit_docx = File.objects.get(
                    script=script_id,
                    type="script-docx"
                )
                script_docx = {}
                script_path1 = file_to_audit_docx.file.path
                with open(script_path1, 'rb') as _file:
                    hash2_docx = uploadDataToIPFSNode(_file)
                script_docx["script_file_path"] = script_path1
                script_docx["script_file"] = hash2_docx
                script_docx["type"] = "script-docx"
                audit_data["script-docx"] = script_docx
            except:
                csv_script_path = file_to_audit.file.path  
                df = pd.read_csv(csv_script_path)
                docx = utilities.csv_to_docx(df)

                temp_file_stream = BytesIO()
                docx.save(temp_file_stream)
                temp_file_stream.seek(0)

                docx_file = ContentFile(
                    temp_file_stream.getvalue(),
                    "from_audited_csv_to_document.docx",
                )
                
                query_file = File.objects.create(
                    script= file_to_audit.script,
                    file=docx_file,
                    type="script-docx",
                )
                file_to_audit_docx = File.objects.get(
                    script=script_id,
                    type="script-docx"
                )
                script_docx = {}
                script_path1 = file_to_audit_docx.file.path
                script_size = file_to_audit_docx.file.size
                with open(script_path1, 'rb') as _file:
                    hash2_docx = uploadDataToIPFSNode(_file)
                script_docx["script_file_path"] = script_path1
                script_docx["script_file"] = hash2_docx
                script_docx["type"] = "script-docx"
                audit_data["script-docx"] = script_docx
                
            ## code for pdf also  
            try:
                temp_dir = tempfile.TemporaryDirectory()
                pdf_file_path = utilities.docx_to_pdf(
                script_path1, temp_dir.name)
                
                with open(pdf_file_path, "rb") as temp_pdf:

                    pdf_file = DjangoFile(temp_pdf,pdf_file_path.rsplit('/', 1)[1],)

                    query_file = File.objects.create(
                        script = file_to_audit.script,
                        file = pdf_file,
                        type="script-pdf",
                    )
                script_pdf = {}
                script_path1 = pdf_file_path
                # script_size = file_to_audit_docx.file.size
                with open(script_path1, 'rb') as _file:
                    hash2 = uploadDataToIPFSNode(_file)
                script_pdf["script_file_path"] = script_path1
                script_pdf["script_file"] = hash2
                script_pdf["type"] = "script-pdf"
                audit_data["script-pdf"] = script_pdf
            except:
                pass
            # convert csv to json and store JSON 
            try:
                csv_script_path = file_to_audit.file.path  
                print("csv_path fetched",csv_script_path)
                df = pd.read_csv(csv_script_path)
                df = df.loc[:, ["content", "script_element"]]
                script_json: dict = json.loads(utilities.csv_to_json(df))
                with tempfile.TemporaryDirectory() as temp_dir:
                    print("Temporary directory created:", temp_dir)
                    temp_filename = os.path.join(temp_dir, 'script_json_file.json')
                    print("temp file name ----------------?>",temp_filename)
                    with open(temp_filename, 'w') as json_file:
                        json.dump(script_json, json_file, indent=4)
                    script_json = {}
                    script_path1 = temp_filename
                    # script_size = file_to_audit_docx.file.size
                    with open(script_path1, 'rb') as _file:
                        hash2 = uploadDataToIPFSNode(_file)
                    script_json["script_file_path"] = script_path1
                    script_json["script_file"] = hash2
                    script_json["type"] = "script-json"
                    script_json["screenplay_name"] = str(screenplay_name)
                    audit_data["script-json"] = script_json
                    print("data_uploaded")
            except Exception as exp:
                print("###ERROR:",exp)
                print("######Error from JSON CREATION############")
            try:
                script_csv = {}
                script_path = file_to_audit.file.path
                print("script_file_path_is_here",script_path)
                with open(script_path, 'rb') as _file:
                    hash1 = uploadDataToIPFSNode(_file)
                    script_csv["script_file"] = hash1
                script_csv["script_file_path"] = script_path
                script_csv["type"] = "script-csv"
                audit_data["script-csv"]= script_csv
            except Exception as exp:
                print(exp)
            Response,gasprice = UploadScriptAuditData(OWNER_KEY,blockchain_obj.publicKey,blockchain_obj.user_id,script_id,str(audit_data))
            print("tx_hash",Response)
            transactioni_id = str(Response)
            status.transaction_hash =str(transactioni_id)
            status.save()
            to_email = [user.email]
            print("####### #### to_email",to_email)
            key_value_aud = { "script_name" : str(screenplay_name) }
            email_code = 'SB1'
            sendmail(to_email=to_email , email_code=email_code, key_value = key_value_aud )
            print("$$$$### after sendmail")
            # user_infos =  user_info(tx_hash=Response,service="Script Audit",gas_fee=gasprice)
            # addition_result = user_infos.update_info(request)
            hash2_docx = hash_decrypation(hash2_docx)
            tx_id = Response
            certificate = certificateGenrate(user.username,"script audit",tx_id,projectname=script_file_name,matic=gasprice)
            to_email = [user.email]
            email_code = 'BL1'
            key_value = {
                "service":"Audited Script",
                "hash": hash2_docx,
                "public key":blockchain_obj.publicKey,
                "Transaction Hash": tx_id,
            }
            print("userkey = ", userkey)
            sendmail(to_email=to_email , email_code=email_code, key_value=key_value, filePath=certificate)
            print("mail send sucessfully:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
            data = {"message": "Success"}
            
        else:

            file_to_audit = File.objects.get(
                script=script_id,
                type="script-csv"
            )
            file_to_audit_report = File.objects.get(
                script=script_id,
                type="audit-report"
            )
            hash2 = ""
            hash2_docx = ""
            try:
                file_to_audit_docx = File.objects.get(
                    script=script_id,
                    type="script-docx"
                )
                script_docx = {}
                script_path1 = file_to_audit_docx.file.path
                with open(script_path1, 'rb') as _file:
                    hash2_docx = uploadDataToIPFSNode(_file)
                script_docx["script_file_path"] = script_path1
                script_docx["script_file"] = hash2_docx
                script_docx["type"] = "script-docx"
                audit_data["script-docx"] = script_docx
            except:
                csv_script_path = file_to_audit.file.path  
                df = pd.read_csv(csv_script_path)
                docx = utilities.csv_to_docx(df)

                temp_file_stream = BytesIO()
                docx.save(temp_file_stream)
                temp_file_stream.seek(0)

                docx_file = ContentFile(
                    temp_file_stream.getvalue(),
                    "from_audited_csv_to_document.docx",
                )
                
                query_file = File.objects.create(
                    script= file_to_audit.script,
                    file=docx_file,
                    type="script-docx",
                )
                file_to_audit_docx = File.objects.get(
                    script=script_id,
                    type="script-docx"
                )
                script_docx = {}
                script_path1 = file_to_audit_docx.file.path
                script_size = file_to_audit_docx.file.size
                with open(script_path1, 'rb') as _file:
                    hash2_docx = uploadDataToIPFSNode(_file)
                script_docx["script_file_path"] = script_path1
                script_docx["script_file"] = hash2_docx
                script_docx["type"] = "script-docx"
                audit_data["script-docx"] = script_docx
                
            ## code for pdf also  
            try:
                temp_dir = tempfile.TemporaryDirectory()
                pdf_file_path = utilities.docx_to_pdf(
                script_path1, temp_dir.name)
                
                with open(pdf_file_path, "rb") as temp_pdf:

                    pdf_file = DjangoFile(temp_pdf,pdf_file_path.rsplit('/', 1)[1],)

                    query_file = File.objects.create(
                        script = file_to_audit.script,
                        file = pdf_file,
                        type="script-pdf",
                    )
                script_pdf = {}
                script_path1 = pdf_file_path
                # script_size = file_to_audit_docx.file.size
                with open(script_path1, 'rb') as _file:
                    hash2 = uploadDataToIPFSNode(_file)
                script_pdf["script_file_path"] = script_path1
                script_pdf["script_file"] = hash2
                script_pdf["type"] = "script-pdf"
                audit_data["script-pdf"] = script_pdf
            except:
                pass
            # convert csv to json and store JSON 
            try:
                csv_script_path = file_to_audit.file.path  
                df = pd.read_csv(csv_script_path)
                df = df.loc[:, ["content", "script_element"]]
                script_json: dict = json.loads(utilities.csv_to_json(df))
                with tempfile.TemporaryDirectory() as temp_dir:
                    print("Temporary directory created:", temp_dir)
                    temp_filename = os.path.join(temp_dir, 'script_json_file.json')
                    print("temp file name ----------------?>",temp_filename)
                    with open(temp_filename, 'w') as json_file:
                        json.dump(script_json, json_file, indent=4)
                    script_json = {}
                    script_path1 = temp_filename
                    # script_size = file_to_audit_docx.file.size
                    with open(script_path1, 'rb') as _file:
                        hash2 = uploadDataToIPFSNode(_file)
                    script_json["script_file_path"] = script_path1
                    script_json["script_file"] = hash2
                    script_json["type"] = "script-json"
                    script_json["screenplay_name"] = str(screenplay_name)
                    audit_data["script-json"] = script_json
                    print("data_uploaded")
            except Exception as exp:
                print("###ERROR:",exp)
                print("######Error from JSON CREATION############")
                pass
            
            script_csv = {}
            audit_report ={}
            audit_report_path = file_to_audit_report.file.path
            script_path = file_to_audit.file.path
            script_size = file_to_audit.file.size

            print("script_file_path_is_here",script_path)
            with open(script_path, 'rb') as _file:
                hash1 = uploadDataToIPFSNode(_file)
                script_csv["script_file"] = hash1
            script_csv["script_file_path"] = script_path
            script_csv["type"] = "script-csv"
            with open(audit_report_path, 'rb') as file1:
                hash2 = uploadDataToIPFSNode(file1)
                audit_report["script_file"] = hash2
            audit_report["script_file_path"] = audit_report_path
            audit_report["type"] = "audit-report"
            audit_data["script-csv"]= script_csv
            audit_data["audit-report"]= audit_report
            
            
            Response,gasprice = UploadScriptAuditData(OWNER_KEY,blockchain_obj.publicKey,blockchain_obj.user_id,script_id,str(audit_data))
            print("tx_hash",Response)
            transactioni_id = str(Response)
            status.transaction_hash =str(transactioni_id)
            status.save()

            to_email = [user.email]
            email_code = 'SB1'
            key_value_aud = { "script_name" : str(screenplay_name)}
            sendmail(to_email=to_email , email_code=email_code, key_value = key_value_aud)

            # user_infos =  user_info(tx_hash=Response,service="Script Audit",gas_fee=gasprice)
            # addition_result = user_infos.update_info(request)
            hash2_docx = hash_decrypation(hash2_docx)
            tx_id = Response
            certificate = certificateGenrate(user.username,"script audit",tx_id,projectname=script_file_name,matic=gasprice)
            to_email = [user.email]
            email_code = 'BL1'
            key_value = {
                "service":"Audited Script",
                "hash": hash2_docx,
                "public key":blockchain_obj.publicKey,
                "Transaction Hash": tx_id,
            }
            print("userkey = ", userkey)
            sendmail(to_email=to_email , email_code=email_code, key_value=key_value, filePath=certificate)
            print("mail send sucessfully:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
            data = {"message": "Success"}
            # deleting the folder and files 
            
            #  Delete a file
            # media_path = os.path.join(settings.MEDIA_ROOT, "audit_counter_files", script_file_name)
            # if os.path.exists(media_path):
            #     os.remove(media_path)
            #     print(f"File '{media_path}' deleted successfully.")
            # else:
            #     print(f"File '{media_path}' does not exist.")

            # # Delete a folder and its contents
            # folder1_path = os.path.join(settings.MEDIA_ROOT, "scripts_folder", script_id)
            # if os.path.exists(folder1_path):
            #     shutil.rmtree(folder1_path)
            #     print(f"Folder '{folder1_path}' and its contents deleted successfully.")
            # else:
            #     print(f"Folder '{folder1_path}' does not exist.")

            # folder2_path = os.path.join(settings.MEDIA_ROOT, "audit_folder", script_id)
            # if os.path.exists(folder2_path):
            #     shutil.rmtree(folder2_path)
            #     print(f"Folder '{folder2_path}' and its contents deleted successfully.")
            # else:
            #     print(f"Folder '{folder2_path}' does not exist.")
            return JsonResponse(data, status=200)
            # return Response("Success", status=200)
        

    else:
        
        to_email = [user.email]
        email_code = 'SB2'
        key_value_aud = { "script_name" : str(screenplay_name)}
        sendmail(to_email=to_email , email_code=email_code, key_value = key_value_aud )

        Response = UploadScriptAuditData(OWNER_KEY,blockchain_obj.publicKey,blockchain_obj.user_id,script_id,str(audit_data))
        print("tx_hash",Response)
        hash = hash_decrypation(hash)
        #certificate = certificateGenrate(request.user.username,"script audit",hash)
        tx_id = Response

        # certificate = certificateGenrate(request.user.username,"script audit",tx_id,projectname=script_file_name,matic=gasprice)

        to_email = [user.email]
        email_code = 'BL1'
        key_value = {
            "service":"Orginal Script Audit",
            "hash": hash,
            "public key":blockchain_obj.publicKey,
            "private key":userkey,
            "Transaction Hash": tx_id,
        }
        certificate = certificateGenrate(user.username,"script audit",tx_id,projectname=script_file_name,matic=gasprice)
        sendmail(to_email=to_email , email_code=email_code, key_value=key_value, filePath=certificate)

        print("::::::::::::::",key_value)
        print("userkey = ", userkey)


"""
# # KITCHEN ACCEPT CODE

def run_audit(msg):
    body_dict = json.loads(msg.body)
    msg.delete()
    user = body_dict.get("user")
    s3_url = body_dict.get("s3-file-path")
    screenplay_name = body_dict.get("screenplay_name")
    author = body_dict.get("author")
    language = body_dict.get("language")
    script_ext = body_dict.get("script_ext")
    script_file_name = body_dict.get("script_file_name")
    language = "en"
    print("112")
    object_key = "INPUT/" + str(script_file_name)
    local_file_path = "/home/ubuntu/Conversion_Kitchen_Code/kitchen_counter/media/audit_counter_files/" + script_file_name
    s3_client = boto3.client('s3',
                        aws_access_key_id="AKIAQVLBBGCB45RMLKVW",
                        aws_secret_access_key="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",
                        region_name="ap-south-1"
                        )
    print(object_key, local_file_path)
    s3_client.download_file("conversion-kitchen", object_key, local_file_path)
    audit_parameters = {
            "service_type" : "audit",
            "user":user,
            "s3-file-path": local_file_path,
            "screenplay_name": screenplay_name,
            "author": author,
            "language": language,
            "script_ext": script_ext,
            "script_file_name": script_file_name
        }
    run_audit_in_counter(audit_parameters)    
"""

class AuditedScriptsView_Without_blockchain(LoginRequiredMixin, APIView):
    login_url = "/personal-login"
    authentication_classes = [BasicAuthentication,SessionAuthentication]
    permission_classes = [IsAuthenticated]
    def get(self, request):
        display_list = []
        scripts = Script.objects.filter(screenplay__user=request.user).order_by('-created_on')
        

        for script in scripts:

            if ScriptAuditModel.objects.filter(script=script).exists():
                # audit_status_objects = AuditStatus.objects.filter(script=str(script.id))
                audit_status_objects = ScriptAuditModel.objects.filter(script=script)
                for audit_status_object in audit_status_objects:
                    script_audit_status = audit_status_object.status
                    if script_audit_status == States.SUCCESS:
                        extimated_time = audit_status_object.expected_duration
                        no_of_pages = audit_status_object.number_of_pages
                        screenplay_language = audit_status_object.screenplay_language
                        dialogue_language = audit_status_object.dialogue_language
                        isfdx = audit_status_object.isfdx
                        # transaction_haash = audit_status_object.transaction_hash
                        # privatekeycnf = audit_status_object.bchain_privatekey
                        privatekeycnf = "dhjdjfhd"
                        print("SCRIPT ID = ", str(script.id))
                        print("extimated time = ", extimated_time)
                        print("isfdx", isfdx)
                        print("no of pages = ", no_of_pages)
                        print("screenplay language = ", screenplay_language)
                        print("dialogue_language =", dialogue_language)
                        # print("transaction hash = ", transaction_haash)
                        print(" private key = ", privatekeycnf)
                        display_list.append(
                            {
                                "scriptid": str(script.id),
                                "scriptName": str(script.screenplay.name),
                                "author": str(script.screenplay.author),
                                "iscomplete": "S",
                                "created_on" : str(script.created_on.astimezone(pytz.timezone('Asia/Kolkata')).strftime("%B %d, %Y %I:%M %p")),
                                "extimated_time": str(extimated_time),
                                "screenplay_language": str(screenplay_language),
                                "dialogue_language": str(dialogue_language),
                                "page_number" : str(no_of_pages),
                                "transaction_hash" : str(1),
                                "confirmkey": str(privatekeycnf),
                                "isfdx" : str(isfdx)
                            }
                        )
                    elif script_audit_status == States.FAILURE:
                        display_list.append(
                            {
                                "scriptid": str(script.id),
                                "scriptName": str(script.screenplay.name),
                                "author": str(script.screenplay.author),
                                "iscomplete": "F",
                                "created_on" : str(script.created_on.astimezone(pytz.timezone('Asia/Kolkata')).strftime("%B %d, %Y %I:%M %p")),
                            }
                        )
                    elif script_audit_status == States.STARTED:
                        extimated_time = audit_status_object.expected_duration
                        no_of_pages = audit_status_object.number_of_pages
                        display_list.append(
                            {
                                "scriptid": str(script.id),
                                "scriptName": str(script.screenplay.name),
                                "author": str(script.screenplay.author),
                                "iscomplete": "R",
                                "created_on" : str(script.created_on.astimezone(pytz.timezone('Asia/Kolkata')).strftime("%B %d, %Y %I:%M %p")),
                                "extimated_time": str(extimated_time),
                            }
                        )
                    elif script_audit_status == States.PENDING:
                        continue
        url = get_current_site(request).domain
        return render(
            request,
            "audit/audited_copy.html",
            {"i": display_list, "url": url},
        )
        
   
class AuditedScriptsView_With_Blockchain(LoginRequiredMixin, APIView):
    login_url = "/personal-login"
    authentication_classes = [BasicAuthentication,SessionAuthentication]
    permission_classes = [IsAuthenticated]
    def get(self, request):
        display_list = []
        scripts = Script.objects.filter(screenplay__user=request.user).order_by('-created_on')
        

        for script in scripts:

            if ScriptAuditModel.objects.filter(script=script).exists():
                # audit_status_objects = AuditStatus.objects.filter(script=str(script.id))
                audit_status_objects = ScriptAuditModel.objects.filter(script=script)
                for audit_status_object in audit_status_objects:
                    script_audit_status = audit_status_object.status
                    if script_audit_status == States.SUCCESS and audit_status_object.only_audit == True:
                        extimated_time = audit_status_object.expected_duration
                        no_of_pages = audit_status_object.number_of_pages
                        screenplay_language = audit_status_object.screenplay_language
                        dialogue_language = audit_status_object.dialogue_language
                        isfdx = audit_status_object.isfdx
                        # transaction_haash = audit_status_object.transaction_hash
                        # privatekeycnf = audit_status_object.bchain_privatekey
                        privatekeycnf = "dhjdjfhd"
                        print("SCRIPT ID = ", str(script.id))
                        print("extimated time = ", extimated_time)
                        print("isfdx", isfdx)
                        print("no of pages = ", no_of_pages)
                        print("screenplay language = ", screenplay_language)
                        print("dialogue_language =", dialogue_language)
                        # print("transaction hash = ", transaction_haash)
                        print(" private key = ", privatekeycnf)
                        display_list.append(
                            {
                                "scriptid": str(script.id),
                                "scriptName": str(script.screenplay.name),
                                "author": str(script.screenplay.author),
                                "iscomplete": "S",
                                "created_on" : str(script.created_on.astimezone(pytz.timezone('Asia/Kolkata')).strftime("%B %d, %Y %I:%M %p")),
                                "extimated_time": str(extimated_time),
                                "screenplay_language": str(screenplay_language),
                                "dialogue_language": str(dialogue_language),
                                "page_number" : str(no_of_pages),
                                "transaction_hash" : str(1),
                                "confirmkey": str(privatekeycnf),
                                "isfdx" : str(isfdx)
                            }
                        )
                    elif script_audit_status == States.FAILURE and audit_status_object.only_audit == True:
                        display_list.append(
                            {
                                "scriptid": str(script.id),
                                "scriptName": str(script.screenplay.name),
                                "author": str(script.screenplay.author),
                                "iscomplete": "F",
                                "created_on" : str(script.created_on.astimezone(pytz.timezone('Asia/Kolkata')).strftime("%B %d, %Y %I:%M %p")),
                            }
                        )
                    elif script_audit_status == States.STARTED and audit_status_object.only_audit == True:
                        extimated_time = audit_status_object.expected_duration
                        no_of_pages = audit_status_object.number_of_pages
                        display_list.append(
                            {
                                "scriptid": str(script.id),
                                "scriptName": str(script.screenplay.name),
                                "author": str(script.screenplay.author),
                                "iscomplete": "R",
                                "created_on" : str(script.created_on.astimezone(pytz.timezone('Asia/Kolkata')).strftime("%B %d, %Y %I:%M %p")),
                                "extimated_time": str(extimated_time),
                            }
                        )
                    elif script_audit_status == States.PENDING and audit_status_object.only_audit == True:
                        continue
        url = get_current_site(request).domain
        return render(
            request,
            "audit/audited_copy1.html",
            {"i": display_list, "url": url},
        )



class DownloadScriptFromBlockchain(APIView):
    authentication_classes = [BasicAuthentication,SessionAuthentication]
    permission_classes = [IsAuthenticated]
    """
    View for downloding scripts of desired type eg- docx, pdf
    """

    VALID_FILE_TYPES = ("script-docx", "script-pdf","script-original","audit-report","script-json")
    """
    Files of only above types are allwed to be downloaded.
    """


    def get(self, request) -> FileResponse:

        if not "script_id" in request.query_params:
            raise spex.ScriptIdNotFound("script_id must be provided in body.")

        if not "type" in request.query_params:
            raise spex.ScriptAuditException(
                "type(i.e. type of file) must be provided in body.")

        script_id = request.query_params.get("script_id")
        file_type = request.query_params.get("type")
        privatekey1 = request.query_params.get("privatekey")


        self.user = request.user
        self.Blockchain_script = script_id
        self.privatekey1 = privatekey1
        if not request.query_params.get("type") in self.VALID_FILE_TYPES:
            raise spex.IllegalFiletype(file_type, self.VALID_FILE_TYPES)

        try:
            if UserCredentialsForBlockchain.objects.filter(user=request.user).exists():
                blockchain_obj = UserCredentialsForBlockchain.objects.get(user=request.user)
                userkeys = decryptionOfPrivate(blockchain_obj.privateKey)
                publickey = mypublickey(privatekey1)
                if publickey == blockchain_obj.publicKey:
                    UserId = blockchain_obj.user_id
                    status,getdata = getScriptAudit(privatekey1,UserId,str(script_id))
                    print(status,">>>>>>>>>>>>>>>>>>>>")
                    if status == True:
                        self.auditData = eval(getdata[1])
                    else:
                        return JsonResponse({"status":False, "error": "This Transcation Is Not Found On The Blockchain.",},status=500)
                else:
                    return JsonResponse({"status":False, "error": "Your Private Key Is Wrong", "key": str(userkeys)},status=500)
            else:
                return JsonResponse({"status":False, "error": "Your Wallet is Not Created",},status=500)
        except Exception as e:
            return JsonResponse({"status":False, "error": "Private key Format Wrong",},status=500)
        
        if file_type == "script-json":
            # self.query_file = self.file_model_objects.get(type=file_type)
            try:
                query_file1 = self.auditData.get(file_type)
                file_hash = query_file1.get('script_file')
                file_name = str((str(query_file1['script_file_path']).split("/"))[-1])
                geting_files = str(decryptionOflocalUrl(file_hash))
                # Fetch the JSON data from the URL
                response = requests.get(str(geting_files))
                if response.status_code == 200:
                    # Parse the JSON data
                    json_data = response.json()
                    # Return the JSON data as a response
                    return JsonResponse({"script-json": json_data}, status=200)
                return JsonResponse({"response didnt work": str(geting_files), "message": "File type is script-json"}, status=200)
            except Exception as exp:
                # err = repr(exp)
                return JsonResponse({"fail": repr(exp), "message": "File type is script-json"}, status=400)
                
        try:
            query_file1 = self.auditData[file_type]
        except Exception as exp:
            return JsonResponse({"status":False, "error": "The File Does Not Exist On The Blockchain",},status=500)

        file_path = query_file1.get('script_file_path')

        try:
            file_hash = query_file1.get('script_file')
            file_name = str((str(query_file1['script_file_path']).split("/"))[-1])
        except:
            pass
        try:
            print("file_path")
            print(file_path)
            geting_files = decryptionOflocalUrl(file_hash)
            return JsonResponse({"status":True, "download_link": geting_files, "file_name": str(file_name)})
        except:            
            return JsonResponse({"status":False, "error": "The File Does Not Exist On IPFS Node"},status=500)


class DownloadScript(APIView):
    authentication_classes = [BasicAuthentication,SessionAuthentication]
    permission_classes = [IsAuthenticated]
    """
    View for downloding scripts of desired type eg- docx, pdf
    """

    VALID_FILE_TYPES = ("script-docx", "script-pdf","script-original","audit-report")
    """
    Files of only above types are allwed to be downloaded.
    """

    def get(self, request) -> FileResponse:
        if not "script_id" in request.query_params:
        # script_id = request.query_params.get("script_id")
        # if not "script_id" in request.data.get("script_id"):
            raise spex.ScriptidNotFound("script_id must be provided in body.")

        if not "type" in request.query_params:
            raise spex.ScriptpadException("type(i.e. type of file) must be provided in body.")

        script_id = request.query_params.get("script_id")
        file_type = request.query_params.get("type")

        if not request.query_params.get("type") in self.VALID_FILE_TYPES:
            raise spex.IllegalFiletype(file_type, self.VALID_FILE_TYPES)

        self.file_model_objects = File.objects.filter(script = script_id)

        if not self.file_model_objects.exists():
            raise spex.BadQuery()

        try:
            self.query_file = self.file_model_objects.get(type=file_type)

        except django_exception.ObjectDoesNotExist:

            if file_type == "script-docx":
                self._check_docx()

            elif file_type == "script-pdf":
                self._check_pdf()


        file_path = self.query_file.file.path
        file_size = self.query_file.file.size

        mimetype, _ = mimetypes.guess_type(file_path)

        response = FileResponse(self.query_file.file, content_type=mimetype)
        response['Content-Length'] = file_size
        response['Content-Disposition'] = f"attachment; filename={self.query_file.script.screenplay.name + '_' + str(self.query_file)}.{file_path.rsplit('.',1)[1]}"
        return response

    def _check_docx(self) -> None:
        """
        When docx not found this function can create docx of script for available formats.
        """
        try:
            audit_file_object = self.file_model_objects.get(type="script-csv")

        except django_exception.ObjectDoesNotExist:
            self._download_original()
            return

        df = pd.read_csv(audit_file_object.file)
        docx = utilities.csv_to_docx(df)

        temp_file_stream = BytesIO()
        docx.save(temp_file_stream)
        temp_file_stream.seek(0)

        docx_file = ContentFile(
            temp_file_stream.getvalue(),
            "from_audited_csv_to_document.docx",
        )

        self.query_file = File.objects.create(
            script = audit_file_object.script,
            file = docx_file,
            type = "script-docx",
        )

        temp_file_stream.close()

    def _check_pdf(self) -> None:
        """
        When pdf not found this function can create docx of script for available formats.
        """

        if not self.file_model_objects.filter(type="script-docx").exists():
            self._check_docx()

        try:
            docx_file_object = self.file_model_objects.get(type="script-docx")

        except django_exception.ObjectDoesNotExist:
            self._download_original()
            return

        temp_dir = tempfile.TemporaryDirectory()
        pdf_file_path = utilities.docx_to_pdf(docx_file_object.file.path, temp_dir.name)

        with open(pdf_file_path, "rb") as temp_pdf:

            pdf_file = DjangoFile(
                temp_pdf,
                pdf_file_path.rsplit('/',1)[1],
            )

            self.query_file = File.objects.create(
                script = docx_file_object.script,
                file = pdf_file,
                type = "script-pdf",
            )

    def _download_original(self) -> None:
        """
        used for downloafing the script-original when no other type of scripts are found.
        This downloads the unaudited file.
        """
        self.query_file = self.file_model_objects.get(type="script-original")
        
        
class DeleteScriptAPIView(APIView):
    authentication_classes = [BasicAuthentication,SessionAuthentication]
    permission_classes = [IsAuthenticated]

    def delete(self,request,screen_play):
        try:
            screenplay = ScreenPlay.objects.get(name=screen_play)
            screenplay.delete()
            return Response("Scripts deleted successfully.", status=status.HTTP_204_NO_CONTENT)
        except ScreenPlay.DoesNotExist:
            return Response(f"Screenplay with name '{screen_play}' does not exist.", status=status.HTTP_404_NOT_FOUND)

def string_to_audit(request,path):

    """
    the function uses:
    request for use detail
    path : path of the script to be audited
    """
    gpt_path = path

    u_name = script_id_generator()
    screenplay_name = u_name

    author = "MNF"
    language = "en"
    script_file = gpt_path
    script_ext = script_file.split(".")[-1]
    script_file_name = screenplay_name + "." + script_ext

    with open(script_file, "rb") as script_file:
        file = ContentFile(script_file.read(),
                        script_file_name,
                        )

    result = filesystem.new_screenplay_without_audit_in_background(
        request.user,
        author,
        screenplay_name,
        file,
        "script-original",
        language,

    )
    script_id = result.get("script", {}).get("id")
    try:
        update_audit_status(script_id, States.STARTED)
    except:
        update_audit_status(script_id, States.FAILURE)


    try:
        naudit = NeutralAudit(script_id)
        naudit.audit()
        ScriptAuditModel.objects.update_or_create(
        script = Script.objects.get(
            id = script_id
        ),
        defaults={"status" : "SUCCESS"}
    )

    except:
        ScriptAuditModel.objects.update_or_create(
        script = Script.objects.get(
            id = script_id
        ),
        defaults={"status" : "FAILURE"}
    )

    return script_id


def generate_script_id(path,request):
    input_file = os.path.basename(path)
    input_file1 = os.path.splitext(input_file)[0]
    now = datetime.now()
    screenplay_name = input_file1 +"_"+ now.strftime("%d-%m-%Y_%H-%M-%S_")
    author = "SELF_DEFINED"
    language = "en"
    script_file = path
    script_ext = script_file.split(".")[-1]
    script_file_name = screenplay_name + "." + script_ext
    print(script_file_name)
    with open(path, "rb") as script_file:
        file = ContentFile(script_file.read(),
                        script_file_name,
                        )
    result = filesystem.new_screenplay(
                request.user,
                author,
                screenplay_name,
                file,
                "script-original",
                language,
            )
    script_id = result.get("script", {}).get("id")
    print("\n\n\n\nSCRIPT____ID :",script_id,"\n\n\n\n")
    return script_id