from crewai import LLM
from user_journey_service.core.config import settings
import yaml
from pathlib import Path

prompts_path = 'user_journey_service/config/prompts.yaml'



# from tools.custom_content_reviewer_tool import ContentReviewerTool


# class ContentReviewer:
#     def __init__(self):
#         self.tool = ContentReviewerTool()

#     def review_and_enrich_content(self, topic_sections, main_heading, stage_data, content):
#         return self.tool._run(
#             topic_sections=topic_sections,
#             main_heading=main_heading,
#             stage_data=stage_data,
#             content=content
#         )



class ContentReviewer:
   def __init__(self):
      self.llm = LLM(
          model=f"{settings.PROVIDER}/{settings.LLM1}",
          temperature=settings.TEMPERATURE,
          api_key=settings.OPENAI_KEY1,
          api_base=settings.ENDPOINT1,
          api_version=settings.API_VERSION1,
          seed=settings.SEED,                # For reproducible results
          timeout=settings.TIMEOUT
	   )
      self.prompts = self.load_prompts()

   def load_prompts(self):
        with open(Path(prompts_path), "r") as f:
            return yaml.safe_load(f)


   def review_and_enrich_content(self,topic_sections,main_heading,stage_data,content,word_count):
      print("Inside reviewer function....")
      prompt_template = self.prompts["content_reviewer"]
      prompt = prompt_template.format(
            main_heading=main_heading,
            stage_data=stage_data,
            content=content,
            topic_sections=topic_sections,
            word_count=word_count
        )

      #   prompt = f"""
      #   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

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

      # LLM message format for CrewAI
      messages = [{'role': 'user', 'content': prompt}]

      # Call the LLM
      response = self.llm.call(messages)

      return response