import streamlit as st
from azure.storage.blob import BlobServiceClient
import json
import os

connection_string = os.getenv("CONNECTION")
blob_service_client = BlobServiceClient.from_connection_string(connection_string)


def upload_blob(pdf_name, json_data, pdf_data):
    container_name = "jobdescriptions"
    json_blob_name = f"{pdf_name}_jsondata.json"
    pdf_blob_name = f"{pdf_name}.pdf"

    container_client = blob_service_client.get_container_client(container_name)

    json_blob_client = container_client.get_blob_client(json_blob_name)
    json_blob_client.upload_blob(json_data.encode('utf-8'), overwrite=True)

    pdf_blob_client = container_client.get_blob_client(pdf_blob_name)
    pdf_blob_client.upload_blob(pdf_data, overwrite=True)

    link = "https://tensora.ai/workgenius/cv-evaluation2/?job="+pdf_name

    st.write('Data and PDF have been successfully uploaded. The link to the chatbot for the potential candidates is the following: '+link)


def main():
    st.markdown(
    """
    <style>
        [data-testid=column]{
            text-align: center;
            display: flex;
            align-items: center;
            justify-content: center;
        }
    </style>
    """,
        unsafe_allow_html=True,
    )
    col1, col2 = st.columns([2, 1])


    col1.title("Job description upload")
    col2.image("https://www.workgenius.com/wp-content/uploads/2023/03/WorkGenius_navy-1.svg")

    st.write("Please upload the job description as a PDF and enter the job title for the position. To receive the evaluation of the potential candidates, please enter your email address.")

    uploaded_file = st.file_uploader("Upload a PDF file:", type=["pdf"])

    job_title = st.text_input("Enter the job title:")
    email = st.text_input("Enter the email:")
    if st.button("Submit"):
        if len(job_title) > 0 and len(email) > 0 and uploaded_file:
            data = {
                "title": job_title,
                "email": email
            }
            json_data = json.dumps(data, ensure_ascii=False)

            # Eine zufällige UUID generieren
            random_uuid = uuid.uuid4()

            # Die UUID als String darstellen
            uuid_string = str(random_uuid)

            pdf_name = uuid_string

            pdf_data = uploaded_file.read()

            upload_blob(pdf_name, json_data, pdf_data)
        else:
            st.write("Please fill out both fields and upload a PDF file.")


if __name__ == "__main__":
    main()