from typing import Type
from crewai.tools import BaseTool
from pydantic import BaseModel, Field
import dspy
import os
from dotenv import load_dotenv

# === Load credentials ===
load_dotenv(dotenv_path="/home/azureuser/microlearn/backend/prompt_opt_dspy/.env")

azure_api_key = os.getenv("AZURE_API_KEY")
azure_api_host = os.getenv("AZURE_API_HOST")
azure_api_version = os.getenv("AZURE_API_VERSION")

# === Configure Azure OpenAI LM ===
lm = dspy.LM(
    "azure/Csqr-gpt-4o-mini",
    api_key=azure_api_key,
    api_base=azure_api_host,
    api_version=azure_api_version,
    temperature=0.2,
)
dspy.configure(lm=lm)

# === DSPy Signature ===
class ContentReviewerSignature(dspy.Signature):
    """
    You are an experienced corporate trainer.

        I will provide you with:
        1. Main topic 
        2. A user journey syllabus for the first stage to cover the topic, in that there is 
            + Sub heading of the stage
            + Expected outcome
            + Duration
            + Topics to be covered in the stage
        3. Already created content based on the user journey

        Instructions:
        1. Based on the user journey, carefully review the already created content.
        2. Review the content under the sections {topic_sections}
        3. Fill the gap (if any) with content to enrich the user journey and append the content to the existing content.
        4. Include relevant examples, keeping in mind the user is minimally technical or non-technical.
        5. Keep all other sections as such.

        Output format:
        Same as the already created content in markdown
        Should not include any other comments.

        The Information:
        - Main topic: {main_heading}
        - User journey: {stage_data}
        - Already created content: {content}
    """

    topic_sections: str = dspy.InputField()
    main_heading: str = dspy.InputField()
    stage_data: str = dspy.InputField()
    content: str = dspy.InputField()
    enriched_content: str = dspy.OutputField(desc="Markdown content with enriched sections")


