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


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_deployment_id = os.getenv("AZURE_DEPLOYMENT_ID")
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.0,
)
dspy.configure(lm=lm)
# === DSPy Signature ===
class ResearchSignature(dspy.Signature):
    """
    You are a topic researcher for a user in a specific topic.
    Uncover cutting-edge developments about topic
    The user prefer the researched data to be in match with the company context. The company context is :
      Company Name : {Company_Name}
      Industry : {Industry}
      Company Size : {Company_Size}
      Business Model : {Business_Model}
      Top Use Cases : {Top_Use_Cases}
      Learning Focus : {Learning_Focus}
      Tech Stack : {Tech_stack}
      Compliance : {Compliance}
    
    Perform comprehensive research on the topic **{topic}**, keeping in mind the user's job title, skills, experience, and learning level
    the motive of the user to under go this research.
    Avoid going too deep into technical details — this content is intended for a non-technical or minimally technical audience seeking a well-rounded understanding of the topic.
    Identify and list relevant concepts, methods, tools, techniques, and subtopics that should be included in a structured user journey.
    Your research should cover essential fundamentals, applied tools, advanced topics (aligned with the {Level} level), 
    and current trends relevant to the user's job title **{Job_Title}** and skills **{Skills}** and motive **{motive}**.
  
    A list of researched user journey content items for the topic {topic} tailored to the {Level} level.
    Structure the result as:
    - **Topic Area**: Main area/category (e.g., Fundamentals, Applications)
      - **Subtopics/Concepts**: List of individual concepts or tools under each topic area.
    This is purely research-based content gathering for a non-technical or minimally technical user journey preparation.
       
    """
    topic: str = dspy.InputField()
    company_context: str = dspy.InputField()
    user_context: str = dspy.InputField()
    researched_content: str = dspy.OutputField(
        desc="Structured bullet list of topic areas and subtopics tailored to the user journey"
    )


