Distribution/Update.psm1

# Update Module for MiMo CLI
# Provides update and version management

function Get-MiMoVersion {
    return @{
        Version = "0.2.0"
        Build = "2026-06-14"
        Channel = "stable"
    }
}

function Check-MiMoUpdate {
    [CmdletBinding()]
    param(
        [string]$UpdateUrl = "https://api.xiaomi.com/mimo/updates"
    )
    
    Write-Host "Checking for updates..."
    
    try {
        $currentVersion = Get-MiMoVersion
        $response = Invoke-RestMethod -Uri $UpdateUrl -Method Get
        
        if ($response.version -gt $currentVersion.Version) {
            Write-Host "Update available: $($response.version)" -ForegroundColor Yellow
            return @{
                Available = $true
                Version = $response.version
                DownloadUrl = $response.downloadUrl
                ReleaseNotes = $response.releaseNotes
            }
        }
        else {
            Write-Host "You are up to date (v$($currentVersion.Version))" -ForegroundColor Green
            return @{
                Available = $false
                Version = $currentVersion.Version
            }
        }
    }
    catch {
        Write-Warning "Failed to check for updates: $_"
        return @{
            Available = $false
            Error = $_.Exception.Message
        }
    }
}

function Update-MiMo {
    [CmdletBinding()]
    param(
        [string]$Version = "",
        [switch]$Force
    )
    
    $updateInfo = Check-MiMoUpdate
    
    if (-not $updateInfo.Available -and -not $Force) {
        Write-Host "No updates available"
        return
    }
    
    if ($updateInfo.Error -and -not $Force) {
        Write-Error "Cannot check for updates: $($updateInfo.Error)"
        return
    }
    
    Write-Host "Updating MiMo CLI..."
    
    # Download update
    $downloadUrl = if ($Version) { "https://api.xiaomi.com/mimo/updates/$Version" } else { $updateInfo.DownloadUrl }
    
    try {
        $tempDir = "$env:TEMP\MiMo-Update"
        if (-not (Test-Path $tempDir)) {
            New-Item -ItemType Directory -Path $tempDir -Force | Out-Null
        }
        
        $zipFile = "$tempDir\mimo-update.zip"
        Invoke-WebRequest -Uri $downloadUrl -OutFile $zipFile
        
        # Extract update
        Expand-Archive -Path $zipFile -DestinationPath $tempDir -Force
        
        # Install update
        $installScript = "$tempDir\install.ps1"
        if (Test-Path $installScript) {
            & $installScript
        }
        
        Write-Host "Update completed successfully" -ForegroundColor Green
    }
    catch {
        Write-Error "Update failed: $_"
    }
    finally {
        # Cleanup
        if (Test-Path $tempDir) {
            Remove-Item -Path $tempDir -Recurse -Force
        }
    }
}

function Get-MiMoChangelog {
    param([int]$Count = 10)
    
    $changelog = @(
        @{
            Version = "0.2.0"
            Date = "2026-06-14"
            Changes = @(
                "Added AI model integration",
                "Enhanced IDE support",
                "Improved workflow engine",
                "Added enterprise features"
            )
        },
        @{
            Version = "0.1.0"
            Date = "2026-06-13"
            Changes = @(
                "Initial release",
                "Basic CLI commands",
                "GitHub integration",
                "Configuration system"
            )
        }
    )
    
    return $changelog | Select-Object -First $Count
}

# Export functions
Export-ModuleMember -Function Get-MiMoVersion, Check-MiMoUpdate, Update-MiMo, Get-MiMoChangelog