from fastapi import HTTPException
from collections import defaultdict
import os
import re
from ..utils.hashing import HashGenerator
from user_journey_service.crew import UserJourney 
from user_journey_service.processors.summary_generator import SummaryGenerator
summary_generator = SummaryGenerator()




class SummaryCreationService:
    def __init__(self,input_data):
        self.crew_instance = UserJourney()
        self.input_data = input_data
        self.input_hash = HashGenerator.generate_input_hash(input_data)
        self.output_file = f"output/{self.input_hash}.md"
        self.summary_file = f"summary/{self.input_hash}.md"

    def run(self):
        print("Inside def run function...")
        exists,result = self._fetch_existing_result(self.summary_file)
        # if exists:
        #     print("Inside if of main function")
        #     output = self._restructure_output(result)
        #     return {"status": "success", "message": "Fetched existing result.","output":output}
        
        if self.isStructured():
            exists,result = self._fetch_existing_result(self.summary_file)
            if exists:
                output = self._restructure_output_structured(result)
                return {"status": "success", "message": "Fetch existing result.","output":output}
            self.create_summary_structured()
            exists,result = self._fetch_existing_result(self.summary_file)
            output = self._restructure_output_structured(result)
            return {"status": "success", "message": "Execution completed.","output":output}
        else:
            exists,result = self._fetch_existing_result(self.summary_file)
            if exists:
                output = self._restructure_output_ondemand(result)
                return {"status": "success", "message": "Fetch existing result.","output":output}
            self.create_summary_ondemand()
            exists,result = self._fetch_existing_result(self.summary_file)
            output = self._restructure_output_ondemand(result)
            return {"status": "success", "message": "Execution completed.","output":output}

    def isStructured(self):
        print(f"Inside journey type : {self.input_data.journey}")
        if self.input_data.journey =="Structured Learning":
            return True
        return False

    def create_summary_structured(self):
        inputs = self.input_data.dict()
        with open(self.output_file, 'r', encoding='utf-8') as f:
            content = f.read()
        # summary_generator.structured_learning(content)
        inputs["content"] = content
        crew = self.crew_instance.structured_crew(output_file=self.summary_file)
        crew.kickoff(inputs=inputs)


    def create_summary_ondemand(self):
        print("Inside on demand journey function..")
        inputs = self.input_data.dict()
        with open(self.output_file, 'r', encoding='utf-8') as f:
            content = f.read()
        # summary_generator.ondemand_learning(content)
        inputs["content"] = content
        crew = self.crew_instance.ondemand_crew(output_file=self.summary_file)
        crew.kickoff(inputs=inputs)

    def _fetch_existing_result(self,file_path):
        print("Inside fetch result function")
        if os.path.exists(file_path):
            print("Inside if condition")
            with open(file_path, 'r') as file:
                result = file.read()
                print(f"The fetched result is : {result}")
                print(f"The type of result is : {type(result)}")
            # print(f"The output is : {result}")
            return True, result
        return False, ""

    def _restructure_output_structured(self,result: str) -> dict:
        try:
            print("Inside restructure output function")
            print(f"The type of result is : {type(result)}")
            print(f"Result content: {repr(result)}")
            matches = re.findall(r"##\s*([\w\s]+?)\s*-\s*(\d+)%",result)

            print("match found")
            # Convert to dictionary
            return {skill.strip(): int(percent) for skill, percent in matches}
        except Exception as e:
            print(e)
            return {}
    def _restructure_output_ondemand(self,md_text: str) -> dict:
        lines = md_text.strip().splitlines()
        data = defaultdict(list)
        current_section = None
        temp_skill = {}

        for line in lines:
            line = line.strip()

            # Detect section headers
            if line.startswith("## "):
                current_section = line[3:].strip()
                continue

            # Parse Skill Gaps section
            if current_section == "Skill Gaps":
                if "**Skill:**" in line:
                    skill = re.search(r"\*\*Skill:\*\*\s*(.+)", line)
                    if skill:
                        temp_skill = {"skill": skill.group(1).strip()}
                elif "- Reason:" in line:
                    reason = re.search(r"- Reason:\s*(.+)", line)
                    if reason and temp_skill:
                        temp_skill["reason"] = reason.group(1).strip()
                        data[current_section].append(temp_skill)
                        temp_skill = {}

            # Parse Recommended Modules section
            elif current_section == "Recommended Modules":
                module_match = re.match(r"\d+\.\s+\*\*(.+?)\*\* \(Stage: (.+?)\)", line)
                if module_match:
                    module_title, stage = module_match.groups()
                    module = {"module_title": module_title.strip(), "stage": stage.strip()}
                    data[current_section].append(module)
                elif "- Why Relevant:" in line and data[current_section]:
                    why_relevant = re.search(r"- Why Relevant:\s*(.+)", line)
                    if why_relevant:
                        data[current_section][-1]["why_relevant"] = why_relevant.group(1).strip()

            # Parse simple list sections
            elif current_section in {
                "Personalization Reasons", "Core foundation", "Applied Skills", "Business Context"
            }:
                bullet_match = re.match(r"-\s*(.+)", line)
                if bullet_match:
                    data[current_section].append(bullet_match.group(1).strip())

        return dict(data)