# ╔══════════════════════════════════════════════════════════════════════╗
# ║                      NICKEL INTEGRATION                              ║
# ║              Schema validation and configuration export              ║
# ╚══════════════════════════════════════════════════════════════════════╝

# === PATHS ===
SCHEMA_DIR := "schemas"
CONFIG_DIR := "config"
TYPEDIALOG_DIR := ".typedialog/kogral"
TYPEDIALOG_CORE := ".typedialog/kogral/core"
TYPEDIALOG_MODES := ".typedialog/kogral/modes"
TYPEDIALOG_SCRIPTS := ".typedialog/kogral/scripts"

# Help for Nickel module
help:
    @echo "NICKEL MODULE"
    @echo ""
    @echo "Validation:"
    @echo "  just nickel::validate-all      Validate all schemas (legacy)"
    @echo "  just nickel::validate-config   Validate TypeDialog config schemas"
    @echo "  just nickel::validate FILE     Validate specific schema"
    @echo "  just nickel::typecheck FILE    Typecheck Nickel file"
    @echo ""
    @echo "Export:"
    @echo "  just nickel::export FILE       Export config to JSON"
    @echo "  just nickel::export-all        Export all example configs (legacy)"
    @echo "  just nickel::export-modes      Export all TypeDialog modes to JSON"
    @echo "  just nickel::export-dev        Export dev mode configuration"
    @echo "  just nickel::export-prod       Export prod mode configuration"
    @echo "  just nickel::export-test       Export test mode configuration"
    @echo ""
    @echo "Sync & Generate:"
    @echo "  just nickel::sync-to-schemas   Sync .typedialog/kogral → schemas/kogral"
    @echo "  just nickel::generate-config   Generate .kogral/config.json (default: dev mode)"
    @echo ""
    @echo "Linting:"
    @echo "  just nickel::lint              Lint Nickel schemas"
    @echo "  just nickel::fmt FILE          Format Nickel file"
    @echo ""
    @echo "Testing:"
    @echo "  just nickel::test              Test schema examples"

# === VALIDATION ===