# === DSPy Agent with Optimizer (2-shot training) ===
class ResearcherModule(dspy.Module):
    def __init__(self):
        super().__init__()
        predictor = dspy.Predict(ResearchSignature)

        # --- Two Example Training Shots ---
        trainset = [
            dspy.Example(
                topic="AI in Healthcare",
                company_context="""
                Company Name: MediCore Solutions
                Industry: Healthcare
                Company Size: 2000+ employees
                Business Model: B2B and B2C SaaS
                Top Use Cases: Predictive analytics, Patient monitoring
                Learning Focus: AI diagnostics and workflow automation
                Tech Stack: Python, PyTorch, AWS HealthLake
                Compliance: HIPAA, GDPR
                """,
                user_context="""
                Job Title: Healthcare Analyst
                Skills: Data Analysis, Clinical Research
                Level: Intermediate
                motive: Exploring AI applications to improve patient care
                """,
                researched_content="""
                - **Topic Area**: Fundamentals  
                - **Subtopics/Concepts**:  
                    - Introduction to Artificial Intelligence in Healthcare  
                    - Overview of Machine Learning and its Role in Patient Care  
                    - Types of AI: Supervised, Unsupervised, and Reinforcement Learning  
                    - Ethical Considerations in AI Applications  
                    - Understanding Data Privacy: HIPAA and GDPR Compliance  

                - **Topic Area**: Applications  
                - **Subtopics/Concepts**:  
                    - AI in Predictive Analytics: Enhancing Patient Outcomes  
                    - AI in Patient Monitoring: Wearables and Remote Monitoring  
                    - Use of Natural Language Processing (NLP) in Clinical Documentation  
                    - Chatbots and Virtual Health Assistants for Patient Engagement  
                    - Remote Diagnosis and Telemedicine Solutions  

                - **Topic Area**: Tools and Techniques  
                - **Subtopics/Concepts**:  
                    - Overview of Data Analytics Tools: Python and PyTorch for healthcare  
                    - Using AWS HealthLake for Data Management in Healthcare  
                    - Data Visualization Tools for Presenting AI Insights  
                    - Techniques in Data Preprocessing and Feature Engineering  
                    - Implementing AI Models for Real-Time Patient Monitoring  

                - **Topic Area**: Advanced Topics  
                - **Subtopics/Concepts**:  
                    - Deep Learning Applications in Medical Imaging  
                    - AI-Driven Decision Support Systems for Healthcare Professionals  
                    - Integrating AI into Existing Clinical Workflows  
                    - Case Studies on AI Success Stories in Patient Care  
                    - Challenges and Limitations of AI in Healthcare Practice  

                - **Topic Area**: Current Trends  
                - **Subtopics/Concepts**:  
                    - Rise of Telehealth Solutions and AI Integration  
                    - Innovations in Wearable Health Technology and AI  
                    - The Role of AI in Addressing Pandemic Challenges  
                    - The Future of AI in Personalized Medicine  
                    - Collaborations Between Tech Companies and Healthcare Institutions  

                This structured user journey content is designed to provide a comprehensive overview of AI in Healthcare, particularly aligned with the interests and learning objectives of a Healthcare Analyst looking to explore how AI can improve patient care.

                """
            ).with_inputs("topic", "company_context", "user_context"),

            dspy.Example(
                topic="Blockchain in FinTech",
                company_context="""
                Company Name: FinNext Global
                Industry: FinTech
                Company Size: 1000-1500 employees
                Business Model: B2B
                Top Use Cases: Cross-border payments, Fraud prevention
                Learning Focus: Blockchain adoption and market opportunities
                Tech Stack: Ethereum, Hyperledger, Azure Blockchain Service
                Compliance: KYC, AML
                """,
                user_context="""
                Job Title: Business Development Executive
                Skills: Partnerships, Client Engagement
                Level: Beginner
                motive: Understanding blockchain for business strategy alignment
                """,
                researched_content="""


                - **Topic Area**: Fundamentals  
                - **Subtopics/Concepts**:  
                    - Introduction to Blockchain: Understanding what blockchain technology is and its significance in the FinTech landscape.  
                    - Key Terms: Decentralization, ledgers, nodes, smart contracts, tokens, hash functions.  
                    - Differences between Blockchain Types: Public vs. Private vs. Consortium blockchains.  
                    - Basic Mechanisms: How transactions are processed and validated in a blockchain network.  
                    - Overview of Cryptography: Basic principles of how cryptography secures blockchain solutions.  

                - **Topic Area**: Applications in FinTech  
                - **Subtopics/Concepts**:  
                    - Cross-border Payments: How blockchain facilitates faster, cheaper, and transparent international money transfers.  
                    - Fraud Prevention: Use of blockchain to enhance security and traceability of transactions.  
                    - Digital Identity Management: Evaluating blockchain's role in KYC and AML compliance processes for identity verification.  
                    - Smart Contracts: Understanding automated contract execution and its applications in financial services. 

                - **Topic Area**: Tools & Platforms  
                - **Subtopics/Concepts**:  
                    - Overview of Ethereum and Hyperledger: Key differences, use cases, and their relevance in FinTech.  
                    - Azure Blockchain Service: Brief introduction to Microsoft’s blockchain service and its business applications.  
                    - Wallet Solutions: Insight into digital wallets and their importance for businesses.  
                    - Integration with Existing Systems: Exploring middleware and API solutions to connect blockchain with legacy systems.  

                - **Topic Area**: Market Opportunities  
                - **Subtopics/Concepts**:  
                    - Trends in Blockchain Adoption: Current and future trends driving blockchain in FinTech, including regulatory factors.  
                    - Competitive Landscape: Overview of companies and startups utilizing blockchain for competitive advantage.  
                    - Customer Engagement: How blockchain can improve client engagement through transparency and trust.  

                - **Topic Area**: Business Strategy Alignment  
                - **Subtopics/Concepts**:  
                    - Strategic Partnerships: Identifying potential partnerships in the blockchain ecosystem for business growth.  
                    - Business Models leveraging Blockchain: Understanding various B2B models, including payment solutions, lending, and insurance.  
                    - Measuring ROI: Exploring metrics and frameworks to assess the financial impact of blockchain initiatives.  

                - **Topic Area**: Future Developments  
                - **Subtopics/Concepts**:  
                    - Emerging Trends: Overview of upcoming blockchain technology trends such as NFTs, CBDCs (Central Bank Digital Currencies), and DeFi (Decentralized Finance).  
                    - Regulatory Landscape: Understanding evolving compliance standards and regulations impacting blockchain implementations.  
                    - Education and Skill Development: Resources and communities to explore for continuous learning in blockchain and FinTech.

                This structured user journey provides the necessary foundation and insight for a Business Development Executive at FinNext Global to understand blockchain's application in developing business strategies and enhancing client engagement in the FinTech sector.

                """
            ).with_inputs("topic", "company_context", "user_context"),
        ]

        # Metric: Simple keyword containment
        def research_metric(gold, pred, trace):
            return int(all(
                section.lower().split()[0] in pred.researched_content.lower()
                for section in ["fundamentals", "applications", "tools", "trends"]
            ))

        # Compile with 2-shot training
        optimizer = dspy.BootstrapFewShot(metric=research_metric)
        optimizer.compile(predictor, trainset=trainset)
        

        # Store optimized predictor
        self.research_predictor = predictor
        print("=== Optimized ===")
        print(predictor)

    def forward(self, topic, company_context, user_context):
        return self.research_predictor(
            topic=topic,
            company_context=company_context,
            user_context=user_context
        )


