Config/Config.psm1

# Configuration Module for MiMo CLI - Enhanced Version
# Provides flexible configuration and rule system

function Get-MiMoConfig {
    [CmdletBinding()]
    param(
        [string]$Key,
        [switch]$IncludeEnvVars
    )
    
    $configPath = "$env:USERPROFILE\.mimocode\config.json"
    $config = @{}
    
    # Load from file
    if (Test-Path $configPath) {
        $fileConfig = Get-Content -Path $configPath | ConvertFrom-Json
        foreach ($prop in $fileConfig.PSObject.Properties) {
            $config[$prop.Name] = $prop.Value
        }
    }
    
    # Load from environment variables
    if ($IncludeEnvVars) {
        $envVars = Get-ChildItem Env: | Where-Object { $_.Name -like "MIMO_*" }
        foreach ($envVar in $envVars) {
            $key = ($envVar.Name -replace "MIMO_", "" -replace "_", ".").ToLower()
            $config[$key] = $envVar.Value
        }
    }
    
    if ($Key) {
        return $config[$Key]
    }
    else {
        return $config
    }
}

function Set-MiMoConfig {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true)]
        [string]$Key,
        
        [Parameter(Mandatory=$true)]
        $Value,
        
        [switch]$Global,
        [switch]$AsEnvVar
    )
    
    if ($AsEnvVar) {
        # Set as environment variable
        $envKey = "MIMO_" + ($Key -replace "\.", "_" -replace "-", "_")
        [Environment]::SetEnvironmentVariable($envKey, $Value, "User")
        Write-Host "Environment variable set: $envKey = $Value"
        return
    }
    
    $configDir = "$env:USERPROFILE\.mimocode"
    $configPath = "$configDir\config.json"
    
    # Create directory if it doesn't exist
    if (-not (Test-Path $configDir)) {
        New-Item -ItemType Directory -Path $configDir -Force | Out-Null
    }
    
    if (Test-Path $configPath) {
        $config = Get-Content -Path $configPath | ConvertFrom-Json
    }
    else {
        $config = @{}
    }
    
    $config.$Key = $Value
    $config | ConvertTo-Json -Depth 10 | Out-File -FilePath $configPath -Encoding UTF8
    Write-Host "Configuration updated: $Key = $Value"
}

function Remove-MiMoConfig {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true)]
        [string]$Key,
        
        [switch]$Global,
        [switch]$AsEnvVar
    )
    
    if ($AsEnvVar) {
        # Remove environment variable
        $envKey = "MIMO_" + ($Key -replace "\.", "_" -replace "-", "_")
        [Environment]::SetEnvironmentVariable($envKey, $null, "User")
        Write-Host "Environment variable removed: $envKey"
        return
    }
    
    $configPath = "$env:USERPROFILE\.mimocode\config.json"
    
    if (Test-Path $configPath) {
        $config = Get-Content -Path $configPath | ConvertFrom-Json
        
        if ($config.$Key) {
            $config.PSObject.Properties.Remove($Key)
            $config | ConvertTo-Json -Depth 10 | Out-File -FilePath $configPath -Encoding UTF8
            Write-Host "Configuration key removed: $Key"
        }
        else {
            Write-Host "Configuration key not found: $Key"
        }
    }
    else {
        Write-Host "No configuration file found"
    }
}

function Get-MiMoConfigPath {
    param([switch]$Global)
    
    if ($Global) {
        return "$env:USERPROFILE\.mimocode"
    }
    else {
        return "."
    }
}

function Test-MiMoConfig {
    param([string]$Key)
    
    $config = Get-MiMoConfig
    return $config.ContainsKey($Key)
}

function Get-MiMoConfigKeys {
    $config = Get-MiMoConfig
    return $config.Keys
}

function Get-MiMoRules {
    [CmdletBinding()]
    param()
    
    $rulesPath = "$env:USERPROFILE\.mimocode\rules.json"
    
    if (Test-Path $rulesPath) {
        $rules = Get-Content -Path $rulesPath | ConvertFrom-Json
        return $rules
    }
    else {
        # Return default rules
        $defaultRules = @(
            @{
                Name = "Code Style"
                Pattern = "function\s+\w+"
                Action = "format"
                Description = "Format function declarations"
            },
            @{
                Name = "Error Handling"
                Pattern = "catch\s*\("
                Action = "review"
                Description = "Review error handling"
            }
        )
        return $defaultRules
    }
}

function Set-MiMoRules {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true)]
        [array]$Rules
    )
    
    $rulesPath = "$env:USERPROFILE\.mimocode\rules.json"
    $Rules | ConvertTo-Json -Depth 10 | Out-File -FilePath $rulesPath -Encoding UTF8
    Write-Host "Rules updated"
}

# Export functions
Export-ModuleMember -Function Get-MiMoConfig, Set-MiMoConfig, Remove-MiMoConfig, Get-MiMoConfigPath, Test-MiMoConfig, Get-MiMoConfigKeys, Get-MiMoRules, Set-MiMoRules