# Frontend asset management
#
# Constellation layout: canonical assets live at ../assets/ (parent repo).
# code/assets/ is a materialized copy populated by `just sync-assets`.
# Vendored JS libs live in assets/vendor/ and are served by the daemon
# at /assets/vendor/<file>. Pin versions explicitly; bump manually.
#
# Pattern guide: reflection/templates/vendor-frontend-assets-prompt.md

CYTOSCAPE_NAVIGATOR_VERSION := "2.0.1"
HTMX_VERSION := "2.0.7"

# Sync assets from the constellation parent (../assets/) into code/assets/.
# Copies CSS build source, vendor JS, and logos needed by this repo.
# Skips files unchanged by content hash. Writes assets/.sync-manifest.json
# with source path and per-file digests (precursor to ore sync assets mode).
[doc("Sync assets from constellation parent ../assets/ into code/assets/")]
sync-assets:
    #!/usr/bin/env nu
    let src = (($env.PWD | path dirname) | path join "assets")
    let dst = ($env.PWD | path join "assets")

    if not ($src | path exists) {
        error make { msg: $"sync-assets: source not found at ($src)" }
    }

    # Returns true when dst is missing or its sha256 differs from src.
    def needs-copy [s: string, d: string]: nothing -> bool {
        if not ($d | path exists) { return true }
        (open --raw $s | hash sha256) != (open --raw $d | hash sha256)
    }

    def sync-file [s: string, d: string, label: string]: nothing -> record {
        if (needs-copy $s $d) {
            mkdir ($d | path dirname)
            cp $s $d
            { file: $label, action: "copied", sha256: (open --raw $d | hash sha256) }
        } else {
            { file: $label, action: "unchanged", sha256: (open --raw $d | hash sha256) }
        }
    }

    mut results = []

    # CSS build source — node_modules excluded (pnpm install regenerates it)
    for f in ["package.json", "uno.config.js", "tailwind.config.js"] {
        let s = ($src | path join "css" $f)
        if ($s | path exists) {
            $results = ($results | append (sync-file $s ($dst | path join "css" $f) $"css/($f)"))
        }
    }

    # Vendor JS
    let vendor_src = ($src | path join "vendor")
    if ($vendor_src | path exists) {
        mkdir ($dst | path join "vendor")
        for f in (ls $vendor_src | where type == file | get name) {
            let name = ($f | path basename)
            $results = ($results | append (sync-file $f ($dst | path join "vendor" $name) $"vendor/($name)"))
        }
    }

    # Logos referenced by README.md and docs
    for f in ["ontoref-logo.svg", "ontoref.svg", "ontoref_text.svg", "ontoref_v.svg"] {
        let s = ($src | path join $f)
        if ($s | path exists) {
            $results = ($results | append (sync-file $s ($dst | path join $f) $f))
        }
    }

    # Aggregate digest: sha256 of sorted "file:sha256\n" pairs (same algorithm as ore sync assets)
    let file_hashes = ($results | each { |r| $"($r.file):($r.sha256)" } | sort | str join "\n")
    let aggregate = $"sha256:($file_hashes | hash sha256)"
    let synced_at = (date now | format date "%Y-%m-%dT%H:%M:%SZ")

    {
        source: ($src | into string),
        aggregate_digest: $aggregate,
        synced_at: $synced_at,
        files: $results,
    } | to json | save --force ($dst | path join ".sync-manifest.json")

    let copied = ($results | where action == "copied" | length)
    let unchanged = ($results | where action == "unchanged" | length)
    print $"✓ sync-assets  copied=($copied) unchanged=($unchanged)  ($dst)"

# Export this daemon's API catalog to api-catalog.json.
# Run after any #[onto_api] annotation is added or changed.
# The file is read by the ontoref UI when this project is registered as a
# non-primary slug — consumer projects that run as separate binaries use this
# to expose their API surface in the ontoref UI.
[doc("Export #[onto_api] routes to artifacts/api-catalog-ontoref-daemon.ncl")]
export-api-catalog:
    mkdir -p artifacts
    cargo run -p ontoref-daemon --no-default-features -- --dump-api-catalog > artifacts/api-catalog-ontoref-daemon.ncl
    @echo "exported routes to artifacts/api-catalog-ontoref-daemon.ncl"

# Download/update all vendored frontend JS dependencies
[doc("Vendor all frontend JS dependencies")]
vendor-js: vendor-cytoscape-navigator vendor-htmx

# cytoscape-navigator — minimap extension for Cytoscape.js
[doc("Vendor cytoscape-navigator (minimap)")]
vendor-cytoscape-navigator:
    mkdir -p assets/vendor
    curl -fsSL \
        "https://cdn.jsdelivr.net/npm/cytoscape-navigator@{{CYTOSCAPE_NAVIGATOR_VERSION}}/cytoscape-navigator.js" \
        -o assets/vendor/cytoscape-navigator.js
    @echo "vendored cytoscape-navigator@{{CYTOSCAPE_NAVIGATOR_VERSION}}"

# htmx — HTML-first hypermedia library (goes to public/vendor — served via /public/)
[doc("Vendor htmx")]
vendor-htmx:
    mkdir -p crates/ontoref-daemon/public/vendor
    curl -fsSL \
        "https://cdn.jsdelivr.net/npm/htmx.org@{{HTMX_VERSION}}/dist/htmx.min.js" \
        -o crates/ontoref-daemon/public/vendor/htmx.min.js
    @echo "vendored htmx@{{HTMX_VERSION}}"
