// server.el — Neuron landing page server.
//
// Serves the pre-rendered index.html (generated by the El component pipeline)
// plus health check, founding-counter API, and static assets.
//
// Compile:
//   elc server.el > server.c
//   cc -std=c11 -O2 -lcurl -lpthread -o landing server.c el_runtime.c
//
// Run:
//   PORT=8080 LANDING_ROOT=./src ./landing
//
// Routes:
//   GET  /                   → index.html
//   GET  /health             → {"status":"ok"}
//   GET  /api/founding-count → {"sold":N,"total":N,"remaining":N}
//   GET  /assets/*           → static files under $LANDING_ROOT/assets/
//   GET  /brand/*            → static files under $LANDING_ROOT/assets/brand/
//   GET  /<other>            → 404 JSON

// ── Path helpers ──────────────────────────────────────────────────────────────

fn strip_query(path: String) -> String {
    let q: Int = str_index_of(path, "?")
    if q < 0 { return path }
    str_slice(path, 0, q)
}

fn root_dir() -> String {
    let r: String = env("LANDING_ROOT")
    if str_eq(r, "") { let r = "./src" }
    r
}

fn parse_port() -> Int {
    let p: String = env("PORT")
    if str_eq(p, "") { let p = "8080" }
    str_to_int(p)
}

// Pick a content type from a file extension. Cloud Run / browsers care.
fn content_type_for(path: String) -> String {
    if str_ends_with(path, ".html") { return "text/html; charset=utf-8" }
    if str_ends_with(path, ".css")  { return "text/css; charset=utf-8" }
    if str_ends_with(path, ".js")   { return "application/javascript; charset=utf-8" }
    if str_ends_with(path, ".json") { return "application/json; charset=utf-8" }
    if str_ends_with(path, ".png")  { return "image/png" }
    if str_ends_with(path, ".jpg")  { return "image/jpeg" }
    if str_ends_with(path, ".jpeg") { return "image/jpeg" }
    if str_ends_with(path, ".svg")  { return "image/svg+xml" }
    if str_ends_with(path, ".ico")  { return "image/x-icon" }
    if str_ends_with(path, ".webp") { return "image/webp" }
    if str_ends_with(path, ".woff2"){ return "font/woff2" }
    if str_ends_with(path, ".woff") { return "font/woff" }
    "application/octet-stream"
}

// ── Routes ────────────────────────────────────────────────────────────────────

fn route_health() -> String {
    "{\"status\":\"ok\",\"engine\":\"el-landing\"}"
}

fn route_founding_count() -> String {
    // Hardcoded for now; later wired to Stripe via http_get_with_headers.
    let sold: Int = 47
    let total: Int = 1000
    let remaining: Int = total - sold
    "{\"sold\":" + int_to_str(sold) + ",\"total\":" + int_to_str(total) + ",\"remaining\":" + int_to_str(remaining) + "}"
}

fn route_index() -> String {
    fs_read(root_dir() + "/index.html")
}

fn route_static(path: String) -> String {
    // path comes in like "/assets/foo.png" — read from <root>/assets/foo.png
    fs_read(root_dir() + path)
}

fn route_brand(path: String) -> String {
    // /brand/foo.png → <root>/assets/brand/foo.png
    let after: String = str_slice(path, 6, str_len(path))
    fs_read(root_dir() + "/assets/brand" + after)
}

fn err_404(path: String) -> String {
    "{\"error\":\"not found\",\"path\":\"" + path + "\"}"
}

// ── Dispatcher ────────────────────────────────────────────────────────────────

fn handle_request(method: String, path: String, body: String) -> String {
    let clean: String = strip_query(path)

    if !str_eq(method, "GET") {
        return "{\"error\":\"method not allowed\"}"
    }

    if str_eq(clean, "/") {
        return route_index()
    }
    if str_eq(clean, "/health") {
        return route_health()
    }
    if str_eq(clean, "/api/founding-count") {
        return route_founding_count()
    }
    if str_starts_with(clean, "/assets/") {
        return route_static(clean)
    }
    if str_starts_with(clean, "/brand/") {
        return route_brand(clean)
    }
    if str_eq(clean, "/legal/enterprise-terms") {
        return fs_read(root_dir() + "/enterprise-terms.html")
    }
    if str_eq(clean, "/legal/terms") {
        return fs_read(root_dir() + "/terms.html")
    }
    if str_eq(clean, "/about") {
        return fs_read(root_dir() + "/about.html")
    }

    err_404(clean)
}

// ── Entry ─────────────────────────────────────────────────────────────────────

let port: Int = parse_port()
println("[landing] root=" + root_dir())
println("[landing] listening on " + int_to_str(port))

http_set_handler("handle_request")
http_serve(port, "handle_request")
