# nimpak/types/grafting_types.nim
# Shared types for grafting system to avoid circular imports

import std/[tables, times, json, options]
export tables, times, json, options

type
  # Package adapter interface
  PackageAdapter* = ref object of RootObj
    name*: string
    priority*: int
    enabled*: bool

  # Grafting cache for performance
  GraftingCache* = object
    cacheDir*: string
    metadata*: Table[string, GraftedPackageMetadata]
    archives*: Table[string, string]  # hash -> path

  GraftingConfig* = object
    enabled*: bool
    verifyGraftedPackages*: bool
    convertToNpkAutomatically*: bool
    adapters*: Table[string, AdapterConfig]

  AdapterConfig* = object
    enabled*: bool
    priority*: int
    settings*: JsonNode

  GraftedPackageMetadata* = object
    packageName*: string
    version*: string
    source*: string
    graftedAt*: DateTime
    originalHash*: string
    graftHash*: string
    buildLog*: string
    provenance*: ProvenanceInfo

  ProvenanceInfo* = object
    originalSource*: string
    downloadUrl*: string
    archivePath*: string
    extractedPath*: string
    conversionLog*: string

  GraftTransaction* = object
    id*: string
    packageName*: string
    adapter*: string
    status*: GraftStatus
    startTime*: DateTime
    endTime*: Option[DateTime]
    operations*: seq[GraftOperation]

  GraftStatus* = enum
    GraftPending, GraftInProgress, GraftCompleted, GraftFailed, GraftRolledBack

  GraftOperation* = object
    kind*: GraftOperationKind
    target*: string
    data*: JsonNode
    timestamp*: DateTime

  GraftOperationKind* = enum
    Download, Extract, Convert, Verify, Install, Cleanup

  GraftResult* = object
    success*: bool
    packageId*: string  # Using string instead of PackageId to avoid more imports
    metadata*: GraftedPackageMetadata
    npkPath*: Option[string]
    errors*: seq[string]