// =============================================================================
// Boko Example App — demonstrates the modern framework API
// =============================================================================
module Main {

    import Std::Io::{PrintLine, Print};
    import Std::String::{
        String_Eq, String_Len,
        StringBuilder, StringBuilder_New, StringBuilder_Append,
        StringBuilder_Build, StringBuilder_Free
    };
    import Boko::{
        App, App_New, App_Run,
        Request, Response,
        Response_Html, Response_Json, Response_NotFound, Response_Redirect,
        Path_Match,
        HttpVerb
    };

    // =============================================================================
    // Boko_Router — user-defined dispatch (called by the framework)
    // =============================================================================
    func Boko_Router(req: Request) -> Response {
        // --- GET / ---
        if String_Eq(req.path, "/") && req.method.tag == HttpVerb_GET {
            return Response_Html(PageHome());
        }

        // --- GET /api/health ---
        if String_Eq(req.path, "/api/health") {
            return Response_Json("{\"status\":\"ok\",\"framework\":\"Boko\",\"version\":\"0.2.0\"}");
        }

        // --- GET /api/info ---
        if String_Eq(req.path, "/api/info") {
            return Response_Json("{\"name\":\"Boko\",\"language\":\"Bux\",\"inspiration\":\"FastAPI\",\"features\":[\"routing\",\"path-params\",\"query-params\",\"json\"]}");
        }

        // --- GET /hello?name=World ---
        if String_Eq(req.path, "/hello") {
            var name: String = req.GetQuery("name");
            if String_Len(name) == 0 { name = "World"; }
            let html: String = f"<h1>Hello, {name}!</h1>";
            return Response_Html(html);
        }

        // --- GET /users/{id} ---
        if Path_Match("/users/{id}", req.path, &req) {
            let id: String = req.GetPathParam("id");
            let sb: StringBuilder = StringBuilder_New();
            StringBuilder_Append(&sb, "{\"id\":");
            StringBuilder_Append(&sb, id);
            StringBuilder_Append(&sb, ",\"name\":\"User ");
            StringBuilder_Append(&sb, id);
            StringBuilder_Append(&sb, "\"}");
        let json: String = StringBuilder_Build(&sb);
        StringBuilder_Free(&sb);
        return Response_Json(json);
    }

    // --- GET /posts/{postId}/comments/{commentId} ---
    if Path_Match("/posts/{postId}/comments/{commentId}", req.path, &req) {
        let pid: String = req.GetPathParam("postId");
        let cid: String = req.GetPathParam("commentId");
        let sb: StringBuilder = StringBuilder_New();
        StringBuilder_Append(&sb, "{\"postId\":");
        StringBuilder_Append(&sb, pid);
        StringBuilder_Append(&sb, ",\"commentId\":");
        StringBuilder_Append(&sb, cid);
        StringBuilder_Append(&sb, ",\"text\":\"Great post!\"}");
        let json: String = StringBuilder_Build(&sb);
        StringBuilder_Free(&sb);
        return Response_Json(json);
    }

    // --- GET /redirect → 302 to / ---
    if String_Eq(req.path, "/redirect") {
        return Response_Redirect("/");
    }

    // --- POST /api/echo ---
    if String_Eq(req.path, "/api/echo") && req.method.tag == HttpVerb_POST {
        let sb: StringBuilder = StringBuilder_New();
        StringBuilder_Append(&sb, "{\"echo\":");
        StringBuilder_Append(&sb, req.body);
        StringBuilder_Append(&sb, "}");
        let json: String = StringBuilder_Build(&sb);
        StringBuilder_Free(&sb);
        return Response_Json(json);
    }

    // --- 404 ---
    return Response_NotFound();
}

// =============================================================================
// HTML landing page (raw multi-line string via backticks)
// =============================================================================
func PageHome() -> String {
    return `<!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Boko Framework</title>
    <style>
    *{margin:0;padding:0;box-sizing:border-box}
    body{font-family:system-ui,sans-serif;background:#0d1117;color:#c9d1d9;min-height:100vh;display:flex;align-items:center;justify-content:center}
    .card{background:#161b22;border:1px solid #30363d;border-radius:12px;padding:3rem;max-width:560px;width:100%}
    h1{color:#58a6ff;font-size:2rem;margin-bottom:.5rem}
    .tag{color:#8b949e;font-size:.9rem;margin-bottom:2rem}
    h3{color:#d2a8ff;margin:1.5rem 0 .75rem}
    .ep{display:flex;gap:1rem;padding:.35rem 0;font-family:monospace;font-size:.9rem}
    .ep .m{color:#3fb950;font-weight:bold;min-width:52px}
    .ep .p{color:#c9d1d9}
    .ep .d{color:#484f58;margin-left:auto}
    a{color:#58a6ff}
    </style>
    </head>
    <body>
    <div class="card">
    <h1>⚡ Boko</h1>
    <p class="tag">Async web framework for Bux — inspired by FastAPI</p>
    <h3>Try it</h3>
    <div class="ep"><span class="m">GET</span><span class="p"><a href="/hello?name=Bux">/hello?name=Bux</a></span><span class="d">query param</span></div>
    <div class="ep"><span class="m">GET</span><span class="p"><a href="/users/42">/users/42</a></span><span class="d">path param</span></div>
    <div class="ep"><span class="m">GET</span><span class="p"><a href="/posts/7/comments/3">/posts/7/comments/3</a></span><span class="d">multi params</span></div>
    <div class="ep"><span class="m">GET</span><span class="p"><a href="/redirect">/redirect</a></span><span class="d">302 → /</span></div>
    <div class="ep"><span class="m">GET</span><span class="p"><a href="/api/health">/api/health</a></span><span class="d">JSON</span></div>
    <div class="ep"><span class="m">GET</span><span class="p"><a href="/api/info">/api/info</a></span><span class="d">JSON</span></div>
    <h3>Features</h3>
    <div class="ep"><span class="m">✓</span><span class="p">Path routing with {params}</span></div>
    <div class="ep"><span class="m">✓</span><span class="p">Query parameter extraction</span></div>
    <div class="ep"><span class="m">✓</span><span class="p">JSON / HTML / Text responses</span></div>
    <div class="ep"><span class="m">✓</span><span class="p">Multi-threaded (configurable)</span></div>
    <div class="ep"><span class="m">✓</span><span class="p">Redirects (302)</span></div>
    <div class="ep"><span class="m">✓</span><span class="p">POST body access</span></div>
    </div>
    </body>
    </html>`;
}

// =============================================================================
// Main — start the server
// =============================================================================
func Main() -> int {
    let app: App = App_New(8080, 4);
    App_Run(&app);
    return 0;
}

} // module Main
