modules/core/cleanarch-version.ps1

#Requires -Version 5.1
<#
.SYNOPSIS
    Generator versiyonunu ve son degisiklikleri gosterir.
.EXAMPLE
    cleanarch-version
    cleanarch-version -Full # Tum changelog gosterilir
#>

[CmdletBinding(PositionalBinding=$false)]
param(
    [switch]$Full,
    [switch]$OnlyVersion,
    [Alias("h")]
    [switch]$Help
)

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

if ($Help) {
    Write-Host ""
    Write-Host " cleanarch-version" -ForegroundColor Cyan
    Write-Host " -----------------" -ForegroundColor DarkCyan
    Write-Host " Shows generator version and changelog." -ForegroundColor White
    Write-Host " Generator versiyonunu ve son degisiklikleri gosterir." -ForegroundColor DarkGray
    Write-Host ""
    Write-Host " Parameters / Parametreler:" -ForegroundColor Yellow
    Write-Host " -Full : Show full changelog / Tum changelog gecmisini gosterir" -ForegroundColor White
    Write-Host " -OnlyVersion : Return only version number (0.0.0) / Sadece versiyon numarasini dondurur" -ForegroundColor White
    Write-Host ""
    return
}

$ScriptDir     = $PSScriptRoot
$GenRoot       = Split-Path (Split-Path $ScriptDir -Parent) -Parent
$ManifestPath  = Join-Path $GenRoot "cleanarch.psd1"
$ChangelogPath = Join-Path $GenRoot "changelog.md"
$KnownIssues   = Join-Path $GenRoot "known-issues.md"



# 1. Version extraction from module manifest (Single source of truth)
$Version = "0.0.0"
if (Test-Path $ManifestPath) {
    $psd = Import-PowerShellDataFile $ManifestPath
    $Version = $psd.ModuleVersion
} elseif (Test-Path $ChangelogPath) {
    # Fallback to changelog
    $lines = Get-Content $ChangelogPath -Encoding UTF8
    $versionLine = $lines | Where-Object { $_ -match '^\s*##\s*\[v' } | Select-Object -First 1
    if ($versionLine -match '\[v?(.+?)\]') {
        $Version = $Matches[1]
    }
}

if ($OnlyVersion) {
    return $Version
}

Write-Host ""
Write-Host " Clean Architecture Template Generator" -ForegroundColor Cyan
Write-Host " --------------------------------------" -ForegroundColor DarkCyan
Write-Host " Versiyon : v$Version" -ForegroundColor White

if (Test-Path $ChangelogPath) {
    $lines = Get-Content $ChangelogPath -Encoding UTF8
    Write-Host ""

    if ($Full) {
        Write-Host "--- CHANGELOG -------------------------------" -ForegroundColor DarkCyan
        $lines | ForEach-Object { Write-Host " $_" }
    } else {
        # Son versiyonun icerigi (ilk ## blogu)
        $inBlock = $false
        $blockLines = @()
        foreach ($line in $lines) {
            if ($line -match '^\s*##\s*\[v') {
                if ($inBlock) { break }
                $inBlock = $true
                continue
            }
            if ($inBlock) { $blockLines += $line }
        }
        Write-Host " Son degisiklikler:" -ForegroundColor White
        $blockLines | Select-Object -First 15 | ForEach-Object { Write-Host " $_" }
        Write-Host ""
        Write-Host " Tum gecmis icin: (docs/releases/changelog.md)" -ForegroundColor DarkGray
    }
}

if (Test-Path $KnownIssues) {
    $issues = Get-Content $KnownIssues -Encoding UTF8 | Where-Object { $_ -match '^\s*-\s*\[' } | Select-Object -First 5
    if ($issues) {
        Write-Host " Acik sorunlar:" -ForegroundColor Yellow
        $issues | ForEach-Object { Write-Host " $_" -ForegroundColor DarkYellow }
        Write-Host ""
    }
}

$TestReportsPath = Join-Path (Split-Path $GenRoot -Parent) "docs\technical\test-reports\README.md"
if (Test-Path $TestReportsPath) {
    $reportsContent = Get-Content $TestReportsPath -Encoding UTF8
    $inList = $false
    $count = 0
    $reportLines = @()
    foreach ($line in $reportsContent) {
        if ($line -match '^\s*##\s*.*Tarihsel') {
            $inList = $true
            continue
        }
        if ($inList) {
            if ($line -match '^\s*##\s+[^#]') {
                break
            }
            if ($line -match '^\s*###\s*\[(.+?)\]\s+\D\s+(.+)') {
                $date = $Matches[1]
                $title = $Matches[2]
                $reportLines += " - [$date] $title"
                $count++
                if ($count -ge 3) { break }
            }
        }
    }
    if ($reportLines.Count -gt 0) {
        Write-Host " Son Test Dogrulamalari:" -ForegroundColor Yellow
        $reportLines | ForEach-Object { Write-Host $_ -ForegroundColor DarkYellow }
        Write-Host ""
    }
}

Write-Host " Moduller dizini : $(Split-Path $ScriptDir -Parent)" -ForegroundColor DarkGray
Write-Host " Dokumantasyon : docs/README.md" -ForegroundColor DarkGray
Write-Host ""