# === Tool Input Schema ===
class ResearchToolInput(BaseModel):
    """Input schema for ResearchAnalysisTool."""
    topic: str = Field(..., description="The topic to research")
    Company_Name: str = Field(..., description="Name of the company")
    Industry: str = Field(..., description="Industry of the company")
    Company_Size: str = Field(..., description="Size of the company")
    Business_Model: str = Field(..., description="Company's business model")
    Top_Use_Cases: str = Field(..., description="Top use cases the company focuses on")
    Learning_Focus: str = Field(..., description="Learning focus for the user")
    Tech_stack: str = Field(..., description="Company's tech stack")
    Compliance: str = Field(..., description="Relevant compliance requirements")
    Job_Title: str = Field(..., description="Job title of the user")
    Skills: str = Field(..., description="Skills of the user")
    Level: str = Field(..., description="Learning level (Beginner, Intermediate, Advanced)")
    motive: str = Field(..., description="The user's motive for research")


# === Custom DSPy Tool ===
class ResearchAnalysisTool(BaseTool):
    name: str = "Research Analysis Tool"
    description: str = (
        "Performs comprehensive research on a given topic while keeping in mind company context "
        "and user profile (job title, skills, level, and motive). Produces structured user journey "
        "content with topic areas and subtopics."
    )
    args_schema: Type[BaseModel] = ResearchToolInput

    def _run(
        self,
        topic: str,
        Company_Name: str,
        Industry: str,
        Company_Size: str,
        Business_Model: str,
        Top_Use_Cases: str,
        Learning_Focus: str,
        Tech_stack: str,
        Compliance: str,
        Job_Title: str,
        Skills: str,
        Level: str,
        motive: str
    ) -> str:

        # Build company + user context
        company_context = f"""
        Company Name: {Company_Name}
        Industry: {Industry}
        Company Size: {Company_Size}
        Business Model: {Business_Model}
        Top Use Cases: {Top_Use_Cases}
        Learning Focus: {Learning_Focus}
        Tech Stack: {Tech_stack}
        Compliance: {Compliance}
        """

        user_context = f"""
        Job Title: {Job_Title}
        Skills: {Skills}
        Level: {Level}
        Motive: {motive}
        """

        researcher = ResearcherModule()
        result = researcher.forward(topic, company_context, user_context)

        return result.researched_content



# ## Standalone Usage lines

# research_tool = ResearchAnalysisTool()

# # Run it
# result = research_tool._run(
#     topic="Generative AI",
#     Company_Name="TechNova",
#     Industry="Healthcare",
#     Company_Size="5000 employees",
#     Business_Model="B2B SaaS",
#     Top_Use_Cases="Clinical decision support, patient engagement",
#     Learning_Focus="AI for non-technical staff",
#     Tech_stack="Python, TensorFlow, Azure Cloud",
#     Compliance="HIPAA, GDPR",
#     Job_Title="Product Manager",    
#     Skills="Healthcare domain knowledge, basic analytics",
#     Level="Beginner",
#     motive="Understand AI use cases to build product strategy"
# )

# print(result)