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 tna(Goals,Challenges, skills_db, 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 with escaped curly braces and f-string
    system_prompt = PromptTemplate(
        input_variables=["skills_db", "Goals","Challenges"],
        template="""
        You are an AI assistant specializing in Training Need Analysis (TNA). 
        Given a company's business goals, challenges, and existing skills database, 
        suggest skill areas for development. 

        **Existing Skills Database:**
        {skills_db}

        **Business Goals:**
        {Goals}

        **Challenges:**
        {Challenges}

        **Objective:**
        - Identify skill gaps.
        - Recommend relevant skill enhancements.
        - Provide justifications for each recommendation.
        - You can also give skills 
        - You may also suggest skills outside the existing database if they align with the business goals and challenges. But make sure you write there that these are exclusive from your skills
        - SUggest minimum 10 skills
        Return your response in a structured format:
        - **Recommended Skills**
        - **JSON Output**
        - **Why These Skills Are Important**
        """
    )


    # Create the PromptTemplate using from_template
    prompt = system_prompt.format(Goals=Goals,Challenges=Challenges,skills_db=skills_db)

    # Format the prompt with skills_db and text
   
    
    # Optionally, print the formatted prompt for debugging
    # print("Formatted Prompt:")
    # print(formatted_prompt)

    # Send the formatted prompt to ChatGroq
    try:
        output = chat.invoke(prompt)
        token_consumed = output.response_metadata['token_usage']['total_tokens']
        skill_tree = output.content
    except Exception as e:
        print(f"An error occurred during chat invocation: {e}")
        return None, 0

    return skill_tree, token_consumed



def tna2(Goals,Challenges, 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 with escaped curly braces and f-string
    system_prompt = PromptTemplate(
        input_variables=["Goals","Challenges"],
        template="""
        You are an AI assistant specializing in Training Need Analysis (TNA). 
        Given a company's business goals, challenges suggest skill areas for development. 

       
        **Business Goals:**
        {Goals}

        **Challenges:**
        {Challenges}

        **Objective:**
        - Identify skill gaps.
        - Recommend relevant skill enhancements.
        - Provide justifications for each recommendation.
        - You can also give skills 
        - SUggest minimum 10 skills
        - ALso provide courses to improve skills 
        - minimum 2 couses with each skills
        Return your response in a structured format:
        - **Recommended Skills**
        - **JSON Output**
        - **Why These Skills Are Important**
        """
    )


    # Create the PromptTemplate using from_template
    prompt = system_prompt.format(Goals=Goals,Challenges=Challenges)

    # Format the prompt with skills_db and text
   
    
    # Optionally, print the formatted prompt for debugging
    # print("Formatted Prompt:")
    # print(formatted_prompt)

    # Send the formatted prompt to ChatGroq
    try:
        output = chat.invoke(prompt)
        token_consumed = output.response_metadata['token_usage']['total_tokens']
        skill_tree = output.content
    except Exception as e:
        print(f"An error occurred during chat invocation: {e}")
        return None, 0

    return skill_tree, token_consumed

def tna3(Goals, Challenges, skills_db, course, 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", "Goals", "Challenges", "course"],
        template="""
        You are an AI assistant specializing in Training Need Analysis (TNA). 
        Given a company's business goals, challenges, existing skills database, and available courses, 
        suggest skill areas for development and recommend relevant courses.

        **Existing Skills Database:**
        {skills_db}

        **Business Goals:**
        {Goals}

        **Challenges:**
        {Challenges}

        **Available Courses:**
        {course}

        **Objective:**
        - Identify skill gaps.
        - Recommend relevant skill enhancements.
        - Provide justifications for each recommendation.
        - Suggest at least **10 skills** based on business needs and challenges.
        - Cross-check the **available courses** to suggest the best courses for each recommended skill.
        - If needed, suggest additional skills **outside the existing database** and label them as **(Exclusive Suggestions).**

        **Return your response in a structured format:**
        ```json
        {{
            "Recommended Skills": [...],
            "Skill-Course Mapping": {{
                "Skill1": ["Course1", "Course2"],
                "Skill2": ["Course3", "Course4"]
            }},
            "Why These Skills Are Important": {{
                "Skill1": "Justification...",
                "Skill2": "Justification..."
            }}
        }}
        ```
        """
    )

    # Format the prompt
    prompt = system_prompt.format(Goals=Goals, Challenges=Challenges, skills_db=skills_db, course=course)

    # Send the formatted prompt to ChatGroq
    try:
        output = chat.invoke(prompt)
        token_consumed = output.response_metadata.get('token_usage', {}).get('total_tokens', 0)
        skill_tree = output.content
    except Exception as e:
        print(f"Error during chat invocation: {e}")
        return None, 0

    return skill_tree, token_consumed





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"
    '''

    
    skills = '''
    "Python",
    "JavaScript",
    "SQL",
    "C++",
    "React.js",
    "Node.js",
    "Django",
    "Flask",
    "TensorFlow",
    "PyTorch",
    "Docker",
    "Kubernetes",
    "AWS",
    "Google Cloud Platform (GCP)",
    "RESTful APIs",
    "GraphQL",
    "Git",
    "CI/CD Pipelines",
    "Cybersecurity Principles",
    "Data Visualization"
    '''

    courses = '''
    [
    "Python for Everybody - University of Michigan (Coursera)",
    "JavaScript: Understanding the Weird Parts (Udemy)",
    "The Complete SQL Bootcamp 2024 (Udemy)",
    "Docker for Beginners (Udemy)",
    "Deep Learning Specialization - Andrew Ng (Coursera)",
    "React - The Complete Guide (Udemy)",
    "AWS Certified Solutions Architect - Associate (Udemy)",
    "Introduction to Cyber Security (Udemy)",
    "Git & GitHub for Beginners (Udemy)",
    "Data Visualization with Python (Coursera)",
    "Complete Python Developer in 2024 - Zero to Mastery (Udemy)",
    "The Modern JavaScript Bootcamp (Udemy)",
    "SQL for Data Science - University of California, Davis (Coursera)",
    "Docker Mastery: With Kubernetes & Swarm (Udemy)",
    "TensorFlow for Beginners (Udemy)",
    "Full-Stack Web Development with React (Coursera)",
    "AWS Fundamentals (Coursera)",
    "Cybersecurity Specialization - University of Maryland (Coursera)",
    "Version Control with Git (Coursera)",
    "Data Visualization with Tableau (Udemy)",
    "Python Data Structures - University of Michigan (Coursera)",
    "JavaScript Basics for Beginners (Coursera)",
    "Advanced SQL for Data Scientists (LinkedIn Learning)",
    "Docker Deep Dive (Pluralsight)",
    "Introduction to TensorFlow for AI, ML, and DL (Coursera)",
    "React for Beginners (Scrimba)",
    "AWS Cloud Practitioner Essentials (AWS Training)",
    "The Complete Cyber Security Course (Udemy)",
    "Mastering Git and GitHub (Udemy)",
    "Storytelling with Data (LinkedIn Learning)",
    "Python Programming Masterclass (Udemy)",
    "Eloquent JavaScript (Self-paced Book & Online Exercises)",
    "Databases and SQL for Data Science with Python (Coursera)",
    "Kubernetes and Docker: The Complete Guide (Udemy)",
    "TensorFlow Developer Certificate Training (Udemy)",
    "Modern React with Redux (Udemy)",
    "Architecting with AWS (Pluralsight)",
    "Cybersecurity Essentials (Cisco Networking Academy)",
    "The Git & GitHub Bootcamp (Udemy)",
    "Data Visualization with D3.js (Udacity)",
    "Automate the Boring Stuff with Python (Udemy)",
    "JavaScript Algorithms and Data Structures (freeCodeCamp)",
    "Introduction to SQL (Codecademy)",
    "Docker Essentials: A Developer Introduction (IBM Skills Network)",
    "TensorFlow in Practice (Coursera)",
    "Frontend Development with React (Pluralsight)",
    "AWS Certified DevOps Engineer (A Cloud Guru)",
    "CompTIA Security+ Certification Training (Udemy)",
    "Learn Git & GitHub (Codecademy)",
    "Advanced Data Visualization with Python and Matplotlib (Pluralsight)"
    ]
    '''

    competency_names = [
    "Adaptability", "Agile Methodologies", "Business Acumen", "cCCCCC",
    "Cloud Architecture", "Cloud Computing", "Cloud Migration", "Cloud Monitoring", "Communication",
    "Computer Basic", "Creativity", "Critical Thinking", "Cybersecurity", "Data Analysis",
    "Data Encryption", "Data Science", "Data Visualization", "Decision Making", "Empathy",
    "Incident Response", "Industry Trends", "Leadership", "Machine Learning", "Negotiation",
    "Network Security", "Parent", "Problem Solving", "Programming Languages", "Project Management",
    "Py", "Risk Management", "Sector Expertise", "Soft Skills", "Software Development",
    "Software Testing", "Stakeholder Communication", "Teamwork", "Technology Industry Knowledge", "Tester",
    "Time Management", "undefined", "Version Control"
    ]
    
    assigned_course = ["Python for Everybody - University of Michigan (Coursera)",  
    "JavaScript: Understanding the Weird Parts (Udemy)",  
    "The Complete SQL Bootcamp 2024 (Udemy)",  
    "Docker for Beginners (Udemy)",  
    "Deep Learning Specialization - Andrew Ng (Coursera)",  
    "React - The Complete Guide (Udemy)",  
    "AWS Certified Solutions Architect - Associate (Udemy)",  
    "Git & GitHub for Beginners (Udemy)",  
    "Complete Python Developer in 2024 - Zero to Mastery (Udemy)",  
    "SQL for Data Science - University of California, Davis (Coursera)",  
    "Docker Mastery: With Kubernetes & Swarm (Udemy)",  
    "TensorFlow for Beginners (Udemy)",  
    "Cybersecurity Specialization - University of Maryland (Coursera)",  
    "Kubernetes and Docker: The Complete Guide (Udemy)",  
    "Automate the Boring Stuff with Python (Udemy)" 
    ]

    Goals='Want to imporve my tech team'
    Challenge='My tech team is not able to work efficiently '
    skill_tree,token_consumed  = tna3(skills_db = skillset, Goals=Goals,Challenges=Challenge,course= courses, groq_api_key=GROQ_API_KEY)
    #skill_tree,token_consumed  = tna2(Goals=Goals,Challenges=Challenge, groq_api_key=GROQ_API_KEY)
    #skill_tree,token_consumed  = description_skill(industry=industry,groq_api_key=GROQ_API_KEY)

    #print(token_consumed)
    print(skill_tree)
    #print(type(skill_tree))