# Nushell Environment Configuration Template
# Default environment setup for Nushell + Plugins distribution
# Created by nushell-plugins full distribution system

# =============================================================================
# CORE ENVIRONMENT VARIABLES
# =============================================================================

# Default editor and browser
$env.EDITOR = (
    if $nu.os-info.name == "windows" {
        "notepad.exe"
    } else if (which code | is-not-empty) {
        "code"
    } else if (which nano | is-not-empty) {
        "nano"
    } else if (which vim | is-not-empty) {
        "vim"
    } else {
        "vi"
    }
)

$env.BROWSER = (
    if $nu.os-info.name == "windows" {
        "msedge.exe"
    } else if $nu.os-info.name == "macos" {
        "open"
    } else if (which firefox | is-not-empty) {
        "firefox"
    } else if (which chromium | is-not-empty) {
        "chromium"
    } else {
        "xdg-open"
    }
)

# Pager configuration
$env.PAGER = (
    if (which bat | is-not-empty) {
        "bat"
    } else if (which less | is-not-empty) {
        "less"
    } else {
        "more"
    }
)

# =============================================================================
# NUSHELL SPECIFIC CONFIGURATION
# =============================================================================

# Nushell library directories
$env.NU_LIB_DIRS = [
    ($nu.config-path | path dirname | path join "scripts")
    ($nu.config-path | path dirname | path join "lib")
    ($nu.config-path | path dirname | path join "modules")
]

# Plugin directories
$env.NU_PLUGIN_DIRS = [
    ($nu.config-path | path dirname | path join "plugins")
]

# Custom prompt indicators
$env.PROMPT_INDICATOR = "〉"
$env.PROMPT_INDICATOR_VI_INSERT = ": "
$env.PROMPT_INDICATOR_VI_NORMAL = "〉"
$env.PROMPT_MULTILINE_INDICATOR = "::: "

# =============================================================================
# DEVELOPMENT ENVIRONMENT
# =============================================================================

# Rust environment
if (which cargo | is-not-empty) {
    # Add cargo bin to PATH if not already there
    let cargo_bin = ($env.HOME | path join ".cargo" "bin")
    if ($cargo_bin | path exists) and ($env.PATH | split row (char esep) | where $it == $cargo_bin | is-empty) {
        $env.PATH = ($env.PATH | split row (char esep) | append $cargo_bin | str join (char esep))
    }
}

# Go environment
if (which go | is-not-empty) {
    $env.GOPATH = ($env.HOME | path join "go")
    let go_bin = ($env.GOPATH | path join "bin")
    if ($go_bin | path exists) and ($env.PATH | split row (char esep) | where $it == $go_bin | is-empty) {
        $env.PATH = ($env.PATH | split row (char esep) | append $go_bin | str join (char esep))
    }
}

# Node.js environment
if (which npm | is-not-empty) {
    # Add global npm bin to PATH
    try {
        let npm_bin = (npm config get prefix | str trim | path join "bin")
        if ($npm_bin | path exists) and ($env.PATH | split row (char esep) | where $it == $npm_bin | is-empty) {
            $env.PATH = ($env.PATH | split row (char esep) | append $npm_bin | str join (char esep))
        }
    }
}

# Python environment
if (which python3 | is-not-empty) or (which python | is-not-empty) {
    # Add user Python bin to PATH
    let python_cmd = if (which python3 | is-not-empty) { "python3" } else { "python" }
    try {
        let python_user_bin = (run-external $python_cmd "-m" "site" "--user-base" | str trim | path join "bin")
        if ($python_user_bin | path exists) and ($env.PATH | split row (char esep) | where $it == $python_user_bin | is-empty) {
            $env.PATH = ($env.PATH | split row (char esep) | append $python_user_bin | str join (char esep))
        }
    }
}

# =============================================================================
# SYSTEM SPECIFIC CONFIGURATION
# =============================================================================

# Linux specific
if $nu.os-info.name == "linux" {
    # Add local bin directories to PATH
    let local_bins = [
        ($env.HOME | path join ".local" "bin")
        "/usr/local/bin"
    ]

    for local_bin in $local_bins {
        if ($local_bin | path exists) and ($env.PATH | split row (char esep) | where $it == $local_bin | is-empty) {
            $env.PATH = ($env.PATH | split row (char esep) | append $local_bin | str join (char esep))
        }
    }

    # XDG directories
    $env.XDG_CONFIG_HOME = ($env.HOME | path join ".config")
    $env.XDG_DATA_HOME = ($env.HOME | path join ".local" "share")
    $env.XDG_CACHE_HOME = ($env.HOME | path join ".cache")
}

# macOS specific
if $nu.os-info.name == "macos" {
    # Add Homebrew to PATH if it exists
    let homebrew_paths = [
        "/opt/homebrew/bin"    # Apple Silicon
        "/usr/local/bin"       # Intel
    ]

    for homebrew_path in $homebrew_paths {
        if ($homebrew_path | path exists) and ($env.PATH | split row (char esep) | where $it == $homebrew_path | is-empty) {
            $env.PATH = ($env.PATH | split row (char esep) | prepend $homebrew_path | str join (char esep))
        }
    }

    # macOS specific environment variables
    if (which brew | is-not-empty) {
        $env.HOMEBREW_NO_ANALYTICS = "1"
        $env.HOMEBREW_NO_AUTO_UPDATE = "1"
    }
}

