#!/bin/bash

# Backup management script for Mold Cost Calculator
# This script provides easy commands for backup operations

set -e

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

# Function to print colored output
print_status() {
    echo -e "${GREEN}[INFO]${NC} $1"
}

print_warning() {
    echo -e "${YELLOW}[WARNING]${NC} $1"
}

print_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

print_header() {
    echo -e "${BLUE}=== $1 ===${NC}"
}

# Function to create manual backup
create_backup() {
    print_header "Creating Manual Backup"
    if podman-compose exec backup /backup.sh; then
        print_status "Manual backup completed successfully!"
    else
        print_error "Manual backup failed!"
        exit 1
    fi
}

# Function to list backups
list_backups() {
    print_header "Available Backups"
    if [ -d "./backups" ]; then
        if [ "$(ls -A ./backups)" ]; then
            ls -lah ./backups/backup_*.sql.gz 2>/dev/null | while read -r line; do
                echo "  $line"
            done
        else
            print_warning "No backup files found in ./backups directory"
        fi
    else
        print_warning "Backups directory does not exist"
    fi
}

# Function to restore from backup
restore_backup() {
    local backup_file="$1"
    
    if [ -z "$backup_file" ]; then
        print_error "Please specify a backup file to restore from."
        echo "Usage: $0 restore <backup_file>"
        echo "Example: $0 restore ./backups/backup_20241201_120000.sql.gz"
        exit 1
    fi
    
    if [ ! -f "$backup_file" ]; then
        print_error "Backup file '$backup_file' not found!"
        list_backups
        exit 1
    fi
    
    print_header "Database Restore"
    print_warning "This will overwrite the current database!"
    echo "Backup file: $backup_file"
    read -p "Are you sure you want to continue? (y/N): " -n 1 -r
    echo
    
    if [[ $REPLY =~ ^[Yy]$ ]]; then
        print_status "Stopping application..."
        podman-compose stop app
        
        print_status "Restoring database..."
        if podman-compose exec backup /restore.sh restore "/backups/$(basename "$backup_file")"; then
            print_status "Database restored successfully!"
            print_status "Starting application..."
            podman-compose start app
            print_status "Restore completed!"
        else
            print_error "Database restore failed!"
            print_status "Starting application..."
            podman-compose start app
            exit 1
        fi
    else
        print_status "Restore cancelled."
    fi
}

# Function to show backup status
show_status() {
    print_header "Backup Service Status"
    
    # Check if backup container is running
    if podman-compose ps backup | grep -q "Up"; then
        print_status "Backup service is running"
    else
        print_error "Backup service is not running"
    fi
    
    # Show recent backup logs
    print_header "Recent Backup Logs"
    podman-compose logs --tail=10 backup
    
    # List available backups
    list_backups
}

# Function to show help
show_help() {
    print_header "Backup Management Script"
    echo "Usage: $0 [command] [options]"
    echo ""
    echo "Commands:"
    echo "  backup                    - Create a manual backup"
    echo "  list                      - List available backups"
    echo "  restore <backup_file>     - Restore database from backup"
    echo "  status                    - Show backup service status"
    echo "  help                      - Show this help message"
    echo ""
    echo "Examples:"
    echo "  $0 backup"
    echo "  $0 list"
    echo "  $0 restore ./backups/backup_20241201_120000.sql.gz"
    echo "  $0 status"
}

# Main script logic
case "${1:-help}" in
    "backup"|"create")
        create_backup
        ;;
    "list"|"ls")
        list_backups
        ;;
    "restore")
        restore_backup "$2"
        ;;
    "status")
        show_status
        ;;
    "help"|"--help"|"-h"|"")
        show_help
        ;;
    *)
        print_error "Unknown command '$1'"
        echo ""
        show_help
        exit 1
        ;;
esac 