#!/usr/bin/env nu
# VAPORA Deployment Rollback Script
# Rolls back to previous deployment versions
# Version: 1.0.0

def main [
    --target: string = "kubernetes"
    --deployment: string = "all"
    --revision: int = 0
] {
    print "🔙 VAPORA Rollback Manager"
    print $"Target: ($target) | Deployment: ($deployment)"
    print ""

    match $target {
        "docker" => { rollback-docker $deployment }
        "kubernetes" => { rollback-kubernetes $deployment $revision }
        _ => {
            error make {msg: $"Unknown target: ($target)"}
        }
    }
}

def rollback-docker [deployment: string] {
    print "⚠️  Docker rollback requires manual intervention"
    print ""
    print "Available options:"
    print "1. Using docker compose:"
    print "   $ docker compose -f docker-compose.yml.backup up -d"
    print ""
    print "2. Remove containers and redeploy:"
    print "   $ docker compose down"
    print "   $ docker compose up -d"
    print ""
    print "3. View version history:"
    let history_result = do {
        find dist -name "docker-compose*.yml*" -type f
    } | complete

    if $history_result.exit_code == 0 {
        print "   Available backups:"
        $history_result.stdout | lines | each { |line|
            print $"   • ($line)"
        }
    }
}

def rollback-kubernetes [deployment: string, revision: int] {
    print "☸️  Rolling back Kubernetes deployments..."
    print ""

    let deployments = if $deployment == "all" {
        ["vapora-backend", "vapora-agents", "vapora-llm-router"]
    } else {
        [$deployment]
    }

    $deployments | each { |deploy|
        let current_result = do {
            kubectl rollout history deployment/$deploy -n vapora
        } | complete

        if $current_result.exit_code != 0 {
            print $"⚠️  ($deploy): not found or error"
            return
        }

        print $"Deployment: ($deploy)"
        print $current_result.stdout
        print ""

        # Show available revisions
        let revisions_result = do {
            kubectl rollout history deployment/$deploy -n vapora | tail -n +2
        } | complete

        if $revision == 0 {
            print $"Reverting ($deploy) to previous revision..."
            let undo_result = do {
                kubectl rollout undo deployment/$deploy -n vapora
            } | complete

            if $undo_result.exit_code == 0 {
                print $"✓ ($deploy) rolled back"
            } else {
                print $"✗ ($deploy) rollback failed: ($undo_result.stderr)"
            }
        } else {
            print $"Reverting ($deploy) to revision ($revision)..."
            let undo_result = do {
                kubectl rollout undo deployment/$deploy --to-revision=$revision -n vapora
            } | complete

            if $undo_result.exit_code == 0 {
                print $"✓ ($deploy) rolled back to revision ($revision)"
            } else {
                print $"✗ ($deploy) rollback failed: ($undo_result.stderr)"
            }
        }

        # Wait for rollout to complete
        print "Waiting for rollout to complete..."
        let status_result = do {
            kubectl rollout status deployment/$deploy -n vapora --timeout=5m
        } | complete

        if $status_result.exit_code == 0 {
            print $"✓ ($deploy) rollout complete"
        } else {
            print $"⚠️  ($deploy) rollout timeout or error"
        }

        print ""
    }

    print "✅ Rollback complete"
}

# Run main function
main
