# Version Bump Script
# Aggiorna automaticamente la versione nel .csproj e crea tag Git

param(
    [Parameter(Mandatory=$true)]
    [ValidateSet('major', 'minor', 'patch')]
    [string]$BumpType,
    
    [Parameter(Mandatory=$false)]
    [string]$Message = ""
)

$ErrorActionPreference = "Stop"

Write-Host ""
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "?? TradingBot Version Bump" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""

# Path al .csproj
$csprojPath = Join-Path $PSScriptRoot "TradingBot.csproj"

if (-not (Test-Path $csprojPath)) {
    Write-Host "? File TradingBot.csproj non trovato!" -ForegroundColor Red
    exit 1
}

# Leggi versione corrente
Write-Host "?? Reading current version..." -ForegroundColor Yellow
[xml]$csproj = Get-Content $csprojPath
$currentVersion = $csproj.Project.PropertyGroup.Version

if (-not $currentVersion) {
    Write-Host "? Version not found in .csproj!" -ForegroundColor Red
    exit 1
}

Write-Host "   Current version: $currentVersion" -ForegroundColor Gray

# Parse versione
$versionParts = $currentVersion.Split('.')
$major = [int]$versionParts[0]
$minor = [int]$versionParts[1]
$patch = [int]$versionParts[2]

# Bump versione
switch ($BumpType) {
    'major' {
        $major++
        $minor = 0
        $patch = 0
        Write-Host "   Bumping: MAJOR version (breaking changes)" -ForegroundColor Magenta
    }
    'minor' {
        $minor++
        $patch = 0
        Write-Host "   Bumping: MINOR version (new features)" -ForegroundColor Blue
    }
    'patch' {
        $patch++
        Write-Host "   Bumping: PATCH version (bug fixes)" -ForegroundColor Green
    }
}

$newVersion = "$major.$minor.$patch"
$newAssemblyVersion = "$major.$minor.$patch.0"

Write-Host "   New version: $newVersion" -ForegroundColor Green
Write-Host ""

# Aggiorna .csproj
Write-Host "?? Updating TradingBot.csproj..." -ForegroundColor Yellow

# Update Version
$csproj.Project.PropertyGroup.Version = $newVersion
$csproj.Project.PropertyGroup.AssemblyVersion = $newAssemblyVersion
$csproj.Project.PropertyGroup.FileVersion = $newAssemblyVersion

# Save
$csproj.Save($csprojPath)

Write-Host "   ? Updated Version: $newVersion" -ForegroundColor Green
Write-Host "   ? Updated AssemblyVersion: $newAssemblyVersion" -ForegroundColor Green
Write-Host ""

# Git commit
Write-Host "?? Creating Git commit..." -ForegroundColor Yellow

$commitMessage = if ($Message) { 
    "chore: Bump version to $newVersion - $Message" 
} else { 
    "chore: Bump version to $newVersion" 
}

git add $csprojPath

if ($LASTEXITCODE -ne 0) {
    Write-Host "? Git add failed!" -ForegroundColor Red
    exit 1
}

git commit -m $commitMessage

if ($LASTEXITCODE -ne 0) {
    Write-Host "? Git commit failed!" -ForegroundColor Red
    exit 1
}

Write-Host "   ? Committed: $commitMessage" -ForegroundColor Green
Write-Host ""

# Git tag
Write-Host "???  Creating Git tag..." -ForegroundColor Yellow

$tagName = "v$newVersion"
$tagMessage = if ($Message) {
    "Release $newVersion - $Message"
} else {
    "Release $newVersion"
}

git tag -a $tagName -m $tagMessage

if ($LASTEXITCODE -ne 0) {
    Write-Host "? Git tag failed!" -ForegroundColor Red
    exit 1
}

Write-Host "   ? Created tag: $tagName" -ForegroundColor Green
Write-Host ""

# Summary
Write-Host "========================================" -ForegroundColor Green
Write-Host "? Version bumped successfully!" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Green
Write-Host ""
Write-Host "?? Version: $currentVersion ? $newVersion" -ForegroundColor White
Write-Host "???  Git tag: $tagName" -ForegroundColor White
Write-Host ""
Write-Host "?? Next steps:" -ForegroundColor Cyan
Write-Host "   1. Push commit: git push origin main" -ForegroundColor White
Write-Host "   2. Push tag: git push origin $tagName" -ForegroundColor White
Write-Host "   3. Build & Publish: Visual Studio ? Publish (Docker profile)" -ForegroundColor White
Write-Host "   4. Deploy on Unraid" -ForegroundColor White
Write-Host ""
Write-Host "?? Or push both at once:" -ForegroundColor Cyan
Write-Host "   git push origin main --tags" -ForegroundColor White
Write-Host ""
