import os
import time
import random
import re
import json
import requests
from langchain_community.embeddings import OllamaEmbeddings
from langchain_community.vectorstores import FAISS
from langchain_core.prompts import ChatPromptTemplate
from crawl4ai import AsyncWebCrawler
import asyncio
from langchain_groq import ChatGroq
from langchain_core.prompts import PromptTemplate
from langchain_core.output_parsers import StrOutputParser, PydanticOutputParser
from langchain_core.runnables import RunnablePassthrough
from langchain_community.document_loaders import PyPDFLoader, TextLoader, Docx2txtLoader, UnstructuredPowerPointLoader
from PyPDF2 import PdfReader
from langchain_experimental.text_splitter import SemanticChunker
from pydantic import BaseModel, Field
from typing import List, Optional, Dict
from urllib.parse import urlparse

import shutil

GROQ_API_KEY = "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"
OLLAMA_MODEL = "nomic-embed-text"


def map_skills_to_designations(skills_db, designations, groq_api_key, model_name="mixtral-8x7b-32768", temperature=0):
   

    # Initialize the chat model
    chat = ChatGroq(temperature=temperature, groq_api_key=groq_api_key, model_name=model_name)

    # Define the system prompt
    system_prompt = PromptTemplate(
        input_variables=["skills_db", "designations"],
        template='''
You are an expert in skill-to-designation mapping. Your task is to analyze a given set of skills and map them to the most appropriate designations.

### Input Skills:
{skills_db}

### Designations:
{designations}

### Output Format:
{{
  "Designation 1": ["Skill 1", "Skill 2", "Skill 3"],
  "Designation 2": ["Skill 4", "Skill 5"],
  "Designation 3": ["Skill 6", "Skill 7", "Skill 8"]
}}

### Notes:
- Ensure that each skill is mapped only to the most relevant designation(s).
- If a skill fits multiple designations, include it in both.
- Use clear and concise mappings.
- Respond only with the JSON object in the specified format, without any additional text.
- If you found no skills for some designations check does designation at last one more time

### Instructions:
- **Respond only with the JSON object in the specified format.**
- **Do not include any additional text, explanations, or code fences (e.g., ```json).**
- **Ensure that the JSON is properly formatted and valid.**
'''
    )

    # Format the prompt with the input skills and designations
    formatted_prompt = system_prompt.format(skills_db=skills_db, designations=designations)

    # Send the formatted prompt to ChatGroq
    try:
        output = chat.invoke(formatted_prompt)
        token_consumed = output.response_metadata['token_usage']['total_tokens']
        skill_to_designation_mapping = output.content
    except Exception as e:
        print(f"An error occurred during chat invocation: {e}")
        return None, 0

    return skill_to_designation_mapping, token_consumed

# Example usage
if __name__ == "__main__":
    GROQ_API_KEY = "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"  # Replace with your actual Groq API key
    industry = "Infrastructure"
    #skill_tree,token_consumed  = generate_skill_tree(prompt_text=industry, groq_api_key=GROQ_API_KEY)
    skillset ='''
      Budgeting,
      Cost Estimation,
    
      Civil Engineering,
      Structural Analysis,
   
      AutoCAD,
      Revit,
    
      Communication,
      Leadership,
      Teamwork,
      Urban Design,
      Water Supply and Distribution
      , SQL, C++ , Management, Housekeeping Management,Front Desk Management,Complaint Handling,Recruitment,Training & Development
    '''

    Designation = '''
        HR, Developer, Manager, Planner,CEO, Infrastructure Deverloper
        
        '''
    skill_tree,token_consumed  = map_skills_to_designations(skills_db= skillset,designations =Designation , groq_api_key=GROQ_API_KEY)


    print(token_consumed)
    print(skill_tree)
    print(type(skill_tree))