# Evaluated by lian-build CLI with:
#   LIAN_BUILD_NICKEL_IMPORT_PATH=/path/to/lian-build
# "defaults/build_directives.ncl" resolves via LIAN_BUILD_ROOT import-path.
# "jpl-project.ncl" resolves via LIAN_BUILD_NICKEL_IMPORT_PATH (not relative) so
# the justfile can shadow it with a version-patched copy without touching source.
#
# This file exports ONE directive PER render profile, keyed by profile name.
# Nickel 1.16 has no env access, so profile selection happens in provisioning/
# build.nu (which runs check-wasm-needed.nu against site/config/rendering.ncl):
# build.nu extracts the matching field and feeds that single directive to
# lian-build. The two profiles are genuinely disjoint builds — different base
# images, different Dockerfile, different artifacts (pkg/ vs none) — not one
# build with a flag.
let d       = import "defaults/build_directives.ncl" in
let project = import "jpl-project.ncl" in

# Args common to both Dockerfiles. project.port is the canonical port.
let common_args = {
  RUST_VERSION    = "1.93",
  NICKEL_VERSION  = "1.16.0",
  LAMINA_REGISTRY = "registry.example.com/lamina",
  BIN_NAME        = "rustelo-leptos-server",
  SERVER_PORT     = std.string.from_number project.port,
} in

# leptos-hydration: cargo-leptos build over lamina/leptos + lamina/rustelo,
# emits the WASM pkg/ (version-matched to the binary). content-static bakes
# content + i18n at compile time.
let leptos_args = common_args & {
  RUSTELO_VERSION    = "0.1.0",
  LEPTOS_VERSION     = "0.3.6",
  NODE_VERSION       = "22",
  LEPTOS_OUTPUT_NAME = "website",
  BIN_FEATURES       = "content-static",
} in

# htmx-ssr: plain cargo build over lamina/rust (no wasm toolchain, no node/css
# stage, no pkg/). Content is served from the PV at runtime, not baked.
let htmx_args = common_args & {
  BIN_FEATURES = "htmx-ssr",
} in

# Fields identical across profiles: where to push, how to sign/cache/adapt.
# image: registry is the primary write surface (reachable from lian-01).
# registry.example.com (project.image_base) is the K8s pull address; it syncs
# from registry on-demand per ADR-019. These must not be the same ref.
let shared = {
  image_ref = "registry.example.com/%{project.domain}/%{project.name}:%{project.image_tag}",

  adapter = d.make_adapter_ref {
    kind              = 'fleet,
    nats_url          = "nats://10.0.8.5:30422",
    consumer_identity = "lian-build",
    subject_prefix    = "fleet.libre-forge",
    memory_hint_gb    = 7,
  },

  registry = d.default_zot_registry "registry.example.com" {
    path = project.build_secrets.registry_push,
    kind = 'docker_config,
  },

  cache = d.ci_cache_policy project.workspace_id,

  signing = {
    key_ref = { kind = 'cosign_private_key, path = project.build_secrets.cosign },
  },

  sccache = {
    bucket     = "wuji-sccache",
    endpoint   = "https://fsn1.your-objectstorage.com",
    region     = "eu-central-1",
    credential = { kind = 's3, path = project.build_secrets.sccache },
  },
} in

let make_directive = fun df bargs =>
  d.make_build_directives {
    workspace = project.workspace_id,
    artifacts = [
      d.make_artifact {
        image      = shared.image_ref,
        dockerfile = df,
        context    = "..",  # parent of lian-build/ = repo root
        platforms  = ["linux/arm64"],
        build_args = bargs,
      },
    ],
    adapter  = shared.adapter,
    registry = shared.registry,
    cache    = shared.cache,
    signing  = shared.signing,
    sccache  = shared.sccache,
  }
in

# Dockerfile suffix === profile name, so the path never drifts from the key.
{
  "leptos-hydration" = make_directive "lian-build/Dockerfile.leptos-hydration" leptos_args,
  "htmx-ssr"         = make_directive "lian-build/Dockerfile.htmx-ssr" htmx_args,
}
