## nimpak/nippel_types.nim
## Core type definitions for Nippels
##
## This module contains all the core type definitions used by Nippels
## to avoid circular import dependencies between nippels.nim and other modules.

import std/[times, json, options]

# =============================================================================
# Core Type Definitions
# =============================================================================

type
  # Security Profiles (Requirement 6.1-6.6)
  SecurityProfile* = enum
    ## Security profiles for different system roles
    Workstation    ## Standard isolation + desktop integration
    Homestation    ## Standard isolation + relaxed network (default)
    Satellite      ## Strict isolation + limited network (remote/mobile)
    NetworkIOT     ## Strict isolation + minimal resources (embedded)
    Server         ## Strict isolation + no desktop + enhanced auditing

  # Isolation Levels (Requirement 5.1-5.5)
  IsolationLevel* = enum
    ## Isolation levels for Nippels
    None      ## No isolation - full system access
    Standard  ## Mount + filesystem namespaces
    Strict    ## Mount + PID + network + IPC namespaces
    Quantum   ## All namespaces + cryptographic boundaries

  # Network Access Levels
  NetworkAccessLevel* = enum
    NoNetwork      ## No network access
    Limited        ## Limited network access (specific hosts/ports)
    Relaxed        ## Relaxed network access (most protocols)
    Full           ## Full network access

  # Resource Limits
  ResourceLimits* = object
    maxMemory*: int64              ## Maximum memory usage (bytes)
    maxCpu*: float                 ## Maximum CPU usage (0.0-1.0)
    maxDisk*: int64               ## Maximum disk usage (bytes)
    maxProcesses*: int            ## Maximum number of processes
    maxOpenFiles*: int            ## Maximum open file descriptors

  # Profile Settings (Requirement 6.7-6.8)
  ProfileSettings* = object
    isolationLevel*: IsolationLevel
    desktopIntegration*: bool
    networkAccess*: NetworkAccessLevel
    resourceLimits*: ResourceLimits
    auditingEnabled*: bool

  # XDG Directories (Requirement 2.1-2.5)
  XDGDirectories* = object
    dataHome*: string      ## XDG_DATA_HOME
    configHome*: string    ## XDG_CONFIG_HOME
    cacheHome*: string     ## XDG_CACHE_HOME
    stateHome*: string     ## XDG_STATE_HOME
    runtimeDir*: string    ## XDG_RUNTIME_DIR

  # Namespace Configuration
  NamespaceHandle* = object
    mountNS*: bool
    pidNS*: bool
    networkNS*: bool
    ipcNS*: bool
    userNS*: bool
    utsNS*: bool
    nsPath*: string  ## Path to namespace file

  # CAS Entry
  CASEntry* = object
    hash*: string
    size*: int64
    refCount*: int
    path*: string

  # Package Reference
  PackageRef* = object
    name*: string
    version*: string
    casHash*: string

  # UTCP Address (Requirement 12.1-12.5)
  UTCPAddress* = object
    scheme*: string      ## "utcp://"
    host*: string        ## hostname or IP
    resource*: string    ## "nippel/dev-env"
    port*: int          ## optional

  # Error Codes (Requirement 1.2)
  ErrorCode* = enum
    ErrNippelNotFound
    ErrNippelAlreadyExists
    ErrNamespaceCreationFailed
    ErrCASStorageFailed
    ErrMerkleVerificationFailed
    ErrProfileLoadFailed
    ErrXDGEnforcementFailed
    ErrUTCPAddressingFailed
    ErrPermissionDenied
    ErrResourceExhausted
    ErrInvalidConfiguration
    ErrActivationFailed
    ErrDeactivationFailed

  # Nippel Error (Requirement 1.2)
  NippelError* = object of CatchableError
    code*: ErrorCode
    nippelName*: string
    context*: JsonNode

  # Main Nippel Type (Requirements 1.1-1.5, 6.1-6.8, 12.1-12.5)
  Nippel* = object
    # Identity
    name*: string
    id*: string
    created*: DateTime
    lastUsed*: DateTime

    # Configuration (Requirement 6.1-6.8)
    profile*: SecurityProfile
    profileSettings*: ProfileSettings
    isolationLevel*: IsolationLevel

    # Paths
    cellRoot*: string
    xdgDirs*: XDGDirectories

    # Namespaces (Requirement 1.1, 1.4, 1.5)
    namespaceHandle*: Option[NamespaceHandle]

    # Storage (Requirement 3.1-3.5)
    casEntries*: seq[CASEntry]
    merkleRoot*: string

    # Network (Requirement 12.1-12.5)
    utcpAddress*: UTCPAddress

    # Packages
    packages*: seq[PackageRef]

    # Metadata
    metadata*: JsonNode
    version*: string

# =============================================================================
# Helper Functions
# =============================================================================

proc `$`*(profile: SecurityProfile): string =
  ## Convert SecurityProfile to string
  case profile:
  of Workstation: "Workstation"
  of Homestation: "Homestation"
  of Satellite: "Satellite"
  of NetworkIOT: "NetworkIOT"
  of Server: "Server"

proc `$`*(level: IsolationLevel): string =
  ## Convert IsolationLevel to string
  case level:
  of None: "None"
  of Standard: "Standard"
  of Strict: "Strict"
  of Quantum: "Quantum"

proc `$`*(access: NetworkAccessLevel): string =
  ## Convert NetworkAccessLevel to string
  case access:
  of NoNetwork: "NoNetwork"
  of Limited: "Limited"
  of Relaxed: "Relaxed"
  of Full: "Full"

proc formatUTCPAddress*(address: UTCPAddress): string =
  ## Format UTCP address as string
  result = address.scheme & address.host
  if address.port > 0:
    result.add(":" & $address.port)
  result.add("/" & address.resource)