# Validate all Nickel schemas
[doc("Validate all Nickel schemas")]
validate-all:
    @echo "=== Validating Nickel schemas ==="
    @for file in {{ SCHEMA_DIR }}/*.ncl; do \
        echo "Validating $$file..."; \
        nickel typecheck "$$file" || exit 1; \
    done
    @echo "✓ All schemas valid"

# Validate specific Nickel file
[doc("Validate specific Nickel file")]
validate FILE:
    @echo "=== Validating {{ FILE }} ==="
    nickel typecheck "{{ FILE }}"
    @echo "✓ {{ FILE }} is valid"

# Typecheck Nickel file
[doc("Typecheck Nickel file")]
typecheck FILE:
    @nickel typecheck "{{ FILE }}"

# === EXPORT ===

# Export config file to JSON
[doc("Export Nickel config to JSON")]
export FILE:
    @echo "=== Exporting {{ FILE }} to JSON ==="
    @nickel export --format json "{{ FILE }}"

# Export all example configs
[doc("Export all example configs to JSON")]
export-all:
    @echo "=== Exporting all configs ==="
    @for file in {{ CONFIG_DIR }}/*.ncl; do \
        output="$${file%.ncl}.json"; \
        echo "Exporting $$file → $$output"; \
        nickel export --format json "$$file" > "$$output"; \
    done
    @echo "✓ All configs exported"

# === LINTING ===

# Lint Nickel schemas (check formatting and style)
[doc("Lint Nickel schemas")]
lint:
    @echo "=== Linting Nickel files ==="
    @for file in {{ SCHEMA_DIR }}/*.ncl {{ CONFIG_DIR }}/*.ncl; do \
        echo "Checking $$file..."; \
        nickel typecheck "$$file" || exit 1; \
    done
    @echo "✓ Nickel lint passed"

# Format Nickel file
[doc("Format Nickel file")]
fmt FILE:
    @echo "=== Formatting {{ FILE }} ==="
    nickel format "{{ FILE }}"

# === TYPEDIALOG CONFIGURATION ===

# Validate TypeDialog config schemas
[doc("Validate TypeDialog config schemas (.typedialog/kogral/)")]
validate-config:
    @echo "=== Validating TypeDialog config schemas ==="
    nu {{justfile_directory()}}/.typedialog/kogral/scripts/validate-config.nu

# Export all TypeDialog modes to JSON
[doc("Export all TypeDialog modes to JSON")]
export-modes: export-dev export-prod export-test
    @echo "✓ All modes exported"

# Export dev mode configuration
[doc("Export dev mode configuration")]
export-dev:
    @mkdir -p .kogral && \
    nickel export --format json .typedialog/kogral/modes/dev.ncl > .kogral/config.dev.json && \
    echo "  ✓ Exported: .kogral/config.dev.json"

# Export prod mode configuration
[doc("Export prod mode configuration")]
export-prod:
    @mkdir -p .kogral && \
    nickel export --format json .typedialog/kogral/modes/prod.ncl > .kogral/config.prod.json && \
    echo "  ✓ Exported: .kogral/config.prod.json"

# Export test mode configuration
[doc("Export test mode configuration")]
export-test:
    @mkdir -p .kogral && \
    nickel export --format json .typedialog/kogral/modes/test.ncl > .kogral/config.test.json && \
    echo "  ✓ Exported: .kogral/config.test.json"

# Sync .typedialog/kogral to schemas/kogral (source of truth)
[doc("Sync .typedialog/kogral → schemas/kogral")]
sync-to-schemas:
    @echo "=== Syncing TypeDialog config to schemas ==="
    @mkdir -p schemas/kogral
    @echo "  Copying core schemas..."
    @cp {{ TYPEDIALOG_CORE }}/contracts.ncl schemas/kogral/
    @cp {{ TYPEDIALOG_CORE }}/defaults.ncl schemas/kogral/
    @cp {{ TYPEDIALOG_CORE }}/helpers.ncl schemas/kogral/
    @mkdir -p schemas/kogral/modes
    @echo "  Copying mode overlays..."
    @cp {{ TYPEDIALOG_MODES }}/dev.ncl schemas/kogral/modes/
    @cp {{ TYPEDIALOG_MODES }}/prod.ncl schemas/kogral/modes/
    @cp {{ TYPEDIALOG_MODES }}/test.ncl schemas/kogral/modes/
    @echo "✓ Sync completed"

# Generate .kogral/config.json for CLI/MCP (default: dev mode)
[doc("Generate .kogral/config.json (default: dev mode)")]
generate-config MODE="dev":
    @echo "=== Generating config from {{ MODE }} mode ==="
    @if [ "{{ MODE }}" != "dev" ] && [ "{{ MODE }}" != "prod" ] && [ "{{ MODE }}" != "test" ]; then \
        echo "Error: Invalid mode. Must be dev, prod, or test"; \
        exit 1; \
    fi
    @mkdir -p .kogral
    nu {{justfile_directory()}}/.typedialog/kogral/scripts/generate-configs.nu --mode {{ MODE }} --output .kogral
    @echo "✓ Configuration generated at .kogral/config.json"

# === TESTING ===

# Test schema examples
[doc("Test schema examples")]
test:
    @echo "=== Testing Nickel schemas ==="
    @just nickel::validate-all
    @just nickel::export-all
    @echo "✓ Schema tests passed"

# Test TypeDialog configuration pipeline
[doc("Test TypeDialog configuration pipeline")]
test-config:
    @echo "=== Testing TypeDialog config pipeline ==="
    @just nickel::validate-config
    @just nickel::export-modes
    @just nickel::sync-to-schemas
    @just nickel::generate-config dev
    @echo "✓ TypeDialog config pipeline tests passed"
