import os
import logging
from typing import List, Dict
import docx
import markdown

# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')

def read_documents(directory: str) -> List[str]:
    """
    Reads all .md and .docx files in the specified directory and returns their content as a list of strings.
    """
    documents = []
    for filename in os.listdir(directory):
        filepath = os.path.join(directory, filename)
        try:
            if filename.endswith(".md"):
                with open(filepath, "r", encoding="utf-8") as f:
                    documents.append(f.read())
            elif filename.endswith(".docx"):
                doc = docx.Document(filepath)
                full_text = []
                for para in doc.paragraphs:
                    full_text.append(para.text)
                documents.append('\n'.join(full_text))
        except Exception as e:
            logging.error(f"Error reading file {filename}: {e}")
    return documents

def extract_insights(documents: List[str]) -> Dict[str, List[str]]:
    """
    Extracts methodologies, templates, and best practices from the given documents.
    This is a basic implementation using keyword searches.
    """
    methodologies = []
    templates = []
    best_practices = []

    for doc in documents:
        if "methodology" in doc.lower() or "approach" in doc.lower():
            methodologies.append(doc)
        if "template" in doc.lower() or "example" in doc.lower():
            templates.append(doc)
        if "best practice" in doc.lower() or "guideline" in doc.lower():
            best_practices.append(doc)

    return {
        "methodologies": methodologies,
        "templates": templates,
        "best_practices": best_practices,
    }

def suggest_prompt_improvements(insights: Dict[str, List[str]]) -> List[str]:
    """
    Suggests improvements to the Ralph system prompt based on the extracted insights.
    """
    improvements = []
    if insights["methodologies"]:
        improvements.append("Incorporate the following methodologies into your task execution strategy:")
        for methodology in insights["methodologies"]:
            improvements.append(f"- {methodology[:200]}...")  # Limit to 200 chars for brevity

    if insights["templates"]:
        improvements.append("Utilize these templates for structuring your outputs:")
        for template in insights["templates"]:
            improvements.append(f"- {template[:200]}...")

    if insights["best_practices"]:
        improvements.append("Adhere to the following best practices during task execution:")
        for practice in insights["best_practices"]:
            improvements.append(f"- {practice[:200]}...")

    return improvements

def consolidate_knowledge(insights: Dict[str, List[str]], prompt_improvements: List[str]) -> str:
    """
    Consolidates the extracted knowledge and prompt improvements into a single string.
    """
    knowledge = f"""\"\"\"
This file contains the consolidated knowledge extracted from the RALPH WIGGUM documents.
It includes methodologies, templates, best practices, and suggested improvements to the Ralph system prompt.
\"\"\"

# Methodologies
methodologies = {insights["methodologies"]}

# Templates
templates = {insights["templates"]}

# Best Practices
best_practices = {insights["best_practices"]}

# Suggested Prompt Improvements
prompt_improvements = {prompt_improvements}
"""
    return knowledge

def write_to_file(filepath: str, content: str) -> None:
    """
    Writes the given content to the specified file.
    """
    try:
        with open(filepath, "w", encoding="utf-8") as f:
            f.write(content)
        logging.info(f"Successfully wrote to {filepath}")
    except Exception as e:
        logging.error(f"Error writing to file {filepath}: {e}")


if __name__ == "__main__":
    ralph_wiggum_dir = "/mnt/e/genesis-system/RALPH WIGGUM/"
    output_file = "/mnt/e/genesis-system/core/knowledge/ralph_methodology.py"

    documents = read_documents(ralph_wiggum_dir)
    insights = extract_insights(documents)
    prompt_improvements = suggest_prompt_improvements(insights)
    knowledge = consolidate_knowledge(insights, prompt_improvements)
    write_to_file(output_file, knowledge)