# === DSPy Module ===
class ContentReviewerModule(dspy.Module):
    def __init__(self):
        super().__init__()
        predictor = dspy.Predict(ContentReviewerSignature)

        # --- Few-shot examples (you can add more here) ---
        trainset = [
            dspy.Example(
                topic_sections="""Introduction to Data Analysis,
                Importance of Data in Decision Making ,
                Key Terminology: Data, Information, Metrics,
                Understanding Data Types: Qualitative vs Quantitative ,
                Data Collection Methods: Surveys, Interviews, Observations,
                The Data Analysis Process: Steps Involved

                """,
                main_heading="Data Analytics for Business Leaders: A User Journey for Vice Presidents in Financial Services",
                stage_data="""
                    {
                        "stage_title": "Stage 1 - Understanding Data Fundamentals",
                        "focus": "Introduce the foundational concepts of data analysis that are crucial for decision-making in financial services.",
                        "outcome": "Gain a solid understanding of key data concepts, types, and the data analysis process.",
                        "duration": "60 minutes",
                        "topics_covered": [
                            "Introduction to Data Analysis",
                            "Importance of Data in Decision Making",
                            "Key Terminology: Data, Information, Metrics",
                            "Understanding Data Types: Qualitative vs Quantitative",
                            "Data Collection Methods: Surveys, Interviews, Observations",
                            "The Data Analysis Process: Steps Involved"
                        ]
                    }
                """,
                content="""
                ## Introduction
                Welcome to Stage 1 of our course, "Enhancing Data Analysis Skills for Product Management." In this stage, we will embark on a journey to understand the fundamentals of data analysis and its critical role in decision-making, particularly within the realm of product management. As we navigate through this content, you will gain foundational knowledge that will empower you to leverage data effectively in your career advancement.

                ## Main Content
                ### Understanding Data Analysis
                Data analysis is the process of inspecting, cleansing, transforming, and modeling data with the goal of discovering useful information, informing conclusions, and supporting decision-making. In the context of product management, data analysis is not just a technical skill; it is a vital component that drives strategic decisions and enhances product development.

                ### Importance in Decision-Making
                In today’s technology-driven landscape, the ability to analyze data effectively can set you apart as a product manager. Data-driven decision-making allows you to base your strategies on empirical evidence rather than intuition alone. This approach minimizes risks and maximizes the potential for success. For instance, by analyzing customer feedback data, you can identify trends and pain points that inform product enhancements, ensuring that your offerings align with market needs.

                ### Key Concepts in Data Analysis
                1. **Descriptive Analytics**: This involves summarizing historical data to understand what has happened in the past. For example, analyzing sales data from the previous quarter can help you identify which products performed well and which did not.

                2. **Predictive Analytics**: This type of analysis uses statistical models and machine learning techniques to forecast future outcomes based on historical data. For instance, by examining user behavior patterns, you can predict which features will be most popular in your next product release.

                3. **Prescriptive Analytics**: This goes a step further by recommending actions based on the analysis. It helps you understand the best course of action to take in a given situation. For example, if data shows a decline in user engagement, prescriptive analytics can suggest targeted marketing strategies to re-engage users.

                ### Relevance in Product Management
                As a product manager at IBM, where the focus is on data analytics and cloud architecture, understanding these concepts is crucial. Your tech stack, which includes React, Node.js, and MongoDB, provides a robust foundation for implementing data analysis techniques. By integrating data analysis into your product management processes, you can enhance customer service and drive business growth.

                ## Conclusion
                In conclusion, mastering the fundamentals of data analysis is essential for your career advancement in product management. By understanding the importance of data in decision-making and familiarizing yourself with key analytical concepts, you will be better equipped to make informed decisions that positively impact your products and your organization. As you move forward, remember that data is not just numbers; it tells a story that can guide your strategic initiatives.

                ## Reflection Questions
                1. How can you apply the concepts of descriptive, predictive, and prescriptive analytics in your current role?
                2. What specific data sources do you have access to that could enhance your decision-making process?
                3. In what ways can you advocate for a data-driven culture within your team or organization?
                """,
                enriched_content="""
                ## Introduction  
                Welcome to Stage 1 of "Data Analytics for Business Leaders: A User Journey for Vice Presidents in Financial Services." In this stage, we will explore the foundational concepts of data analysis crucial for effective decision-making in the financial services sector. Understanding data is not just a technical skill; it's a key driver of strategic business outcomes. So, let's embark on this journey together, enhancing your career advancement potential through a solid grasp of data fundamentals.

                ## Main Content  

                ### Introduction to Data Analysis  
                Data analysis is the process of inspecting, cleansing, transforming, and modeling data with the goal of discovering useful information, drawing conclusions, and supporting decision-making. In financial services, where every decision can have significant ramifications, a thorough understanding of data analysis is not just beneficial; it’s essential. Imagine making a strategic decision based on solid data insights rather than intuition alone. This stage will empower you to do just that. For example, a financial analyst might evaluate market trends by analyzing historical stock performance data, allowing them to predict future movements and make informed investment choices.

                ### Importance of Data in Decision Making  
                Data plays a pivotal role in decision-making, particularly in the financial services industry, where accuracy can mean the difference between profit and loss. For instance, consider a bank assessing the creditworthiness of a loan applicant. By utilizing historical data and analytics, the bank can make informed, data-driven decisions that minimize risk. When leaders like you harness data effectively, they elevate their organizations’ performance and foster a culture of informed decisions. A practical example could be a financial institution using customer transaction data to identify spending patterns, enabling personalized marketing strategies that enhance customer retention.

                ### Key Terminology: Data, Information, Metrics  
                Let’s clarify some key terms. **Data** refers to raw facts and figures without context, while **information** is data that has been processed and organized to provide meaning. For example, the number of loan applications received is data, but the percentage of approved loans represents useful information. **Metrics** are quantifiable measures used to track performance and progress toward goals. Understanding these distinctions is vital for effective communication and decision-making in your organization. For example, a metric such as the Customer Acquisition Cost (CAC) can help financial leaders assess the efficiency of their marketing efforts.

                ### Understanding Data Types: Qualitative vs Quantitative  
                Next, we’ll explore data types. Data can be classified into two primary categories: **qualitative** and **quantitative**. Qualitative data is descriptive and conceptual. For instance, customer feedback on service quality is qualitative. Conversely, quantitative data is numerical and can be measured. An example would be the total revenue generated in a quarter. Recognizing which type of data you're dealing with can shape your analysis and outcomes. For example, a bank might use qualitative data from customer satisfaction surveys alongside quantitative data from transaction volumes to get a comprehensive view of customer experience.

                ### Data Collection Methods: Surveys, Interviews, Observations  
                Now, let’s look at how we gather data. There are various methods, including surveys, interviews, and observations. Surveys can provide vast amounts of data quickly, but they must be carefully crafted to avoid bias. Interviews offer deeper insights but can be time-consuming. Observations allow for real-time data collection, which is particularly valuable in understanding customer behavior. Utilizing the right method is crucial for obtaining relevant data that drives insights. For example, a bank might conduct surveys to learn about customer preferences while also observing how customers interact with their online banking platform to identify areas for improvement.

                ### The Data Analysis Process: Steps Involved  
                Finally, we’ll discuss the data analysis process itself. This process generally involves several key steps: defining your question, collecting data, cleaning and organizing data, analyzing the data, and finally interpreting the results. Think of it as a journey—each step is interconnected and builds upon the previous one. For instance, if your data collection is flawed, your analysis will also suffer. A systematic approach ensures that your insights are reliable and actionable. An example in a financial context could be analyzing customer churn rates: defining the problem, collecting customer feedback data, cleaning it to remove duplicates, and then analyzing trends to develop effective retention strategies.

                ## Conclusion  
                In this stage, we've laid the groundwork for understanding data fundamentals. From the importance of data in decision-making to the nuances of different data types and collection methods, you now have a solid foundation to build upon. These concepts will not only enhance your analytical skills but also empower you to lead your teams in making informed, data-driven decisions.

                ## Reflection Questions  
                1. How can you apply the concepts of data types and collection methods in your current role to improve decision-making?  
                2. Reflect on a recent decision made in your organization. How could data analysis have changed the outcome?  
                3. What steps will you take to deepen your understanding of data analysis as part of your career advancement journey?  

                Thank you for joining me in this exploration of data fundamentals, and I look forward to seeing you in the next stage!
                """
                ).with_inputs("topic_sections", "main_heading", "stage_data", "content"),
            dspy.Example(
                topic_sections="""Understanding Data Analysis,
                Importance in Decision-Making ,
                Key Concepts in Data Analysis,
                Relevance in Product Management,
                """,
                main_heading="Enhancing Data Analysis Skills for Product Management",
                stage_data="""
                    {
                    "stage_title": "Stage 2 - Data Analysis Tools and Techniques",
                    "focus": "Familiarization with essential data analysis tools and techniques.",
                    "outcome": "Learn to utilize various tools and techniques for effective data analysis.",
                    "duration": "90 minutes",
                    "topics_covered": [
                        "Overview of Data Analysis Tools (e.g., Excel, Tableau, Power BI)",
                        "Introduction to Statistical Analysis",
                        "Data Visualization Techniques",
                        "Basics of SQL for Data Retrieval"
                    ]
                }
                """,
                content="""
                ## Introduction
                Welcome to Stage 1 of our course, "Enhancing Data Analysis Skills for Product Management." In this stage, we will embark on a journey to understand the fundamentals of data analysis and its critical role in decision-making, particularly within the realm of product management. As we navigate through this content, you will gain foundational knowledge that will empower you to leverage data effectively in your career advancement.

                ## Main Content
                ### Understanding Data Analysis
                Data analysis is the process of inspecting, cleansing, transforming, and modeling data with the goal of discovering useful information, informing conclusions, and supporting decision-making. In the context of product management, data analysis is not just a technical skill; it is a vital component that drives strategic decisions and enhances product development.

                ### Importance in Decision-Making
                In today’s technology-driven landscape, the ability to analyze data effectively can set you apart as a product manager. Data-driven decision-making allows you to base your strategies on empirical evidence rather than intuition alone. This approach minimizes risks and maximizes the potential for success. For instance, by analyzing customer feedback data, you can identify trends and pain points that inform product enhancements, ensuring that your offerings align with market needs.

                ### Key Concepts in Data Analysis
                1. **Descriptive Analytics**: This involves summarizing historical data to understand what has happened in the past. For example, analyzing sales data from the previous quarter can help you identify which products performed well and which did not.

                2. **Predictive Analytics**: This type of analysis uses statistical models and machine learning techniques to forecast future outcomes based on historical data. For instance, by examining user behavior patterns, you can predict which features will be most popular in your next product release.

                3. **Prescriptive Analytics**: This goes a step further by recommending actions based on the analysis. It helps you understand the best course of action to take in a given situation. For example, if data shows a decline in user engagement, prescriptive analytics can suggest targeted marketing strategies to re-engage users.

                ### Relevance in Product Management
                As a product manager at IBM, where the focus is on data analytics and cloud architecture, understanding these concepts is crucial. Your tech stack, which includes React, Node.js, and MongoDB, provides a robust foundation for implementing data analysis techniques. By integrating data analysis into your product management processes, you can enhance customer service and drive business growth.

                ## Conclusion
                In conclusion, mastering the fundamentals of data analysis is essential for your career advancement in product management. By understanding the importance of data in decision-making and familiarizing yourself with key analytical concepts, you will be better equipped to make informed decisions that positively impact your products and your organization. As you move forward, remember that data is not just numbers; it tells a story that can guide your strategic initiatives.

                ## Reflection Questions
                1. How can you apply the concepts of descriptive, predictive, and prescriptive analytics in your current role?
                2. What specific data sources do you have access to that could enhance your decision-making process?
                3. In what ways can you advocate for a data-driven culture within your team or organization?
                """,
                enriched_content="""
                ## Introduction  
                Welcome to Stage 1: Fundamentals of Data Analysis. In this session, we will explore the core concepts of data analysis and understand its significance in the financial services industry. As a Vice President in this field, mastering data analytics is not just an advantage; it’s a necessity for career advancement. In the next 90 minutes, we will equip you with the knowledge to articulate the importance of data-driven decision-making and help you recognize various types of data. 

                ## Main Content  

                ### Introduction to Data Analysis  
                Let’s begin with an overview of data analysis. At its core, data analysis is the process of inspecting, cleansing, transforming, and modeling data with the goal of discovering useful information, suggesting conclusions, and supporting decision-making. In the financial services sector, this could mean analyzing customer transaction data to identify spending trends, which can inform marketing strategies or risk assessments. For example, a bank might analyze data from credit card transactions to pinpoint which demographics are most likely to respond to a promotional campaign, allowing for more targeted marketing efforts.

                ### Importance of Data in Decision Making  
                Now, why is data crucial for decision-making? In the fast-paced world of financial services, decisions based on intuition alone can lead to costly mistakes. Data-driven decision-making allows leaders to make informed choices backed by evidence. For example, a bank may analyze customer feedback data to improve services, ultimately increasing customer satisfaction and retention. Additionally, when investment firms evaluate market trends using historical data, they can make more strategic investment decisions, thereby enhancing profitability and minimizing risks.

                ### Key Terminology: Data, Information, Metrics  
                Understanding the terminology is vital for effective communication in data analysis. Data refers to raw facts and figures, while information is data that has been processed and organized in a meaningful way. Metrics, on the other hand, are quantifiable measures that help us assess performance against our objectives. Imagine a financial institution analyzing the metric of loan approval rates. This metric can provide insight into the effectiveness of their lending criteria. Another example could be analyzing the average time it takes to process a loan application, which could highlight areas for operational improvement.

                ### Understanding Data Types: Qualitative vs Quantitative  
                Next, we differentiate between qualitative and quantitative data. Qualitative data is descriptive and can provide insights into customer opinions and behaviors. For instance, customer interviews can reveal why clients prefer a specific service. Conversely, quantitative data can be measured and expressed numerically. An example here would be the number of transactions processed in a month, providing a clear picture of business volume. Additionally, qualitative data can help explain the "why" behind the numbers, such as understanding customer dissatisfaction that may not be immediately evident from quantitative metrics alone.

                ### Data Collection Methods: Surveys, Interviews, Transaction Data  
                Finally, let’s discuss data collection methods. Surveys are one of the most common tools used to gather qualitative data. They can be designed to capture customer feedback effectively. Interviews provide deeper insights, allowing for a more nuanced understanding of customer experiences. On the other hand, transaction data is a powerful quantitative source that can reveal trends over time, such as seasonal spending patterns or the impact of economic shifts on customer behavior. Another method to consider is observational data, where analysts can observe customer interactions in real-time, providing context that surveys or interviews may miss.

                ## Conclusion  
                As we wrap up this stage, remember that data analysis is foundational in the financial services industry. Understanding its principles can empower you to make informed, strategic decisions that drive business growth. The ability to differentiate between types of data and employ various collection methods will serve you well in your career advancement. 

                ## Reflection Questions  
                1. How can you leverage data analysis in your current role to enhance decision-making?  
                2. What types of data do you currently collect, and how can you improve your data collection methods?  
                3. Reflect on a recent decision you made: could it have benefited from a data-driven approach?  

                Thank you for joining this session on the Fundamentals of Data Analysis. We look forward to continuing this journey with you as we delve deeper into the world of data analytics.
                """
                ).with_inputs("topic_sections", "main_heading", "stage_data", "content"),

        ]

        # Metric: ensure enriched content keeps markdown structure and examples
        def review_metric(gold, pred, trace=None):
            return int(
                "##" in pred.enriched_content
                and len(pred.enriched_content) >= len(gold.enriched_content)
            )
        optimizer = dspy.BootstrapFewShot(metric=review_metric)
        # optimizer.compile(predictor, trainset=trainset)
        self.optimized_predictor = optimizer.compile(predictor, trainset=trainset)

        # self.optimized_predictor = predictor

    def forward(self, topic_sections, main_heading, stage_data, content):
        return self.optimized_predictor(
            topic_sections=topic_sections,
            main_heading=main_heading,
            stage_data=stage_data,
            content=content
        )


# === Tool Input Schema ===
class ContentReviewerInput(BaseModel):
    topic_sections: str = Field(..., description="Sections to review")
    main_heading: str = Field(..., description="Main topic heading")
    stage_data: str = Field(..., description="User journey syllabus data for the stage")
    content: str = Field(..., description="Already created content in markdown")


# === Custom DSPy Tool ===
class ContentReviewerTool(BaseTool):
    name: str = "Content Reviewer Tool"
    description: str = (
        "Reviews and enriches existing content based on a user journey syllabus "
        "and topic sections, outputting updated markdown only."
    )
    args_schema: Type[BaseModel] = ContentReviewerInput

    def _run(self, topic_sections, main_heading, stage_data, content) -> str:
        reviewer = ContentReviewerModule()
        result = reviewer.forward(
            topic_sections=topic_sections,
            main_heading=main_heading,
            stage_data=stage_data,
            content=content
        )
        return result.enriched_content


