#!/bin/bash
# Database Restore Script for Lottery Application
# This script restores a MySQL database from a backup file
#
# Usage:
#   ./scripts/restore-database.sh <backup-file>
#
# Examples:
#   # Restore from local file
#   ./scripts/restore-database.sh /opt/app/backups/lottery_db_backup_20240101_120000.sql.gz
#
#   # Restore from backup VPS
#   ./scripts/restore-database.sh 5.45.77.77:/raid/backup/acc_260182/lottery_db_backup_20240101_120000.sql.gz
#
# Prerequisites:
#   1. Database password accessible via /run/secrets/honey-config.properties
#   2. Docker container 'honey-mysql' running
#   3. Database will be DROPPED and RECREATED (all data will be lost!)

set -euo pipefail

# Configuration
MYSQL_CONTAINER="honey-mysql"
MYSQL_DATABASE="lottery_db"
SECRET_FILE="/run/secrets/honey-config.properties"
BACKUP_VPS_USER="acc_260182"  # User account on backup VPS

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# Logging function
log() {
    echo -e "${GREEN}[$(date +'%Y-%m-%d %H:%M:%S')]${NC} $1"
}

error() {
    echo -e "${RED}[ERROR]${NC} $1" >&2
}

warn() {
    echo -e "${YELLOW}[WARN]${NC} $1"
}

# Check arguments
if [ $# -eq 0 ]; then
    error "No backup file specified"
    echo "Usage: $0 <backup-file>"
    echo ""
    echo "Examples:"
    echo "  $0 /opt/app/backups/lottery_db_backup_20240101_120000.sql.gz"
    echo "  $0 5.45.77.77:/raid/backup/acc_260182/lottery_db_backup_20240101_120000.sql.gz"
    exit 1
fi

BACKUP_SOURCE="$1"

# Check if running as root or with sudo
if [ "$EUID" -ne 0 ]; then
    error "This script must be run as root (or with sudo)"
    exit 1
fi

# Load database password
if [ ! -f "$SECRET_FILE" ]; then
    error "Secret file not found at $SECRET_FILE"
    exit 1
fi

DB_PASSWORD=$(grep "^SPRING_DATASOURCE_PASSWORD=" "$SECRET_FILE" | cut -d'=' -f2- | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')

if [ -z "$DB_PASSWORD" ]; then
    error "SPRING_DATASOURCE_PASSWORD not found in secret file"
    exit 1
fi

# Check if MySQL container is running
if ! docker ps --format '{{.Names}}' | grep -q "^${MYSQL_CONTAINER}$"; then
    error "MySQL container '${MYSQL_CONTAINER}' is not running"
    exit 1
fi

# Determine if backup is remote or local
TEMP_BACKUP="/tmp/restore_backup_$$"
BACKUP_IS_COMPRESSED=false

if [[ "$BACKUP_SOURCE" == *":"* ]]; then
    # Remote backup (format: host:/path/to/file)
    log "Detected remote backup: ${BACKUP_SOURCE}"
    HOST_PATH=(${BACKUP_SOURCE//:/ })
    REMOTE_HOST="${HOST_PATH[0]}"
    REMOTE_PATH="${HOST_PATH[1]}"
    
    log "Downloading backup from ${REMOTE_HOST}..."
    if scp "${REMOTE_HOST}:${REMOTE_PATH}" "${TEMP_BACKUP}"; then
        log "✅ Backup downloaded successfully"
    else
        error "Failed to download backup from remote VPS"
        exit 1
    fi
else
    # Local backup
    if [ ! -f "$BACKUP_SOURCE" ]; then
        error "Backup file not found: ${BACKUP_SOURCE}"
        exit 1
    fi
    log "Using local backup: ${BACKUP_SOURCE}"
    cp "$BACKUP_SOURCE" "${TEMP_BACKUP}"
fi

# Check if backup is compressed
if [[ "$TEMP_BACKUP" == *.gz ]] || file "$TEMP_BACKUP" | grep -q "gzip compressed"; then
    BACKUP_IS_COMPRESSED=true
    log "Backup is compressed (gzip)"
fi

# Get backup file size
BACKUP_SIZE=$(du -h "${TEMP_BACKUP}" | cut -f1)
log "Backup size: ${BACKUP_SIZE}"

# WARNING: This will destroy all existing data!
warn "⚠️  WARNING: This will DROP and RECREATE the database '${MYSQL_DATABASE}'"
warn "⚠️  ALL EXISTING DATA WILL BE LOST!"
echo ""
read -p "Are you sure you want to continue? Type 'YES' to confirm: " CONFIRM

if [ "$CONFIRM" != "YES" ]; then
    log "Restore cancelled by user"
    rm -f "${TEMP_BACKUP}"
    exit 0
fi

log "Starting database restore..."

# Drop and recreate database
log "Dropping existing database (if exists)..."
docker exec "${MYSQL_CONTAINER}" mysql -u root -p"${DB_PASSWORD}" -e "DROP DATABASE IF EXISTS ${MYSQL_DATABASE};" || true

log "Creating fresh database..."
docker exec "${MYSQL_CONTAINER}" mysql -u root -p"${DB_PASSWORD}" -e "CREATE DATABASE ${MYSQL_DATABASE} CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"

# Restore database
log "Restoring database from backup..."

if [ "$BACKUP_IS_COMPRESSED" = true ]; then
    # Restore from compressed backup
    if gunzip -c "${TEMP_BACKUP}" | docker exec -i "${MYSQL_CONTAINER}" mysql -u root -p"${DB_PASSWORD}" "${MYSQL_DATABASE}"; then
        log "✅ Database restored successfully from compressed backup"
    else
        error "Failed to restore database"
        rm -f "${TEMP_BACKUP}"
        exit 1
    fi
else
    # Restore from uncompressed backup
    if docker exec -i "${MYSQL_CONTAINER}" mysql -u root -p"${DB_PASSWORD}" "${MYSQL_DATABASE}" < "${TEMP_BACKUP}"; then
        log "✅ Database restored successfully"
    else
        error "Failed to restore database"
        rm -f "${TEMP_BACKUP}"
        exit 1
    fi
fi

# Clean up temporary file
rm -f "${TEMP_BACKUP}"

# Verify restore
log "Verifying restore..."
TABLE_COUNT=$(docker exec "${MYSQL_CONTAINER}" mysql -u root -p"${DB_PASSWORD}" -N -e "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = '${MYSQL_DATABASE}';" 2>/dev/null || echo "0")

if [ "$TABLE_COUNT" -gt 0 ]; then
    log "✅ Restore verified: ${TABLE_COUNT} tables found in database"
else
    warn "⚠️  Warning: No tables found in database after restore"
fi

log "✅ Database restore completed!"
warn "⚠️  Remember to restart the backend container if it's running:"
warn "   docker restart honey-backend"

