import os
import logging

logging.basicConfig(level=logging.INFO)


def generate_api_documentation(api_endpoints: list[dict], output_path: str) -> None:
    """
    Generates API documentation in Markdown format from a list of API endpoint dictionaries.

    Args:
        api_endpoints: A list of dictionaries, where each dictionary represents an API endpoint.
                       Each dictionary should have keys like 'path', 'method', 'description', and 'parameters'.
        output_path: The path to the output Markdown file.

    Returns:
        None.  Writes the Markdown content to the specified file.
    """

    try:
        markdown_content = "# API Documentation\n\n"
        for endpoint in api_endpoints:
            markdown_content += f"## {endpoint['method']} {endpoint['path']}\n\n"
            markdown_content += f"{endpoint['description']}\n\n"

            if 'parameters' in endpoint:
                markdown_content += "### Parameters\n\n"
                for param in endpoint['parameters']:
                    markdown_content += f"- `{param['name']}`: {param['type']} - {param['description']}\n"
                markdown_content += "\n"

        # Ensure the directory exists
        os.makedirs(os.path.dirname(output_path), exist_ok=True)

        with open(output_path, "w") as f:
            f.write(markdown_content)

        logging.info(f"API documentation generated successfully at {output_path}")

    except Exception as e:
        logging.error(f"Error generating API documentation: {e}")


def main():
    """
    Main function to define API endpoints and generate documentation.
    """

    api_endpoints = [
        {
            "path": "/users",
            "method": "GET",
            "description": "Retrieves a list of all users.",
            "parameters": []
        },
        {
            "path": "/users/{user_id}",
            "method": "GET",
            "description": "Retrieves a specific user by ID.",
            "parameters": [
                {"name": "user_id", "type": "integer", "description": "The ID of the user to retrieve."}
            ]
        },
        {
            "path": "/items",
            "method": "POST",
            "description": "Creates a new item.",
            "parameters": [
                {"name": "name", "type": "string", "description": "The name of the item."},
                {"name": "description", "type": "string", "description": "A description of the item."}
            ]
        }
    ]

    output_path = "/mnt/e/genesis-system/docs/api.md"
    generate_api_documentation(api_endpoints, output_path)


if __name__ == "__main__":
    main()