# Bump Version Script for ekdnsr
# Automatically increments version based on semantic versioning
#
# USAGE:
#   .\bump-version.ps1 -Bump patch              # 1.0.0 -> 1.0.1 (creates and pushes git tag)
#   .\bump-version.ps1 -Bump minor              # 1.0.1 -> 1.1.0 (creates and pushes git tag)
#   .\bump-version.ps1 -Bump major              # 1.1.0 -> 2.0.0 (creates and pushes git tag)
#   .\bump-version.ps1 -Bump patch -NoPush      # Create tag locally only, don't push
#   .\bump-version.ps1 -Bump patch -DryRun      # Display only, don't create tag
#
# EXAMPLES:
#   # Bug fix release
#   .\bump-version.ps1 -Bump patch
#
#   # New feature release
#   .\bump-version.ps1 -Bump minor
#
#   # Breaking change release
#   .\bump-version.ps1 -Bump major
#
#   # Preview what would happen
#   .\bump-version.ps1 -Bump minor -DryRun

param(
    [Parameter(Mandatory=$true)]
    [ValidateSet('major','minor','patch')]
    [string]$Bump,

    [Parameter(Mandatory=$false)]
    [switch]$NoPush,

    [Parameter(Mandatory=$false)]
    [switch]$DryRun
)

$ErrorActionPreference = "Stop"

Write-Host "============================================" -ForegroundColor Cyan
Write-Host "  ekdnsr" -ForegroundColor Cyan
Write-Host "  Version Bump" -ForegroundColor Cyan
Write-Host "============================================" -ForegroundColor Cyan
Write-Host ""

# Get current version from git tags
Write-Host "Fetching current version from git tags..." -ForegroundColor Gray
$currentTag = git describe --tags --abbrev=0 2>$null

if (-not $currentTag) {
    $currentTag = "v0.0.0"
    Write-Host "No existing tags found. Starting from v0.0.0" -ForegroundColor Yellow
}

Write-Host "Current version: $currentTag" -ForegroundColor Cyan

# Parse version (remove 'v' prefix if present)
$version = $currentTag -replace '^v', ''
$parts = $version -split '\.'

if ($parts.Count -ne 3) {
    Write-Host "Error: Invalid version format '$currentTag'. Expected format: v1.2.3" -ForegroundColor Red
    exit 1
}

$major = [int]$parts[0]
$minor = [int]$parts[1]
$patch = [int]$parts[2]

# Bump version based on type
Write-Host "Bumping $Bump version..." -ForegroundColor Gray

switch ($Bump) {
    'major' {
        $major++
        $minor = 0
        $patch = 0
        Write-Host "  Major version bump (breaking changes)" -ForegroundColor Yellow
    }
    'minor' {
        $minor++
        $patch = 0
        Write-Host "  Minor version bump (new features)" -ForegroundColor Green
    }
    'patch' {
        $patch++
        Write-Host "  Patch version bump (bug fixes)" -ForegroundColor Blue
    }
}

$newVersion = "v$major.$minor.$patch"

Write-Host ""
Write-Host "Version Update:" -ForegroundColor White
Write-Host "  From: $currentTag" -ForegroundColor Red
Write-Host "  To:   $newVersion" -ForegroundColor Green
Write-Host ""

# Create git tag unless dry run
if ($DryRun) {
    Write-Host "[DRY RUN] Would create git tag: $newVersion" -ForegroundColor Yellow
    Write-Host "Run without -DryRun to create the tag." -ForegroundColor Gray
} else {
    Write-Host "Creating git tag $newVersion..." -ForegroundColor Gray

    # Check for uncommitted changes
    $status = git status --porcelain
    if ($status) {
        Write-Host "Warning: You have uncommitted changes:" -ForegroundColor Yellow
        Write-Host $status -ForegroundColor Yellow
        Write-Host ""
        $response = Read-Host "Continue with tag creation? (y/N)"
        if ($response -ne 'y' -and $response -ne 'Y') {
            Write-Host "Tag creation cancelled." -ForegroundColor Red
            exit 1
        }
    }

    # Create the tag
    try {
        git tag -a $newVersion -m "Release $newVersion"
        Write-Host "[OK] Tag created: $newVersion" -ForegroundColor Green

        # Push unless -NoPush specified
        if (-not $NoPush) {
            Write-Host "Pushing tag to origin..." -ForegroundColor Gray
            git push origin $newVersion
            Write-Host "[OK] Tag pushed to origin" -ForegroundColor Green
        } else {
            Write-Host ""
            Write-Host "Tag created locally. To push it, run:" -ForegroundColor Yellow
            Write-Host "  git push origin $newVersion" -ForegroundColor White
        }
    }
    catch {
        Write-Host "Error creating/pushing tag: $_" -ForegroundColor Red
        exit 1
    }
}

Write-Host ""
Write-Host "Next steps:" -ForegroundColor Cyan
Write-Host "  1. Build release:  .\build.ps1 -Release -Package" -ForegroundColor White
Write-Host "  2. Test the build: .\build\ekdns--help" -ForegroundColor White
Write-Host ""

# Output the new version for CI/CD pipelines
Write-Host "NEW_VERSION=$newVersion" -ForegroundColor Magenta
Write-Output $newVersion
