// styles.el - Page shell: DOCTYPE, <head>, CSS, and the minimal
// inline <script> for scroll-reveal and nav scroll effect.
//
// This file contains NO application logic - only the outer HTML
// document structure and presentation layer.
//
// CSS lives in dist/page_css.c (pre-compiled C function) to avoid
// elc tokenization bugs: # stripped as comments, spaces stripped
// between tokens, quotes stripped from font names, etc.
//
// Complex scripts (GA inline, JSON-LD schema) also live in pre-compiled
// C functions for the same reason.

// Pre-compiled C functions — bypass elc tokenizer for CSS and complex scripts
extern fn page_css() -> String
extern fn page_ga_script() -> String
extern fn page_schema() -> String

extern fn _page_close_impl() -> String

fn page_close() -> String {
    return _page_close_impl()
}

// el-html vessel — extern declarations (implementations in dist/elhtml_impl.c)
extern fn el_meta(name: String, content: String) -> String
extern fn el_meta_charset(charset: String) -> String
extern fn el_link_stylesheet(href: String) -> String
extern fn el_script_src(src: String, defer_load: Bool) -> String
extern fn el_script_inline(js: String) -> String
extern fn el_title(text: String) -> String

// ── Shared head infrastructure ────────────────────────────────────────────────
// page_head_base() emits charset, viewport, favicons, fonts, CSS, scripts.
// page_seo_block() emits the SEO/OG/canonical block for a given page.
// page_head() assembles both for the homepage.
// page_open_seo() is the variant used by inner pages with custom meta.

fn page_head_base() -> String {
    return el_meta_charset("UTF-8")
        + el_meta("viewport", "width=device-width, initial-scale=1.0")
        + "<link rel=\"icon\" type=\"image/png\" sizes=\"16x16\" href=\"/assets/favicon-16.png\">"
        + "<link rel=\"icon\" type=\"image/png\" sizes=\"32x32\" href=\"/assets/favicon-32.png\">"
        + "<link rel=\"preconnect\" href=\"https://fonts.googleapis.com\">"
        + "<link rel=\"preconnect\" href=\"https://fonts.gstatic.com\" crossorigin>"
        + el_link_stylesheet("https://fonts.googleapis.com/css2?family=Playfair+Display:ital,wght@0,400;0,500;0,600;1,400;1,500&family=IBM+Plex+Sans:wght@300;400;500;600&display=swap")
        + page_css()
        + "<script src=\"https://cdn.jsdelivr.net/npm/marked/marked.min.js\" integrity=\"sha384-948ahk4ZmxYVYOc+rxN1H2gM1EJ2Duhp7uHtZ4WSLkV4Vtx5MUqnV+l7u9B+jFv+\" crossorigin=\"anonymous\" defer></script>"
        + "<script src=\"https://cdn.jsdelivr.net/npm/@supabase/supabase-js@2\" defer></script>"
        + "<script src=\"https://challenges.cloudflare.com/turnstile/v0/api.js\" async defer></script>"
        + "<noscript><style>.reveal { opacity: 1 !important; transform: none !important; }</style></noscript>"
        + "<script async src=\"https://www.googletagmanager.com/gtag/js?id=G-Y1EE43X9RN\"></script>"
        + page_ga_script()
}

// page_seo_block — emits title, description, canonical, OG, and Twitter Card
// for a given page. Pass the production canonical path (e.g. "/" or "/about").
fn page_seo_block(title: String, description: String, canonical_path: String, og_description: String) -> String {
    let base: String = "https://neurontechnologies.ai"
    let canonical: String = base + canonical_path
    let og_image: String = base + "/assets/brand/neuron-wordmark-on-light@2x.png"
    return el_title(title)
        + el_meta("description", description)
        + "<meta property=\"og:type\" content=\"website\">"
        + "<meta property=\"og:url\" content=\"" + canonical + "\">"
        + "<meta property=\"og:title\" content=\"" + title + "\">"
        + "<meta property=\"og:description\" content=\"" + og_description + "\">"
        + "<meta property=\"og:image\" content=\"" + og_image + "\">"
        + "<meta property=\"og:site_name\" content=\"Neuron\">"
        + "<meta name=\"twitter:card\" content=\"summary_large_image\">"
        + "<meta name=\"twitter:title\" content=\"" + title + "\">"
        + "<meta name=\"twitter:description\" content=\"" + og_description + "\">"
        + "<meta name=\"twitter:image\" content=\"" + og_image + "\">"
        + "<link rel=\"canonical\" href=\"" + canonical + "\">"
}

fn page_head() -> String {
    return page_head_base()
        + page_seo_block(
            "Neuron — The AI That Remembers You",
            "Every AI resets when you close the tab. Neuron doesn&#39;t. Runs on your machine. Remembers everything. Start free — no credit card required.",
            "/",
            "Every other AI forgets you. Neuron doesn&#39;t. Runs on your machine, builds a persistent memory over time, and gets sharper the longer you use it. Free tier available."
        )
        + page_schema()
}

fn page_open() -> String {
    return "<!DOCTYPE html><html lang=\"en\"><head>" + page_head() + "</head><body>"
}

// page_open_seo — page shell for inner pages with unique per-page SEO.
// title:         full <title> tag text
// description:   meta description (120–160 chars)
// canonical_path: path component, e.g. "/about" or "/checkout"
// og_description: OG/Twitter description (can differ from meta description)
// noindex:       pass "true" to add noindex for non-public pages (e.g. checkout)
fn page_open_seo(title: String, description: String, canonical_path: String, og_description: String, noindex: String) -> String {
    let robots_tag: String = if str_eq(noindex, "true") {
        "<meta name=\"robots\" content=\"noindex, nofollow\">"
    } else {
        ""
    }
    return "<!DOCTYPE html><html lang=\"en\"><head>"
        + page_head_base()
        + page_seo_block(title, description, canonical_path, og_description)
        + robots_tag
        + "</head><body>"
}
