#! vim: set filetype=nu:
# provctl service management wrapper module
#
# Provides unified interface for managing provisioning platform services:
# - provisioning-orchestrator
# - provisioning-control-center
# - provisioning-kms-service
# - Other platform services

# ============================================================================
# Service Management Operations
# ============================================================================

# Start a platform service
export def provctl-service-start [service_name: string] {
    if ($service_name | is-empty) {
        error make { msg: "Service name required" }
    }

    log info $"Starting platform service: [$service_name]"

    let result = (do { ^provctl start $service_name } | complete)

    if ($result.exit_code == 0) {
        log info $"Successfully started: [$service_name]"
        ($result.stdout | str trim)
    } else {
        log error $"Failed to start [$service_name]: ($result.stderr)"
        error make { msg: $"Service start failed: ($result.stderr)" }
    }
}

# Stop a platform service
export def provctl-service-stop [service_name: string] {
    if ($service_name | is-empty) {
        error make { msg: "Service name required" }
    }

    log info $"Stopping platform service: [$service_name]"

    let result = (do { ^provctl stop $service_name } | complete)

    if ($result.exit_code == 0) {
        log info $"Successfully stopped: [$service_name]"
        ($result.stdout | str trim)
    } else {
        log error $"Failed to stop [$service_name]: ($result.stderr)"
        error make { msg: $"Service stop failed: ($result.stderr)" }
    }
}

# Restart a platform service
export def provctl-service-restart [service_name: string] {
    if ($service_name | is-empty) {
        error make { msg: "Service name required" }
    }

    log info $"Restarting platform service: [$service_name]"

    let result = (do { ^provctl restart $service_name } | complete)

    if ($result.exit_code == 0) {
        log info $"Successfully restarted: [$service_name]"
        ($result.stdout | str trim)
    } else {
        log error $"Failed to restart [$service_name]: ($result.stderr)"
        error make { msg: $"Service restart failed: ($result.stderr)" }
    }
}

# Get status of a platform service
export def provctl-service-status [service_name: string] {
    if ($service_name | is-empty) {
        error make { msg: "Service name required" }
    }

    log debug $"Getting status for service: [$service_name]"

    let result = (do { ^provctl status $service_name } | complete)

    if ($result.exit_code == 0) {
        ($result.stdout | str trim)
    } else {
        log error $"Failed to get status for [$service_name]: ($result.stderr)"
        error make { msg: $"Service status check failed: ($result.stderr)" }
    }
}

# Get logs from a platform service
# Arguments: service_name, optional lines (default 50)
export def provctl-service-logs [
    service_name: string
    lines?: int
] {
    if ($service_name | is-empty) {
        error make { msg: "Service name required" }
    }

    let line_count = ($lines // 50)
    log debug $"Getting logs for service: [$service_name] (retrieving ($line_count) lines)"

    let result = (
        do { ^provctl logs $service_name --lines ($line_count | into string) } | complete
    )

    if ($result.exit_code == 0) {
        ($result.stdout | str trim)
    } else {
        log error $"Failed to get logs for [$service_name]: ($result.stderr)"
        error make { msg: $"Service logs retrieval failed: ($result.stderr)" }
    }
}

# ============================================================================
# Batch Operations
# ============================================================================

# Start multiple platform services
export def provctl-services-start [services: list] {
    print "Starting batch of services..."
    print $services
}

# Stop multiple platform services
export def provctl-services-stop [services: list] {
    print "Stopping batch of services..."
    print $services
}

# Restart multiple platform services
export def provctl-services-restart [services: list] {
    print "Restarting batch of services..."
    print $services
}

# ============================================================================
# Utility Functions
# ============================================================================

# Get all platform services status
export def provctl-all-services-status [] {
    [
        { service: "provisioning-orchestrator" }
        { service: "provisioning-control-center" }
        { service: "provisioning-kms-service" }
    ] | each { |item|
        log info $"Checking status of [$($item.service)]"
        $item
    }
}

# Show provctl wrapper help
export def provctl-help [] {
    print "
╔══════════════════════════════════════════════════════════════╗
║         Provisioning Platform Service Management             ║
║                   (provctl wrapper)                          ║
╚══════════════════════════════════════════════════════════════╝

SINGLE SERVICE OPERATIONS:
  provctl-service-start <name>      - Start a service
  provctl-service-stop <name>       - Stop a service
  provctl-service-restart <name>    - Restart a service
  provctl-service-status <name>     - Get service status
  provctl-service-logs <name> [n]   - Get service logs (default: 50 lines)

BATCH OPERATIONS:
  provctl-services-start <list>     - Start multiple services
  provctl-services-stop <list>      - Stop multiple services
  provctl-services-restart <list>   - Restart multiple services

UTILITY:
  provctl-all-services-status       - Show status of all platform services
  provctl-help                      - Show this help message

EXAMPLE USAGE:
  # Start single service
  provctl-service-start provisioning-orchestrator

  # Batch start all platform services
  let services = [
      \"provisioning-orchestrator\"
      \"provisioning-control-center\"
      \"provisioning-kms-service\"
  ]
  provctl-services-start \$services

  # Get logs
  provctl-service-logs provisioning-orchestrator 100
"
}
