import logging

import azure.functions as func
from azure.storage.blob import BlobServiceClient, BlobClient
from io import BytesIO
import zipfile


def main(req: func.HttpRequest) -> func.HttpResponse:
    def copy_azure_blobs(srcName):
        from_Blob = BlobClient("https://SAS.url?SASToken",
                               "archives",
                               srcName)
        srcNameText = srcName.split('/')[-1]
        to_Blob = BlobClient("https://SAS.url?SASToken",
                             clientName.replace('_','-').lower(),
                             srcNameText)
        to_Blob.start_copy_from_url(from_Blob.url)
    
    srcStorageAccount = BlobServiceClient.from_connection_string(conn_str=srcConnStr)
    srcContainer = srcStorageAccount.get_container_client("archives")
    destStorageAccount = BlobServiceClient.from_connection_string(conn_str=destConnStr)
    destContainer = destStorageAccount.get_container_client(clientName.lower())
    fileCount = 0
    inputFileCount = 0
    for blobItem in srcContainer.list_blobs(name_starts_with=clientName+"/"):
        blobName = blobItem.name
        if blobName.endswith(".zip"):
            if "(input)" in blobName:
                inputFileCount += 1
                copy_azure_blobs(blobName)
            else:
                logging.info(f"[+] BlobName -->> {blobName}")
                downloadedStream = srcContainer.download_blob(blobName)
                zstream = BytesIO()
                downloadedStream.download_to_stream(zstream)
                zstream.seek(0)
                zFile = zipfile.ZipFile(zstream, 'r')
                newZipFile_name = blobName.split('/')[-1]
                newZipFile = BytesIO()
                for archivedFile in zFile.namelist():
                    if archivedFile.endswith('incidentLogs.rpt'):
                        fileCount += 1
                        fp_stream = zFile.read(archivedFile)
                        newFileName = archivedFile.split('/')[-1]
                        with zipfile.ZipFile(newZipFile, 'w') as zip_archive:
                            zip_archive.writestr(newFileName, fp_stream)
                            
                # logging.info("Files zipped for "+ blobItem.name + ": " +str(newZipFile.namelist()))
                if fileCount > 0:
                    destContainer.upload_blob(newZipFile_name, newZipFile)

    return func.HttpResponse(f"Total number of zip seen: " + str(inputFileCount+fileCount) +
                "\nTotal number of zips extarcted and retrieved: " + str(fileCount))