#!/usr/bin/env nu
# Materialise the htmx-ssr Minijinja templates into a single deploy directory so
# the running server reads ONE path and never touches crate source trees
# (crates/pages_htmx/templates) or the sibling rustelo checkout. This mirrors how
# cargo-leptos stages target/site/pkg for the leptos-hydration profile.
#
# The output is the framework defaults (rustelo_pages_htmx/templates) overlaid
# with this project's overrides (crates/pages_htmx/templates): project files win,
# framework-only partials (content_header, content/*, nav/*, overlays/*,
# not_found) remain. The project→framework cascade is therefore resolved here, at
# build time — the runtime loader needs no fallback logic.
#
# Symlinks are dereferenced (-L): the project templates may live behind links and
# the tarball must be self-contained.
#
# Flow (symmetric with Leptos pkg):
#   assemble → target/site/htmx-templates  →  distro image_only[htmx-ssr]
#   → site/htmx-templates  →  /var/www/site/htmx-templates  (HTMX_TEMPLATE_PATH)
#
# Usage:
#   nu scripts/build/assemble-htmx-templates.nu
#   nu scripts/build/assemble-htmx-templates.nu --out target/site/htmx-templates

def main [
    --project-root: string = ""        # repo root; auto-derived from script location if empty
    --out: string = "target/site/htmx-templates"  # output dir (relative paths resolved under project-root)
    --project-templates: string = "crates/pages_htmx/templates"
    --framework-templates: string = "" # rustelo htmx templates; auto-derived from DEV_ROOT if empty
]: nothing -> nothing {
    let root = if ($project_root | is-not-empty) {
        $project_root
    } else {
        $env.FILE_PWD | path dirname | path dirname  # scripts/build → root
    }

    let dev_root = ($env.DEV_ROOT? | default "/Users/Akasha/Development")
    let framework = if ($framework_templates | is-not-empty) {
        $framework_templates
    } else {
        $"($dev_root)/rustelo/code/crates/foundation/crates/rustelo_pages_htmx/templates"
    }
    let project = if ($project_templates | str starts-with "/") {
        $project_templates
    } else {
        $root | path join $project_templates
    }
    let out_dir = if ($out | str starts-with "/") { $out } else { $root | path join $out }

    if not ($framework | path exists) {
        error make { msg: $"framework templates not found: ($framework) — set --framework-templates or DEV_ROOT" }
    }
    if not ($project | path exists) {
        error make { msg: $"project templates not found: ($project)" }
    }

    # Idempotent: rebuild from scratch so deleted templates never linger.
    if ($out_dir | path exists) { ^rm -rf $out_dir }
    mkdir $out_dir

    # Framework defaults first (base layer), then project overrides on top.
    # `src/.` copies directory CONTENTS; -L dereferences symlinks; BSD/GNU cp both
    # merge directories and overwrite colliding files, giving the overlay semantics.
    ^cp -RL $"($framework)/." $out_dir
    ^cp -RL $"($project)/." $out_dir

    let count = (^find -L $out_dir -type f | lines | where { |l| $l | is-not-empty } | length)
    print $"(ansi green)✅(ansi reset) htmx templates → ($out_dir) — ($count) files"
}
