import markdown
from flask import Blueprint, request, Response

document = Blueprint('document', __name__, url_prefix='/document')

@document.route('/')
@document.route('')
def docs():
    """
    Reads the API documentation from a Markdown file, converts it to HTML,
    and serves it with a modern, readable stylesheet.
    """
    try:
        with open('./API_DOCUMENTATION.md', 'r', encoding='utf-8') as f:
            content = f.read()
    except FileNotFoundError:
        return "Error: API_DOCUMENTATION.md not found.", 404

    # Use extensions for tables, fenced code blocks, and code highlighting
    # 'codehilite' requires the Pygments library to be installed
    extensions = ['fenced_code', 'tables', 'codehilite']
    html_content = markdown.markdown(content, extensions=extensions)

    # A more robust and beautiful HTML template with embedded CSS
    html_template = f"""
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>YDM C-360 API Documentation</title>
        <style>
            /* --- General & Reset --- */
            body {{
                font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
                line-height: 1.6;
                color: #333;
                background-color: #fdfdfd;
                max-width: 960px;
                margin: 0 auto;
                padding: 2rem;
            }}

            /* --- Typography --- */
            h1, h2, h3, h4, h5, h6 {{
                color: #2c3e50;
                font-weight: 600;
                margin-top: 2.5em;
                margin-bottom: 1em;
            }}
            h1 {{ border-bottom: 2px solid #ecf0f1; padding-bottom: 0.3em; }}
            h2 {{ border-bottom: 1px solid #ecf0f1; padding-bottom: 0.3em; }}
            a {{ color: #3498db; text-decoration: none; }}
            a:hover {{ text-decoration: underline; }}
            
            /* --- Code Blocks & Syntax Highlighting (from Pygments) --- */
            pre {{
                background-color: #2d2d2d;
                color: #f8f8f2;
                border: 1px solid #ddd;
                border-radius: 6px;
                padding: 1em;
                overflow-x: auto;
            }}
            code {{
                font-family: "Fira Code", "Courier New", monospace;
                background-color: #ecf0f1;
                color: #c0392b;
                padding: .2em .4em;
                border-radius: 3px;
                font-size: 0.9em;
            }}
            pre code {{
                background-color: transparent;
                color: inherit;
                padding: 0;
                border-radius: 0;
            }}
            .codehilite .hll {{ background-color: #49483e }}
            .codehilite .c {{ color: #75715e }} /* Comment */
            .codehilite .err {{ color: #960050; background-color: #1e0010 }} /* Error */
            .codehilite .k {{ color: #66d9ef }} /* Keyword */
            .codehilite .l {{ color: #ae81ff }} /* Literal */
            .codehilite .n {{ color: #f8f8f2 }} /* Name */
            .codehilite .o {{ color: #f92672 }} /* Operator */
            .codehilite .p {{ color: #f8f8f2 }} /* Punctuation */
            .codehilite .s {{ color: #e6db74 }} /* String */
            .codehilite .na {{ color: #a6e22e }} /* Name.Attribute */
            .codehilite .nb {{ color: #f8f8f2 }} /* Name.Builtin */
            .codehilite .nc {{ color: #a6e22e }} /* Name.Class */
            .codehilite .ne {{ color: #a6e22e }} /* Name.Exception */
            .codehilite .nf {{ color: #a6e22e }} /* Name.Function */
            .codehilite .nl {{ color: #f8f8f2 }} /* Name.Label */
            .codehilite .nt {{ color: #f92672 }} /* Name.Tag */
            .codehilite .nv {{ color: #f8f8f2 }} /* Name.Variable */
            .codehilite .si {{ color: #e6db74 }} /* String.Interpol */
            .codehilite .w {{ color: #f8f8f2 }} /* Text.Whitespace */

            /* --- Tables --- */
            table {{
                width: 100%;
                border-collapse: collapse;
                margin: 1em 0;
                box-shadow: 0 1px 3px rgba(0,0,0,0.1);
            }}
            th, td {{
                padding: 12px 15px;
                border: 1px solid #dfe6e9;
                text-align: left;
            }}
            th {{
                background-color: #ecf0f1;
                font-weight: 600;
            }}
            tr:nth-of-type(even) {{
                background-color: #f9fafb;
            }}

            /* --- Other Elements --- */
            hr {{
                border: none;
                border-top: 2px solid #ecf0f1;
                margin: 2em 0;
            }}
            blockquote {{
                border-left: 5px solid #bdc3c7;
                background-color: #ecf0f1;
                color: #555;
                padding: 1em 1.5em;
                margin: 1.5em 0;
            }}
            img {{
                max-width: 100%;
                height: auto;
            }}
            
            /* --- Collapsible Details/Summary --- */
            details {{
                margin: 1.5em 0;
                background: #fafafa;
                border-radius: 6px;
                border: 1px solid #dfe6e9;
            }}
            summary {{
                cursor: pointer;
                padding: 1em;
                font-weight: 600;
                color: #2c3e50;
                outline: none;
            }}
            details[open] > summary {{
                border-bottom: 1px solid #dfe6e9;
            }}
            details > div {{
                padding: 0 1em 1em 1em;
            }}
        </style>
    </head>
    <body>
        {html_content}
    </body>
    </html>
    """
    return Response(html_template, mimetype='text/html')