modules/core/Update-Generator.ps1

#Requires -Version 5.1
[CmdletBinding()]
param(
    [Alias("h")]
    [switch]$Help
)

if ($Help) {
    Write-Host ""
    Write-Host " Update-Generator" -ForegroundColor Cyan
    Write-Host " ----------------" -ForegroundColor DarkCyan
    Write-Host " Generator'i GitHub veya Git uzerinden en son surume gunceller."
    Write-Host ""
    Write-Host " Kullanim: Update-Generator"
    Write-Host ""
    return
}



$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
Push-Location $ScriptDir

function Write-Ok($msg)   { Write-Host "[OK] $msg" -ForegroundColor Green }
function Write-Err($msg)  { Write-Host "[ERROR] $msg" -ForegroundColor Red }
function Write-Warn($msg) { Write-Host "[WARN] $msg" -ForegroundColor Yellow }

Write-Host "================================================" -ForegroundColor DarkCyan
Write-Host " Clean Architecture Generator - Guncelleme " -ForegroundColor Cyan
Write-Host "================================================" -ForegroundColor DarkCyan
Write-Host ""

# Ensure TLS 1.2 for modern web requests
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

$CurrentVersion = & "$ScriptDir\Get-GeneratorVersion.ps1" -OnlyVersion

$GenRoot = Split-Path (Split-Path $ScriptDir -Parent) -Parent
$GitRoot = $GenRoot
if (-not (Test-Path (Join-Path $GitRoot ".git"))) {
    $GitRoot = Split-Path $GenRoot -Parent
}


if (Test-Path (Join-Path $GitRoot ".git")) {
    # Git-based update
    Push-Location $GitRoot
    Write-Host "[INFO] Git uzerinden guncellestirmeler kontrol ediliyor..." -ForegroundColor Cyan
    & git fetch origin main 2>$null
    
    $manifestContent = & git show origin/main:generator/generator.manifest.json 2>$null
    if ($LASTEXITCODE -eq 0 -and $manifestContent) {
        $RemoteVersion = $manifestContent | ConvertFrom-Json | Select-Object -ExpandProperty version
        
        if ($CurrentVersion -eq $RemoteVersion) {
            Write-Ok "Generator zaten guncel (v$CurrentVersion)"
        } else {
            Write-Warn "Yeni versiyon mevcut: v$RemoteVersion (Mevcut: v$CurrentVersion)"
            Write-Host "Guncelleniyor..."
            & git pull origin main
            if ($LASTEXITCODE -eq 0) {
                Write-Ok "Guncelleme basarili."
            } else {
                Write-Err "Guncelleme sirasinda hata olustu."
            }
        }
    } else {
        Write-Host "Guncellemeler kontrol edilemedi, git pull deneniyor..."
        & git pull origin main
    }
    Pop-Location
} else {
    # Zip/Manual-based update
    Write-Host "[INFO] Zip/Manuel kurulum tespit edildi. GitHub uzerinden kontrol ediliyor..." -ForegroundColor Cyan
    $repoUrl = "https://raw.githubusercontent.com/YusufYiildiiriim/CleanArchitectureTemplateGenerator/main/generator/generator.manifest.json"
    try {
        $manifest = Invoke-RestMethod -Uri $repoUrl -UseBasicParsing
        $RemoteVersion = $manifest.version
        
        if ($CurrentVersion -eq $RemoteVersion) {
            Write-Ok "Generator zaten guncel (v$CurrentVersion)"
        } else {
            Write-Warn "Yeni versiyon mevcut: v$RemoteVersion (Mevcut: v$CurrentVersion)"
            Write-Host "Lutfen en son surumu GitHub uzerinden indirin:"
            Write-Host "https://github.com/YusufYiildiiriim/CleanArchitectureTemplateGenerator/archive/refs/heads/main.zip" -ForegroundColor Cyan
        }
    } catch {
        Write-Err "Guncelleme kontrolu basarisiz oldu: $_"
    }
}

Write-Host ""
Pop-Location