# SDT — Project Journal Justfile
# Install just: https://just.systems/man/en/packages.html

set windows-shell := ["pwsh", "-NoProfile", "-ExecutionPolicy", "Bypass", "-Command"]
set shell := ["pwsh", "-c"]

# Detect runtime from OS
runtime := if os() == "windows" { "win-x64" } else { "linux-x64" }

# ── Default: list available recipes ────────────────────────────────────────────
default:
    @just --list

# ── Build ──────────────────────────────────────────────────────────────────────

# Build Journal.Sidecar as self-contained single-file exe
sidecar:
    & ./scripts/publish-sidecar.ps1 -Configuration Release -Runtime {{runtime}}

# Build SvelteKit web bundle (output: Journal.App/build/)
web:
    & ./scripts/publish-app.ps1 -Target web

# Publish WebGateway with embedded web UI (depends: web)
webgateway: web
    & ./scripts/publish-webgateway.ps1 -Configuration Release -Runtime {{runtime}}

# Build Tauri desktop exe — no installer (depends: sidecar)
tauri: sidecar
    & ./scripts/publish-app.ps1 -Target tauri -TauriBundles none

# Build Tauri with NSIS installer (depends: sidecar)
tauri-nsis: sidecar
    & ./scripts/publish-app.ps1 -Target tauri -TauriBundles nsis

# Build Tauri with MSI installer (depends: sidecar)
tauri-msi: sidecar
    & ./scripts/publish-app.ps1 -Target tauri -TauriBundles msi

# Full release build — everything in correct order
all: sidecar web webgateway tauri

# ── Dev ────────────────────────────────────────────────────────────────────────

# Run WebGateway dev server (http://localhost:5180)
run:
    & ./scripts/run-webgateway.ps1

# Run WebGateway with pinned project root (avoids multi-clone ambiguity)
run-pinned:
    & ./scripts/run-webgateway.ps1 -ProjectRoot {{justfile_directory()}} -Urls http://0.0.0.0:5180

# SvelteKit dev server only (http://localhost:1420)
dev-app:
    Set-Location Journal.App; npm run dev

# Tauri dev mode (desktop window + hot reload)
dev-tauri: sidecar
    Set-Location Journal.App; npm run tauri dev

# ── Test ───────────────────────────────────────────────────────────────────────

# Run all smoke tests (~80 integration tests)
test:
    dotnet run --project Journal.SmokeTests/Journal.SmokeTests.csproj

# Full migration gate (build + smoke + parity)
gate:
    & ./scripts/migration-gate.ps1

# Migration gate — skip smoke tests
gate-fast:
    & ./scripts/migration-gate.ps1 -SkipSmoke

# Migration gate — skip API contract tests
gate-no-api:
    & ./scripts/migration-gate.ps1 -SkipApi

# ── .NET ───────────────────────────────────────────────────────────────────────

# dotnet build all projects
build:
    dotnet build

# dotnet build with resilient NuGet defaults (use in restricted environments)
build-safe:
    & ./scripts/dotnet-min.ps1 build Journal.Core/Journal.Core.csproj
    & ./scripts/dotnet-min.ps1 build Journal.Sidecar/Journal.Sidecar.csproj
    & ./scripts/dotnet-min.ps1 build Journal.WebGateway/Journal.WebGateway.csproj

# ── NuGet Cache ────────────────────────────────────────────────────────────────

# Export NuGet cache to zip for offline/transfer use
nuget-export zip="nuget-cache-export.zip":
    & ./scripts/nuget-export-cache.ps1 -OutputZip {{zip}}

# Import NuGet cache zip and validate restore
nuget-import zip="nuget-cache-export.zip":
    & ./scripts/nuget-import-cache.ps1 -InputZip {{zip}}

# ── SDT ────────────────────────────────────────────────────────────────────────

# Launch SDT dev tool TUI
sdt:
    dotnet run --project Journal.DevTool/Journal.DevTool.csproj
