cleanarch.psm1

# cleanarch.psm1

$ScriptRoot = $PSScriptRoot

# UTF-8 encoding (Türkçe karakter desteği)
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
$OutputEncoding            = [System.Text.Encoding]::UTF8

function cleanarch-new {
    [CmdletBinding(PositionalBinding=$false)]
    param(
        [string]$ProjectName = "",
        [ValidateSet("Dapper","EFCore","Hybrid","")]
        [string]$OrmMode = "",
        [string[]]$Entities = @("User", "Product"),
        [ValidateSet("MSSQL","Postgres")]
        [string]$DbProvider = "",
        [string]$OutputRootPath = "",
        [switch]$SkipFrontend,
        [switch]$SkipAiGuide,
        [switch]$SkipTests,
        [switch]$SkipCi,
        [switch]$DryRun,
        [string]$IdeConfig = "VSCode",
        [switch]$Observability,
        [switch]$NonInteractive,
        [string]$Language = "tr",
        [ValidateSet("Web","Mobile","Both","")]
        [string]$Platform = "",
        [ValidateSet("Layered","FeatureSliced")]
        [string]$Architecture = "Layered",
        [switch]$SkipNpm,
        [switch]$Version,
        [Alias("h")]
        [switch]$Help
    )

    & "$ScriptRoot\cleanarch-new.ps1" @PSBoundParameters
}

function cleanarch-version {
    [CmdletBinding(PositionalBinding=$false)]
    param(
        [switch]$Full,
        [switch]$OnlyVersion,
        [Alias("h")]
        [switch]$Help
    )
    & "$ScriptRoot\modules\core\cleanarch-version.ps1" @PSBoundParameters
}

function cleanarch-update {
    [CmdletBinding(PositionalBinding=$false)]
    param(
        [Alias("h")]
        [switch]$Help
    )
    if ($Help) {
        Write-Host ""
        Write-Host " cleanarch-update" -ForegroundColor Cyan
        Write-Host " ----------------" -ForegroundColor DarkCyan
        Write-Host " Updates the module to the latest version from PowerShell Gallery." -ForegroundColor White
        Write-Host " Modulu PowerShell Gallery uzerinden en son surume gunceller." -ForegroundColor DarkGray
        Write-Host ""
        return
    }
    Write-Host "Updating cleanarch module from PowerShell Gallery..." -ForegroundColor Cyan
    Write-Host "cleanarch modulu PowerShell Gallery uzerinden guncelleniyor..." -ForegroundColor DarkGray
    Update-Module -Name cleanarch -Force -ErrorAction Stop
    Write-Host "Update complete! Run 'cleanarch-version' to check the new version." -ForegroundColor Green
    Write-Host "Guncelleme tamamlandi! Yeni versiyon icin 'cleanarch-version' calistirabilirsiniz." -ForegroundColor DarkGray
}

function cleanarch-uninstall {
    [CmdletBinding(PositionalBinding=$false)]
    param(
        [Alias("h")]
        [switch]$Help
    )
    if ($Help) {
        Write-Host ""
        Write-Host " cleanarch-uninstall" -ForegroundColor Cyan
        Write-Host " -------------------" -ForegroundColor DarkCyan
        Write-Host " Completely removes the module from the system." -ForegroundColor White
        Write-Host " Modulu sistemden tamamen kaldirir." -ForegroundColor DarkGray
        Write-Host ""
        return
    }
    Write-Host "Removing cleanarch module from system..." -ForegroundColor Yellow
    Write-Host "cleanarch modulu sistemden kaldiriliyor..." -ForegroundColor DarkGray
    
    # PowerShell profilinden eski wrapper fonksiyonları varsa temizle
    if (Test-Path $PROFILE) {
        $profileContent = Get-Content $PROFILE -Raw -ErrorAction SilentlyContinue
        if ($profileContent) {
            $cleaned = $profileContent -replace "(?s)\r?\nfunction New-CleanProject \{.*?\}\r?\n", ""
            $cleaned = $cleaned -replace "(?s)\r?\nfunction Update-Generator \{.*?\}\r?\n", ""
            $cleaned = $cleaned -replace "(?s)\r?\nfunction Get-GeneratorVersion \{.*?\}\r?\n", ""
            $cleaned = $cleaned -replace "(?s)\r?\nfunction Uninstall-Generator \{.*?\}\r?\n", ""
            $cleaned = $cleaned -replace "(?s)\r?\nfunction cleanarch-new \{.*?\}\r?\n", ""
            $cleaned = $cleaned -replace "(?s)\r?\nfunction cleanarch-version \{.*?\}\r?\n", ""
            $cleaned = $cleaned -replace "(?s)\r?\nfunction cleanarch-update \{.*?\}\r?\n", ""
            $cleaned = $cleaned -replace "(?s)\r?\nfunction cleanarch-uninstall \{.*?\}\r?\n", ""
            [System.IO.File]::WriteAllText($PROFILE, $cleaned.TrimEnd() + "`n")
        }
    }
    
    # Modülü uninstall et
    Uninstall-Module -Name cleanarch -AllVersions -Force -ErrorAction SilentlyContinue
    
    # Mevcut oturumdan fonksiyonları kaldır
    Remove-Item Function:\cleanarch-new -ErrorAction SilentlyContinue
    Remove-Item Function:\cleanarch-version -ErrorAction SilentlyContinue
    Remove-Item Function:\cleanarch-update -ErrorAction SilentlyContinue
    Remove-Item Function:\cleanarch-uninstall -ErrorAction SilentlyContinue
    
    Write-Host "cleanarch module successfully removed." -ForegroundColor Green
    Write-Host "cleanarch modulu basariyla kaldirildi." -ForegroundColor DarkGray
}

Export-ModuleMember -Function cleanarch-new, cleanarch-version, cleanarch-update, cleanarch-uninstall