#!/bin/bash

# MongoDB Migration Status Script
# Provides status information about the migration process

# Source settings
source $SNAP/bin/wekan-read-settings

# Configuration
MIGRATION_STATUS="${SNAP_COMMON}/mongodb-migration-status.json"
MIGRATION_LOG="${SNAP_COMMON}/mongodb-migration-log.txt"
REVERT_FILE="${SNAP_COMMON}/revert-mongodb-migration.txt"

# Show migration status
show_status() {
    if [ -f "$MIGRATION_STATUS" ]; then
        echo "=== MongoDB Migration Status ==="
        echo "Status: $(jq -r '.status' "$MIGRATION_STATUS" 2>/dev/null || echo "unknown")"
        echo "Progress: $(jq -r '.step' "$MIGRATION_STATUS" 2>/dev/null || echo "0")/$(jq -r '.total_steps' "$MIGRATION_STATUS" 2>/dev/null || echo "0") steps"
        echo "Percentage: $(jq -r '.percentage' "$MIGRATION_STATUS" 2>/dev/null || echo "0")%"
        echo "Current Step: $(jq -r '.description' "$MIGRATION_STATUS" 2>/dev/null || echo "Unknown")"
        echo "Last Updated: $(jq -r '.timestamp' "$MIGRATION_STATUS" 2>/dev/null || echo "Unknown")"
        echo ""

        if [ -f "$MIGRATION_LOG" ]; then
            echo "=== Recent Migration Log ==="
            tail -10 "$MIGRATION_LOG"
        fi
    else
        echo "No migration in progress or completed."
    fi
}

# Show migration log
show_log() {
    if [ -f "$MIGRATION_LOG" ]; then
        echo "=== Full Migration Log ==="
        cat "$MIGRATION_LOG"
    else
        echo "No migration log found."
    fi
}

# Check if migration is needed
check_needed() {
    if [ -d "${SNAP_COMMON}/wekan" ] && [ ! -f "${SNAP_COMMON}/mongodb-version-7" ]; then
        echo "Migration needed: MongoDB 3 data detected"
        return 0
    else
        echo "No migration needed"
        return 1
    fi
}

# Start migration
start_migration() {
    if check_needed; then
        echo "Starting MongoDB migration..."
        $SNAP/bin/mongodb-migrate
    else
        echo "Migration not needed or already completed"
    fi
}

# Request revert
request_revert() {
    echo "Requesting migration revert..."
    touch "$REVERT_FILE"
    echo "Revert requested. Restart Wekan to apply revert."
}

# Clear migration status
clear_status() {
    echo "Clearing migration status..."
    rm -f "$MIGRATION_STATUS"
    rm -f "$MIGRATION_LOG"
    rm -f "$REVERT_FILE"
    echo "Migration status cleared."
}

# Show help
show_help() {
    echo "MongoDB Migration Status Tool"
    echo ""
    echo "Usage: $0 [command]"
    echo ""
    echo "Commands:"
    echo "  status    - Show current migration status (default)"
    echo "  log       - Show full migration log"
    echo "  check     - Check if migration is needed"
    echo "  start     - Start migration if needed"
    echo "  revert    - Request migration revert"
    echo "  clear     - Clear migration status"
    echo "  help      - Show this help"
    echo ""
}

# Main execution
case "${1:-status}" in
    "status")
        show_status
        ;;
    "log")
        show_log
        ;;
    "check")
        check_needed
        ;;
    "start")
        start_migration
        ;;
    "revert")
        request_revert
        ;;
    "clear")
        clear_status
        ;;
    "help"|"-h"|"--help")
        show_help
        ;;
    *)
        echo "Unknown command: $1"
        show_help
        exit 1
        ;;
esac
