# Service Command Handler
# Domain: Cross-platform service management (systemd, launchd, runit, openrc)

use ./shared.nu *

def service-install [name: string binary: string --check = false] { {name: $name} }
def service-start [name: string --check = false] { {name: $name} }
def service-stop [name: string --check = false] { {name: $name} }
def service-restart [name: string --check = false] { {name: $name} }
def service-status [name: string] { {name: $name, running: false} }
def service-list [--filter: string = ""] { [] }
def service-detect-init [] { "systemd" }

export def cmd-service [
    action: string
    args: list = []
    --check = false
] {
    if ($action == null) { help-service; return }

    match $action {
        "install" => {
            let name = ($args | get 0?)
            let binary = ($args | get 1?)
            if ($name == null or $binary == null) {
                print "Usage: provisioning service install <name> <binary> [options]"
                exit 1
            }
            let result = (service-install $name $binary --check=$check)
            print $"Service installed: [$result.name]"
        }
        "start" => {
            let name = ($args | get 0?)
            if ($name == null) {
                print "Usage: provisioning service start <name>"
                exit 1
            }
            let result = (service-start $name --check=$check)
            print $"Service started: [$result.name]"
        }
        "stop" => {
            let name = ($args | get 0?)
            if ($name == null) {
                print "Usage: provisioning service stop <name>"
                exit 1
            }
            let result = (service-stop $name --check=$check)
            print $"Service stopped: [$result.name]"
        }
        "restart" => {
            let name = ($args | get 0?)
            if ($name == null) {
                print "Usage: provisioning service restart <name>"
                exit 1
            }
            let result = (service-restart $name --check=$check)
            print $"Service restarted: [$result.name]"
        }
        "status" => {
            let name = ($args | get 0?)
            if ($name == null) {
                print "Usage: provisioning service status <name>"
                exit 1
            }
            let status = (service-status $name)
            print $"Service: [$status.name]"
            print $"  Running: [$status.running]"
        }
        "list" => {
            let filter = ($args | get 0?)
            let services = (service-list --filter=$filter)
            if ($services | length) == 0 {
                print "No services found"
            } else {
                print "Services:"
                $services | each {|s| print $"  • [$s.name] - Running: [$s.running]"}
            }
        }
        "detect-init" => {
            let init = (service-detect-init)
            print $"Detected init system: [$init]"
        }
        "help" | "--help" => { help-service }
        _ => { print $"Unknown service command: [$action]"; help-service; exit 1 }
    }
}

def help-service [] {
    print "Service management - Cross-platform service operations"
    print ""
    print "Usage: provisioning service <action> [args]"
    print ""
    print "Actions:"
    print "  install <name> <binary>  Install service"
    print "  start <name>             Start service"
    print "  stop <name>              Stop service"
    print "  restart <name>           Restart service"
    print "  status <name>            Check service status"
    print "  list [filter]            List services"
    print "  detect-init              Detect init system"
}
