#!/usr/bin/env nu
# Rustelo website-impl admin CLI.
#
# Bootstraps from .env (or process env) then dispatches NATS admin commands.
# Requires: NATS server reachable + rustelo-server running with nats-admin enabled.
#
# Usage:
#   nu admin/admin.nu <command> [flags]
#
# Examples:
#   nu admin/admin.nu health
#   nu admin/admin.nu health check
#   nu admin/admin.nu logs --lines 50
#   nu admin/admin.nu logs tail
#   nu admin/admin.nu logs errors
#   nu admin/admin.nu users list
#   nu admin/admin.nu users get admin@example.com
#   nu admin/admin.nu users disable spammer@example.com
#   nu admin/admin.nu users reset-sessions user@example.com
#   nu admin/admin.nu deploy --version v1.2.0 --sha abc1234
#   nu admin/admin.nu reload --reason "updated rbac.ncl"
#   nu admin/admin.nu env   # show resolved config

use ./mod.nu *

# Bootstrap env once at script load.
admin bootstrap

# ── Health ──────────────────────────────────────────────────────────────────

# Show server health as a structured record.
def "main health" [
    --url:   string = ""
    --creds: string = ""
] {
    nats-admin health --url $url --creds $creds
}

# Assert server health; exits non-zero on failure.
def "main health check" [
    --url:   string = ""
    --creds: string = ""
] {
    nats-admin health check --url $url --creds $creds
    print "OK"
}

# Print formatted health table.
def "main health show" [
    --url:   string = ""
    --creds: string = ""
] {
    nats-admin health show --url $url --creds $creds
}

# ── Logs ────────────────────────────────────────────────────────────────────

# Fetch recent log lines (default: 100).
def "main logs" [
    --lines: int    = 100
    --url:   string = ""
    --creds: string = ""
] {
    nats-admin logs --lines $lines --url $url --creds $creds
}

# Fetch logs as a parsed table (level / target / message columns).
def "main logs table" [
    --lines: int    = 100
    --url:   string = ""
    --creds: string = ""
] {
    nats-admin logs table --lines $lines --url $url --creds $creds
}

# Show only ERROR and WARN lines.
def "main logs errors" [
    --lines: int    = 500
    --url:   string = ""
    --creds: string = ""
] {
    nats-admin logs errors --lines $lines --url $url --creds $creds
}

# Poll logs continuously (Ctrl-C to stop).
def "main logs tail" [
    --interval: int    = 5
    --lines:    int    = 50
    --url:      string = ""
    --creds:    string = ""
] {
    nats-admin logs tail --interval $interval --lines $lines --url $url --creds $creds
}

# ── Users ───────────────────────────────────────────────────────────────────

# List all users.
def "main users list" [
    --url:   string = ""
    --creds: string = ""
] {
    nats-admin users list --url $url --creds $creds
}

# Get a user by email.
def "main users get" [
    email: string
    --url:   string = ""
    --creds: string = ""
] {
    nats-admin users get $email --url $url --creds $creds
}

# Disable a user account.
def "main users disable" [
    email: string
    --url:   string = ""
    --creds: string = ""
] {
    nats-admin users disable $email --url $url --creds $creds
}

# Reset all active sessions for a user.
def "main users reset-sessions" [
    email: string
    --url:   string = ""
    --creds: string = ""
] {
    nats-admin users reset-sessions $email --url $url --creds $creds
}

# ── Website ops ─────────────────────────────────────────────────────────────

# Publish a deploy signal to the ops bus.
def "main deploy" [
    --version: string = "unknown"
    --sha:     string = ""
    --url:     string = ""
    --creds:   string = ""
] {
    website deploy --version $version --sha $sha --url $url --creds $creds
}

# Trigger a hot config-reload on the running server.
def "main reload" [
    --reason: string = ""
    --url:    string = ""
    --creds:  string = ""
] {
    website reload --reason $reason --url $url --creds $creds
}

# ── Utilities ────────────────────────────────────────────────────────────────

# Show resolved NATS connection config (useful for debugging env).
def "main env" [] {
    {
        NATS_ADMIN_URL:              ($env.NATS_ADMIN_URL? | default "<unset>")
        NATS_ADMIN_CREDS:            ($env.NATS_ADMIN_CREDS? | default "<unset>")
        NATS_ADMIN_NAMESPACE_PREFIX: ($env.NATS_ADMIN_NAMESPACE_PREFIX? | default "<unset>")
        NATS_ADMIN_NAMESPACE_ENV:    ($env.NATS_ADMIN_NAMESPACE_ENV? | default "<unset>")
        subject_health:              $"($env.NATS_ADMIN_NAMESPACE_PREFIX? | default 'rustelo').($env.NATS_ADMIN_NAMESPACE_ENV? | default 'local').admin.health"
        subject_logs:                $"($env.NATS_ADMIN_NAMESPACE_PREFIX? | default 'rustelo').($env.NATS_ADMIN_NAMESPACE_ENV? | default 'local').admin.logs"
    } | transpose key value
}

# ── Default (no subcommand) ──────────────────────────────────────────────────

def main [] {
    print "Rustelo website-impl admin CLI"
    print ""
    print "Usage: nu admin/admin.nu <command> [flags]"
    print ""
    print "Commands:"
    print "  health              Server health record"
    print "  health check        Assert health (exit 1 on failure)"
    print "  health show         Formatted health table"
    print "  logs                Fetch recent log lines"
    print "  logs table          Parsed log table (level/target/message)"
    print "  logs errors         Filter ERROR and WARN lines only"
    print "  logs tail           Continuous poll (Ctrl-C to stop)"
    print "  users list          All registered users"
    print "  users get <email>   Single user record"
    print "  users disable <email>"
    print "  users reset-sessions <email>"
    print "  deploy              Publish a deploy signal to ops bus"
    print "  reload              Trigger hot config-reload on server"
    print "  env                 Show resolved NATS config"
    print ""
    print "Flags common to all commands:"
    print "  --url <url>         Override NATS_ADMIN_URL"
    print "  --creds <path>      Override NATS_ADMIN_CREDS"
}
