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
GROQ_API_KEY = "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"


chat = ChatGroq(temperature=0, groq_api_key=GROQ_API_KEY, model_name="mixtral-8x7b-32768")



# Modified System Prompt
system_enrichment = '''You are a skill mapping assistant. Your task is to analyze an existing skill tree and:
1. Identify missing child skills for the provided parent skills.
2. Suggest additional parent skills if needed.
3. Maintain the given format and structure.

### Formatting Guidelines:
- Always respond in the provided JSON format.
- Include explanations for new additions.

### Required JSON Format:
{{
  "Parent Skill": {{
    "description": "Description of the parent skill category.",
    "skills": {{
      "Skill Name": "Description of the specific skill."
    }}
  }},
  "Additions": {{
    "Parent Skills": [{{
      "Parent Skill": {{
        "description": "Description of the parent skill category.",
        "skills": {{
          "Skill Name": "Description of the specific skill."
        }}
      }}
    }}]
  }}
}}

### Notes:
- Clearly separate existing skills from new suggestions in the "Additions" section.
- Avoid duplicating skills already provided.
- Be consistent with formatting and indentation.
'''

human_input = '''
Existing Skill Tree:
{text}

Enhance this skill tree by filling in gaps and suggesting improvements or new parent skills.
'''

# Updated Prompt
prompt_enrichment = ChatPromptTemplate.from_messages([("system", system_enrichment), ("human", human_input)])

# Invoke the Chain with Existing Skill Tree
existing_skill_tree = {
    "Hospitality Management": {
        "description": "Core skills to ensure excellent guest experiences and service.",
        "skills": {
            "Front Desk Management": "Handling guest check-ins, check-outs, and reservations.",
            "Customer Service": "Ensuring guests have a pleasant and personalized experience."
        }
    },
    "Soft Skills": {
        "description": "Essential interpersonal and communication skills.",
        "skills": {
            "Communication": "Effectively conveying information and ideas.",
            "Leadership": "Guiding and inspiring team members."
        }
    }
}

chain_enrichment = prompt_enrichment | chat
#output_enrichment = chain_enrichment.invoke({"text": json.dumps(existing_skill_tree, indent=4)})
# Get output from the chain
output_new_format = chain_enrichment.invoke({"text": json.dumps(existing_skill_tree, indent=4)})

# Parse and process output
if hasattr(output_new_format, "content"):
    output_content = output_new_format.content
    # Extract JSON part
    match = re.search(r"\{.*\}", output_content, re.DOTALL)
    if match:
        json_content = match.group(0)
        skill_tree = json.loads(json_content)
    else:
        raise ValueError("No valid JSON found in the output.")
else:
    raise ValueError("Output does not contain 'content' attribute")

# Save to file
output_file = "enhanced_skill_tree.json"
with open(output_file, "w") as f:
    json.dump(skill_tree, f, indent=4)

print(f"Enhanced skill tree saved to {output_file}")