import os
import datetime
import requests
import textwrap
from offres_emploi import Api
from offres_emploi.utils import dt_to_str_iso
from dash import Dash, html, dcc, callback, Output, Input, dash_table, State, _dash_renderer, clientside_callback
import dash_bootstrap_components as dbc
import plotly.express as px
import plotly.graph_objects as go
import dash_mantine_components as dmc
from dash_iconify import DashIconify
import pandas as pd
from dotenv import load_dotenv
_dash_renderer._set_react_version("18.2.0")
import plotly.io as pio
from langchain_community.llms import HuggingFaceEndpoint
from langchain_core.prompts import ChatPromptTemplate, PromptTemplate
from langchain.schema.output_parser import StrOutputParser
from pinecone import Pinecone
from bs4 import BeautifulSoup
from flask import Flask
server = Flask(__name__)
# external JavaScript files
external_scripts = [
    'https://datacipen-eventia.hf.space/copilot/index.js'
]
# Create a customized version of the plotly_dark theme with a modified background color
custom_plotly_dark_theme = {
    "layout": {
        "paper_bgcolor": "#1E1E1E",  # Update the paper background color
        "plot_bgcolor": "#1E1E1E",   # Update the plot background color
        "font": {
            "color": "#FFFFFF"       # Update the font color
        },
        "xaxis": {
            "gridcolor": "#333333",  # Update the x-axis grid color
            "zerolinecolor": "#666666"  # Update the x-axis zero line color
        },
        "yaxis": {
            "gridcolor": "#333333",  # Update the y-axis grid color
            "zerolinecolor": "#666666"  # Update the y-axis zero line color
        }
    }
}
# Apply the customized theme to your Plotly figures
pio.templates["custom_plotly_dark"] = custom_plotly_dark_theme
pio.templates.default = "custom_plotly_dark"
load_dotenv()
def removeTags(all):
  for data in all(['style', 'script']):
    data.decompose()
  return ''.join(all.stripped_strings)
def htmlToDataframe(htmlTable):
  data = []
  list_header = []
  soup = BeautifulSoup(htmlTable,'html.parser')
  header = soup.find_all("table")[0].find("tr")
  for items in header:
    try:
      list_header.append(items.get_text())
    except:
      continue
  HTML_data = soup.find_all("table")[0].find_all("tr")[1:]
  for element in HTML_data:
    sub_data = []
    for sub_element in element:
      try:
        sub_data.append(sub_element.get_text())
      except:
          continue
    data.append(sub_data)
  dataFrame = pd.DataFrame(data = data, columns = list_header)
  return dataFrame
def getSavoirFaireFromHTMLMetier(url):
  response = requests.get(url)
  soup = BeautifulSoup(response.text, "html.parser")
  allSavoirFaire = soup.select('ul[data-cy="liste-savoir-faire-metier"] > li')
  if len(allSavoirFaire) != 0:
    allSF = "
| categorie | emploi | salaire | 
"
        allsalaires += "| " + removeTags(alltypesalaires[1]) + " | " + removeTags(allcategorie[0]) + " | " + removeTags(allFAPsalaires[0]).replace('\xa0','').replace(' ','').replace('€','') + " | 
"
        allsalaires += "| " + removeTags(alltypesalaires[2]) + " | " + removeTags(allcategorie[0]) + " | " + removeTags(allFAPsalaires[1]).replace('\xa0','').replace(' ','').replace('€','') + " | 
"
        allsalaires += "| " + removeTags(alltypesalaires[3]) + " | " + removeTags(allcategorie[0]) + " | " + removeTags(allFAPsalaires[2]).replace('\xa0','').replace(' ','').replace('€','') + " | 
"
        if len(allFAP) >= 2 and len(allFAPsalaires) == 6:
            allsalaires += "| " + removeTags(alltypesalaires[1]) + " | " + removeTags(allcategorie[4]) + " | " + removeTags(allFAPsalaires[3]).replace('\xa0','').replace(' ','').replace('€','') + " | 
"
            allsalaires += "| " + removeTags(alltypesalaires[2]) + " | " + removeTags(allcategorie[4]) + " | " + removeTags(allFAPsalaires[4]).replace('\xa0','').replace(' ','').replace('€','') + " | 
"
            allsalaires += "| " + removeTags(alltypesalaires[3]) + " | " + removeTags(allcategorie[4]) + " | " + removeTags(allFAPsalaires[5]).replace('\xa0','').replace(' ','').replace('€','') + " | 
"
        allsalaires += "
"
    alltypedifficultes = soup.select('.tabs-main-content_persp-col2-bar.ng-star-inserted')
    alldifficulte = soup.select('p.horizontal-graph_title')
    allpcdifficulte = soup.select('div.horizontal-graph_data')
    alldifficultes = "