Distribution/Packaging.psm1

# Packaging Module for MiMo CLI
# Provides packaging and distribution capabilities

function New-MiMoPackage {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true)]
        [string]$Name,
        
        [string]$Version = "0.1.0",
        [string]$Description = "",
        [string]$Author = "Xiaomi MiMo Team",
        [string]$OutputPath = "."
    )
    
    $package = @{
        Name = $Name
        Version = $Version
        Description = $Description
        Author = $Author
        Created = Get-Date
        Files = @()
        Dependencies = @()
    }
    
    # Create package directory
    $packageDir = "$OutputPath\$Name-$Version"
    if (-not (Test-Path $packageDir)) {
        New-Item -ItemType Directory -Path $packageDir -Force | Out-Null
    }
    
    $package.Directory = $packageDir
    return $package
}

function Add-MiMoPackageFile {
    param(
        [hashtable]$Package,
        [string]$FilePath,
        [string]$TargetPath = ""
    )
    
    $file = @{
        Source = $FilePath
        Target = if ($TargetPath) { $TargetPath } else { Split-Path $FilePath -Leaf }
        Added = Get-Date
    }
    
    $Package.Files += $file
    return $Package
}

function Add-MiMoPackageDependency {
    param(
        [hashtable]$Package,
        [string]$Name,
        [string]$Version = "*"
    )
    
    $dependency = @{
        Name = $Name
        Version = $Version
    }
    
    $Package.Dependencies += $dependency
    return $Package
}

function Build-MiMoPackage {
    param([hashtable]$Package)
    
    Write-Host "Building package: $($Package.Name) v$($Package.Version)"
    
    # Copy files to package directory
    foreach ($file in $Package.Files) {
        $sourcePath = $file.Source
        $targetPath = "$($Package.Directory)\$($file.Target)"
        
        if (Test-Path $sourcePath) {
            Copy-Item -Path $sourcePath -Destination $targetPath -Force
            Write-Host " Copied: $($file.Target)"
        }
    }
    
    # Create package manifest
    $manifest = @{
        Name = $Package.Name
        Version = $Package.Version
        Description = $Package.Description
        Author = $Package.Author
        Created = $Package.Created
        Files = $Package.Files | ForEach-Object { $_.Target }
        Dependencies = $Package.Dependencies
    }
    
    $manifestPath = "$($Package.Directory)\package.json"
    $manifest | ConvertTo-Json -Depth 10 | Out-File -FilePath $manifestPath -Encoding UTF8
    
    Write-Host "Package built successfully: $($Package.Directory)"
    return $Package.Directory
}

function Install-MiMoPackage {
    param(
        [string]$PackagePath,
        [string]$InstallPath = "$env:USERPROFILE\.mimocode\packages"
    )
    
    if (-not (Test-Path $PackagePath)) {
        Write-Error "Package not found: $PackagePath"
        return $false
    }
    
    # Read package manifest
    $manifestPath = "$PackagePath\package.json"
    if (-not (Test-Path $manifestPath)) {
        Write-Error "Invalid package: missing package.json"
        return $false
    }
    
    $manifest = Get-Content -Path $manifestPath | ConvertFrom-Json
    
    # Create install directory
    $packageInstallPath = "$InstallPath\$($manifest.Name)-$($manifest.Version)"
    if (-not (Test-Path $packageInstallPath)) {
        New-Item -ItemType Directory -Path $packageInstallPath -Force | Out-Null
    }
    
    # Copy files
    Copy-Item -Path "$PackagePath\*" -Destination $packageInstallPath -Recurse -Force
    
    Write-Host "Package installed: $($manifest.Name) v$($manifest.Version)"
    Write-Host "Location: $packageInstallPath"
    
    return $true
}

function Get-MiMoInstalledPackages {
    param([string]$InstallPath = "$env:USERPROFILE\.mimocode\packages")
    
    if (-not (Test-Path $InstallPath)) {
        return @()
    }
    
    $packages = @()
    $packageDirs = Get-ChildItem -Path $InstallPath -Directory
    
    foreach ($dir in $packageDirs) {
        $manifestPath = "$($dir.FullName)\package.json"
        if (Test-Path $manifestPath) {
            $manifest = Get-Content -Path $manifestPath | ConvertFrom-Json
            $packages += @{
                Name = $manifest.Name
                Version = $manifest.Version
                Path = $dir.FullName
            }
        }
    }
    
    return $packages
}

# Export functions
Export-ModuleMember -Function New-MiMoPackage, Add-MiMoPackageFile, Add-MiMoPackageDependency, Build-MiMoPackage, Install-MiMoPackage, Get-MiMoInstalledPackages