# Windows specific
if $nu.os-info.name == "windows" {
    # Windows specific environment (most variables should already be set by system)
    # Add user local bin if it exists
    let local_bin = ($env.USERPROFILE | path join ".local" "bin")
    if ($local_bin | path exists) and ($env.PATH | split row ";" | where $it == $local_bin | is-empty) {
        $env.PATH = ($env.PATH + ";" + $local_bin)
    }
}

# =============================================================================
# PERFORMANCE AND BEHAVIOR
# =============================================================================

# History configuration
$env.NU_HISTORY_SIZE = 10000

# Completions
$env.NU_COMPLETION_ALGORITHM = "prefix"  # or "fuzzy"

# Plugin timeout (in seconds)
$env.NU_PLUGIN_TIMEOUT = 60

# =============================================================================
# CUSTOM ALIASES AND SHORTCUTS
# =============================================================================

# Common directory shortcuts
$env.REPOS = ($env.HOME | path join "repos")
$env.PROJECTS = ($env.HOME | path join "projects")
$env.DOWNLOADS = (
    if $nu.os-info.name == "windows" {
        ($env.USERPROFILE | path join "Downloads")
    } else {
        ($env.HOME | path join "Downloads")
    }
)

# =============================================================================
# THEME AND APPEARANCE
# =============================================================================

# Terminal colors (if supported)
$env.COLORTERM = "truecolor"

# LS_COLORS for better directory listings
if $nu.os-info.name != "windows" {
    $env.LS_COLORS = "di=1;34:ln=1;36:so=1;35:pi=1;33:ex=1;32:bd=1;33:cd=1;33:su=1;31:sg=1;31:tw=1;34:ow=1;34"
}

# =============================================================================
# CONDITIONAL FEATURES
# =============================================================================

# Enable starship prompt if available
if (which starship | is-not-empty) {
    $env.STARSHIP_CONFIG = ($nu.config-path | path dirname | path join "starship.toml")
}

# Enable zoxide if available
if (which zoxide | is-not-empty) {
    $env._ZO_DATA_DIR = ($nu.config-path | path dirname | path join "zoxide")
}

# Enable direnv if available
if (which direnv | is-not-empty) {
    $env.DIRENV_LOG_FORMAT = ""  # Quiet direnv output
}

# =============================================================================
# PLUGIN SPECIFIC CONFIGURATION
# =============================================================================

# Clipboard plugin settings
$env.NU_CLIPBOARD_PROVIDER = (
    if $nu.os-info.name == "linux" and (which xclip | is-not-empty) {
        "xclip"
    } else if $nu.os-info.name == "linux" and (which wl-copy | is-not-empty) {
        "wl-clipboard"
    } else if $nu.os-info.name == "macos" {
        "pbcopy"
    } else if $nu.os-info.name == "windows" {
        "windows"
    } else {
        "none"
    }
)

# Image plugin settings
$env.NU_IMAGE_VIEWER = (
    if $nu.os-info.name == "macos" {
        "open"
    } else if $nu.os-info.name == "windows" {
        "mspaint"
    } else if (which feh | is-not-empty) {
        "feh"
    } else if (which eog | is-not-empty) {
        "eog"
    } else {
        "xdg-open"
    }
)

# =============================================================================
# USER CUSTOMIZATION SECTION
# =============================================================================

# Load user-specific environment file if it exists
let user_env_file = ($nu.config-path | path dirname | path join "user_env.nu")
if ($user_env_file | path exists) {
    source $user_env_file
}

# Load local environment file if it exists (for project-specific settings)
let local_env_file = (pwd | path join ".nu_env")
if ($local_env_file | path exists) {
    source $local_env_file
}

# =============================================================================
# FINAL PATH CLEANUP
# =============================================================================

# Remove duplicate PATH entries while preserving order
$env.PATH = (
    $env.PATH
    | split row (char esep)
    | uniq
    | where ($it | path exists)
    | str join (char esep)
)

# =============================================================================
# STARTUP INFORMATION (Optional)
# =============================================================================

# Uncomment to show environment info on startup
# print $"🚀 Nushell environment loaded"
# print $"📁 Config: ($nu.config-path)"
# print $"🧩 Plugins: (plugin list | length) loaded"
# print $"💻 Platform: ($nu.os-info.name) ($nu.os-info.arch)"

# =============================================================================
# NOTES FOR CUSTOMIZATION
# =============================================================================

# This file contains sensible defaults for a Nushell + Plugins environment.
# To customize:
#
# 1. Create user_env.nu in the same directory for user-specific settings
# 2. Create .nu_env in project directories for project-specific settings
# 3. Modify this file directly (but changes may be lost on updates)
#
# For more information:
# - Nushell Book: https://nushell.sh/book/
# - Environment Variables: https://nushell.sh/book/environment.html
# - Custom Commands: https://nushell.sh/book/custom_commands.html