# Platform detection utilities
#
# Provides functions to detect current OS, architecture, and map to cargo target triples

# Detect current operating system
export def detect-os [] {
    let sys = (sys host)
    match $sys.name {
        "Linux" => { "linux" }
        "Darwin" => { "macos" }
        "Windows" => { "windows" }
        _ => {
            let msg = "Unsupported OS: " + ($sys.name | into string)
            error make {msg: $msg}
        }
    }
}

# Detect current architecture
export def detect-arch [] {
    let arch_output = (^uname -m | str trim)
    match $arch_output {
        "x86_64" => { "x86_64" }
        "aarch64" => { "aarch64" }
        "arm64" => { "aarch64" }
        "i686" => { "i686" }
        "i386" => { "i386" }
        _ => {
            let msg = "Unsupported architecture: " + ($arch_output | into string)
            error make {msg: $msg}
        }
    }
}

# Get the appropriate libc variant for Linux
export def detect-libc [] {
    try {
        let ldd_output = (^ldd --version | head -1)
        if ($ldd_output | str contains "musl") {
            "musl"
        } else {
            "glibc"
        }
    } catch {
        "glibc"
    }
}

# Map OS, arch, libc to cargo target triple
export def map-to-target [--os: string = "", --arch: string = "", --libc: string = ""] {
    let os = if ($os == "") { detect-os } else { $os }
    let arch = if ($arch == "") { detect-arch } else { $arch }
    let libc = if (($libc == "") and ($os == "linux")) { detect-libc } else { $libc }

    match [$os, $arch, $libc] {
        ["linux", "x86_64", "glibc"] => { "x86_64-unknown-linux-gnu" }
        ["linux", "x86_64", "musl"] => { "x86_64-unknown-linux-musl" }
        ["linux", "aarch64", "glibc"] => { "aarch64-unknown-linux-gnu" }
        ["linux", "aarch64", "musl"] => { "aarch64-unknown-linux-musl" }
        ["macos", "x86_64", _] => { "x86_64-apple-darwin" }
        ["macos", "aarch64", _] => { "aarch64-apple-darwin" }
        ["windows", "x86_64", _] => { "x86_64-pc-windows-msvc" }
        _ => {
            let msg = "Unsupported combination: os=" + $os + ", arch=" + $arch + ", libc=" + $libc
            error make {msg: $msg}
        }
    }
}

# Get current platform as target triple
export def current-target [] {
    map-to-target
}

# Get all enabled targets from provisioning config
export def available-targets [config_file: path] {
    let config = (open --raw $config_file | from toml)
    # Return all target keys from config
    ["x86_64-unknown-linux-gnu" "aarch64-unknown-linux-gnu" "x86_64-apple-darwin" "aarch64-apple-darwin" "x86_64-pc-windows-msvc"]
}

# Get default installation prefix for a platform
export def default-prefix [target: string] {
    match $target {
        "x86_64-unknown-linux-gnu" => { "/usr/local" }
        "x86_64-unknown-linux-musl" => { "/usr/local" }
        "aarch64-unknown-linux-gnu" => { "/usr/local" }
        "aarch64-unknown-linux-musl" => { "/usr/local" }
        "x86_64-apple-darwin" => { "/usr/local" }
        "aarch64-apple-darwin" => { "/usr/local" }
        "x86_64-pc-windows-msvc" => { "C:\\Program Files\\syntaxis" }
        _ => { "/usr/local" }
    }
}

# Check if target is available in config
export def is-target-enabled [target: string, config_file: path] {
    let config = (open --raw $config_file | from toml)
    let target_config = ($config.targets | get -o $target)
    if (($target_config == null)) {
        false
    } else {
        $target_config.enabled
    }
}

# Get supported formats for a target
export def target-formats [target: string, config_file: path] {
    let config = (open --raw $config_file | from toml)
    let target_config = ($config.targets | get -o $target)
    if (($target_config == null)) {
        ["tar.gz"]
    } else {
        $target_config.formats
    }
}

# Get friendly target description
export def target-description [target: string, config_file: path] {
    let config = (open --raw $config_file | from toml)
    let target_config = ($config.targets | get -o $target)
    if (($target_config == null)) {
        $target
    } else {
        $target_config.description
    }
}

# List all supported targets with descriptions
export def list-targets [config_file: path] {
    let config = (open --raw $config_file | from toml)
    # Return hardcoded target list with descriptions
    [
        {
            target: "x86_64-unknown-linux-gnu"
            enabled: ($config.targets.x86_64-unknown-linux-gnu.enabled // true)
            description: "Linux x86_64 (glibc)"
            formats: "tar.gz"
        }
        {
            target: "aarch64-unknown-linux-gnu"
            enabled: ($config.targets.aarch64-unknown-linux-gnu.enabled // true)
            description: "Linux ARM64 (glibc)"
            formats: "tar.gz"
        }
        {
            target: "x86_64-apple-darwin"
            enabled: ($config.targets.x86_64-apple-darwin.enabled // true)
            description: "macOS x86_64"
            formats: "tar.gz,zip"
        }
        {
            target: "aarch64-apple-darwin"
            enabled: ($config.targets.aarch64-apple-darwin.enabled // true)
            description: "macOS ARM64"
            formats: "tar.gz,zip"
        }
        {
            target: "x86_64-pc-windows-msvc"
            enabled: ($config.targets.x86_64-pc-windows-msvc.enabled // true)
            description: "Windows x86_64"
            formats: "zip"
        }
    ]
}
