# Platform Target Configuration System

use ../utils/nickel_processor.nu [ncl-eval-soft]

# Get deployment configuration directory
def get-config-dir [] {
    if ($nu.os-info.name == "macos") {
        $"($env.HOME)/Library/Application Support/provisioning/platform"
    } else {
        $"($env.HOME)/.config/provisioning/platform"
    }
}

# Load deployment configuration
export def load-deployment-mode [] {
    let config_dir = (get-config-dir)
    let config_file = $"($config_dir)/deployment-mode.ncl"

    if not ($config_file | path exists) {
        print $"ERROR: Configuration file not found at ($config_file)"
        return {}
    }

    let import_path = ($env.PROVISIONING? | default "")
    let import_paths = if ($import_path | is-not-empty) { [$import_path] } else { [] }
    let content = (ncl-eval-soft $config_file $import_paths null)

    if $content != null {
        $content
    } else {
        print "ERROR: Failed to export Nickel configuration"
        {}
    }
}

# Get enabled services
export def get-enabled-services [] {
    let deployment = (load-deployment-mode)

    if not ("services" in $deployment) {
        print "ERROR: No services found in deployment configuration"
        return []
    }

    let services = $deployment.services

    let all_services = ($services | columns)

    # Filter only enabled services
    let enabled = (
        $all_services
        | where {|key|
            let svc = ($services | get $key)
            let is_enabled = ($svc.enabled? | default false)
            $is_enabled
        }
    )

    $enabled
    | each {|name|
        let cfg = $services | get $name
        let priority = ($cfg.priority? | default 999)
        {name: $name, config: $cfg, priority: $priority}
    }
    | sort-by priority
}

# Get single service config
export def get-deployment-service-config [service: string] {
    let deployment = (load-deployment-mode)
    $deployment.services | get $service
}

# Get default target
export def get-default-platform-target [workspace: string] {
    {
        platform: {
            name: $"($workspace)-local"
            type: "local"
            mode: "development"
            services: {
                orchestrator: {
                    enabled: true
                    endpoint: "http://localhost:9090"
                    required: true
                    health_check: {endpoint: "/health", timeout: 5000}
                }
                control: {
                    enabled: false
                    endpoint: "http://localhost:9080"
                    required: false
                    health_check: {endpoint: "/health", timeout: 5000}
                }
                kms: {
                    enabled: true
                    endpoint: "http://localhost:8090"
                    required: true
                    health_check: {endpoint: "/health", timeout: 5000}
                }
            }
        }
    }
}

# Validate target
export def validate-platform-target [target: record] {
    ("platform" in $target)
}

# Detect mode from endpoint
export def detect-platform-mode [endpoint: string] {
    if ($endpoint =~ "localhost") {
        "local"
    } else {
        "remote"
    }
}

# Check if service should start locally
export def should-start-locally [config: record] {
    let mode = (detect-platform-mode $config.endpoint)
    ($mode == "local")
}

# Get endpoint — builds URL from server.{host,port} if no explicit endpoint field.
export def get-platform-endpoint [service: string] {
    let cfg = (get-deployment-service-config $service)
    let explicit = ($cfg | get -o endpoint | default "")
    if ($explicit | is-not-empty) {
        $explicit
    } else {
        let srv = ($cfg | get -o server)
        if $srv == null {
            ""
        } else {
            let host = ($srv | get -o host | default "127.0.0.1")
            let port = ($srv | get -o port | default 0)
            if $port == 0 { "" } else { $"http://($host):($port)" }
        }
    }
}

# Check if enabled
export def is-platform-service-enabled [service: string] {
    let cfg = (get-deployment-service-config $service)
    $cfg.enabled
}

# Get config
export def get-platform-service-config [service: string] {
    get-deployment-service-config $service
}

# List enabled
export def list-enabled-platform-services [] {
    get-enabled-services | each {|s| {name: $s.name}}
}

# List required
export def list-required-platform-services [] {
    get-enabled-services
    | where {|s| ($s.config.required? | default false)}
    | each {|s| {name: $s.name}}
}
