import graphviz
import logging
import os
from typing import Dict

logging.basicConfig(level=logging.INFO)


def create_architecture_diagram(components: Dict[str, str], output_path: str = "architecture_diagram.png") -> None:
    """
    Generates an architecture diagram using graphviz.

    Args:
        components: A dictionary where keys are component names and values are descriptions.
        output_path: The path to save the generated diagram. Defaults to "architecture_diagram.png".

    Returns:
        None
    """
    try:
        dot = graphviz.Digraph(comment='Genesis System Architecture')

        for component, description in components.items():
            dot.node(component, label=f"{component}\n{description}")

        # Define relationships between components (example)
        dot.edge('core', 'AIVA', label='Communicates with')
        dot.edge('core', 'loop', label='Manages')
        dot.edge('core', 'skills', label='Utilizes')
        dot.edge('core', 'swarms', label='Orchestrates')
        dot.edge('loop', 'skills', label='Assigns')


        dot.render(output_path, view=False, format='png', cleanup=True)  # Save as PNG
        logging.info(f"Architecture diagram saved to {output_path}")

    except graphviz.backend.ExecutableNotFound as e:
        logging.error(f"Graphviz executable not found: {e}")
    except Exception as e:
        logging.error(f"An error occurred: {e}")


def main():
    """
    Main function to define the architecture components and generate the diagram.
    """
    components = {
        "core": "Kernel, heartbeat, memory cortex, tool router",
        "AIVA": "AIVA Queen outputs and elevation sprint results",
        "loop": "Ralph-style task tracking and loop infrastructure",
        "skills": "Modular skill implementations",
        "swarms": "Multi-agent swarm orchestration"
    }

    create_architecture_diagram(components, "/mnt/e/genesis-system/core/docs/architecture_diagram")


if __name__ == "__main__":
    main()