# nimpak/cli/help.nim
# Help system for NimPak CLI commands

import std/[strutils, strformat]

proc showMainHelp*() =
  ## Show main nip command help
  echo """
🌱 NimPak (nip) - Universal Package Manager for NexusOS

USAGE:
    nip <command> [options] [arguments]

CORE COMMANDS:
    install <package>           Install a package
    remove <package>            Remove a package
    update                      Update package lists
    upgrade                     Upgrade all packages
    search <query>              Search for packages
    info <package>              Show package information
    list                        List installed packages

GRAFTING COMMANDS (🚀 NEW!):
    graft <source> <package>    Import package from external ecosystem
    convert <package>           Convert grafted package to .npk format
    grafting status             Show grafting engine status

ENVIRONMENT COMMANDS:
    cell create <name>          Create new user environment
    cell activate <name>        Activate user environment
    cell list                   List available environments

REPRODUCIBILITY COMMANDS:
    lock                        Generate lockfile for current state
    restore <lockfile>          Restore from lockfile
    diff                        Show environment drift

VERIFICATION COMMANDS:
    verify <package>            Verify package integrity
    track <package>             Show package provenance

CONFIGURATION COMMANDS:
    config show                 Show current configuration
    config validate             Validate configuration files

GLOBAL OPTIONS:
    --output <format>           Output format: human, json, yaml, kdl
    --log-level <level>         Log level: debug, info, warn, error
    --dry-run                   Simulate without making changes
    --no-color                  Disable colored output

GRAFTING SOURCES:
    pacman, arch               Arch Linux packages (official repos + AUR)
    nix, nixpkgs              Nix packages (80,000+ packages!)
    pkgsrc, netbsd            NetBSD packages (25,000+ packages!)

EXAMPLES:
    nip install htop                    # Install from NexusOS repos
    nip graft pacman neofetch          # Import from Arch Linux
    nip graft nix firefox              # Import from Nix (🔥 GAME CHANGER!)
    nip convert neofetch               # Convert to .npk format
    nip cell create dev-env            # Create development environment
    nip lock                           # Create reproducible lockfile

For detailed help on a specific command:
    nip help <command>

🚀 NimPak: Universal compatibility, cryptographic integrity, superior configuration!
"""

proc showGraftHelp*() =
  ## Show detailed help for grafting commands
  echo """
🌱 NimPak Grafting System - Universal Package Compatibility

The grafting system allows you to import packages from external ecosystems
and convert them to NexusOS .npk format for seamless integration.

GRAFTING COMMANDS:

  nip graft <source> <package> [options]
    Import a package from an external package manager

    Sources:
      pacman, arch    - Arch Linux official repositories and AUR
      nix, nixpkgs    - Nix packages (nixos-unstable channel)
      pkgsrc, netbsd  - NetBSD packages (binary + source builds)

    Options:
      --convert       Automatically convert to .npk after grafting
      --verify        Verify package integrity after grafting
      --output <fmt>  Output format: human, json, yaml, kdl

    Examples:
      nip graft pacman htop              # Import htop from Arch
      nip graft nix firefox --convert    # Import Firefox from Nix and convert
      nip graft arch yay --verify        # Import AUR helper with verification

  nip grafting status [options]
    Show current grafting engine status and cached packages

    Options:
      --verbose, -v   Show detailed information
      --output <fmt>  Output format: human, json, yaml, kdl

    Example:
      nip grafting status --verbose

  nip convert <package> [options]
    Convert a grafted package to native .npk format

    Options:
      --verify        Verify converted package
      --output <path> Save .npk to specific path

    Examples:
      nip convert firefox                 # Convert to .npk
      nip convert htop --output ~/htop.npk

GRAFTING WORKFLOW:

  1. 🌱 GRAFT: Import package from external ecosystem
     → Package is downloaded and cached with full provenance
     → Metadata is extracted and stored
     → Build logs and source information preserved

  2. 🔄 CONVERT: Transform to native .npk format
     → Files are reorganized to GoboLinux structure
     → Build hash is calculated for reproducibility
     → ACUL compliance metadata is added

  3. ✅ VERIFY: Ensure integrity and compliance
     → Cryptographic verification of all files
     → License compliance checking
     → Provenance chain validation

SUPPORTED ECOSYSTEMS:

  📦 Arch Linux (pacman/AUR)
    • 13,000+ official packages
    • 85,000+ AUR packages
    • Automatic PKGBUILD parsing
    • Signature verification

  📦 Nix (nixpkgs) 🔥 GAME CHANGER!
    • 80,000+ packages
    • Reproducible builds
    • Binary cache support
    • Declarative package definitions

  📦 PKGSRC (NetBSD) ✅ IMPLEMENTED!
    • 25,000+ NetBSD packages
    • Binary packages + source builds
    • Cross-platform compatibility
    • Mature, stable package collection

CACHE MANAGEMENT:

  Grafted packages are cached in ~/.nip/graft-cache/ with:
  • Original package archives
  • Extracted file structures
  • Metadata and provenance information
  • Build logs and conversion history

  Use 'nip grafting status' to see cache usage and cleanup options.

🚀 The grafting system gives NexusOS access to 205,000+ packages
   from mature ecosystems while maintaining our superior architecture!
"""

proc showCellHelp*() =
  ## Show detailed help for NexusCell commands
  echo """
🏠 NipCells - Isolated User Environments

NipCells provide per-user isolated package environments, allowing users
to install and manage packages without affecting the system or other users.

CELL COMMANDS:

  nip cell create <name> [options]
    Create a new user environment

    Options:
      --isolation <level>  Isolation level: none, standard, strict
      --description <text> Description for the cell

    Examples:
      nip cell create dev-env
      nip cell create gaming --isolation strict

  nip cell activate <name>
    Activate a user environment (updates PATH, etc.)

    Example:
      nip cell activate dev-env

  nip cell list [options]
    List available user environments

    Options:
      --verbose, -v   Show detailed information
      --output <fmt>  Output format: human, json, yaml, kdl

  nip cell delete <name>
    Delete a user environment and all its packages

    Example:
      nip cell delete old-env

CELL-SPECIFIC INSTALLATION:

  nip install --cell=<name> <package>
    Install package to specific cell

    Examples:
      nip install --cell=dev-env nodejs
      nip graft --cell=gaming nix steam

ISOLATION LEVELS:

  none      - No isolation, shares system environment
  standard  - Isolated package installation, shared system access
  strict    - Full isolation with restricted system access

CELL STRUCTURE:

  ~/.nexus/cells/<name>/
  ├── Programs/           # Cell-specific packages
  ├── Index/             # Cell-specific symlinks
  ├── config/            # Cell configuration
  └── metadata/          # Cell metadata and state

🏠 NipCells enable safe experimentation and user autonomy!
"""

proc showCommandHelp*(command: string) =
  ## Show help for a specific command
  case command.toLower():
  of "graft", "grafting":
    showGraftHelp()
  of "cell", "cells":
    showCellHelp()
  of "install":
    echo "nip install <package> [options] - Install a package"
  of "remove":
    echo "nip remove <package> [options] - Remove a package"
  of "convert":
    echo "nip convert <package> [options] - Convert grafted package to .npk"
  of "verify":
    echo "nip verify <package> [options] - Verify package integrity"
  of "lock":
    echo "nip lock [options] - Generate lockfile for reproducibility"
  of "restore":
    echo "nip restore <lockfile> [options] - Restore from lockfile"
  else:
    echo fmt"Unknown command: {command}"
    echo "Use 'nip help' for available commands"