#!/bin/bash

# Rollback Deployment Script
# Restores previous deployment from backup

set -e

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

REMOTE_USER="${REMOTE_USER:-deploy}"
REMOTE_HOST="${REMOTE_HOST:-141.136.35.199}"
REMOTE_DIR="/opt/ai-tax-agent"

echo -e "${RED}========================================${NC}"
echo -e "${RED}AI Tax Agent - Deployment Rollback${NC}"
echo -e "${RED}========================================${NC}"
echo ""

# Confirm rollback
echo -e "${YELLOW}WARNING: This will rollback to the previous deployment!${NC}"
echo -e "${YELLOW}All current changes will be lost.${NC}"
echo ""
read -p "Are you sure you want to rollback? (yes/no): " confirm

if [ "$confirm" != "yes" ]; then
    echo -e "${BLUE}Rollback cancelled.${NC}"
    exit 0
fi

echo ""
echo -e "${BLUE}Step 1: Listing available backups${NC}"
echo "-----------------------------------"

ssh ${REMOTE_USER}@${REMOTE_HOST} "ls -lht ${REMOTE_DIR}/backups/ | head -10"

echo ""
read -p "Enter backup timestamp to restore (e.g., 20250104_120000): " backup_timestamp

if [ -z "$backup_timestamp" ]; then
    echo -e "${RED}No backup timestamp provided. Exiting.${NC}"
    exit 1
fi

BACKUP_DIR="${REMOTE_DIR}/backups/${backup_timestamp}"

echo ""
echo -e "${BLUE}Step 2: Verifying backup exists${NC}"
echo "--------------------------------"

if ! ssh ${REMOTE_USER}@${REMOTE_HOST} "[ -d ${BACKUP_DIR} ]"; then
    echo -e "${RED}Backup directory not found: ${BACKUP_DIR}${NC}"
    exit 1
fi

echo -e "${GREEN}✓ Backup found${NC}"

echo ""
echo -e "${BLUE}Step 3: Stopping current services${NC}"
echo "----------------------------------"

ssh ${REMOTE_USER}@${REMOTE_HOST} << 'EOF'
cd /opt/ai-tax-agent
docker compose -f compose/production/services.yaml down
docker compose -f compose/production/infrastructure.yaml down
docker compose -f compose/production/monitoring.yaml down
EOF

echo -e "${GREEN}✓ Services stopped${NC}"

echo ""
echo -e "${BLUE}Step 4: Restoring configuration files${NC}"
echo "--------------------------------------"

ssh ${REMOTE_USER}@${REMOTE_HOST} << EOF
# Restore compose files
cp ${BACKUP_DIR}/infrastructure.yaml ${REMOTE_DIR}/compose/production/
cp ${BACKUP_DIR}/services.yaml ${REMOTE_DIR}/compose/production/
cp ${BACKUP_DIR}/monitoring.yaml ${REMOTE_DIR}/compose/production/

# Restore environment file
cp ${BACKUP_DIR}/.env.production ${REMOTE_DIR}/compose/

# Restore Traefik config if exists
if [ -f ${BACKUP_DIR}/traefik-dynamic.yml ]; then
    cp ${BACKUP_DIR}/traefik-dynamic.yml /opt/compose/traefik/config/
fi
EOF

echo -e "${GREEN}✓ Configuration restored${NC}"

echo ""
echo -e "${BLUE}Step 5: Restarting services${NC}"
echo "---------------------------"

ssh ${REMOTE_USER}@${REMOTE_HOST} << 'EOF'
cd /opt/ai-tax-agent

# Start infrastructure first
docker compose -f compose/production/infrastructure.yaml up -d

# Wait for infrastructure to be ready
echo "Waiting 30 seconds for infrastructure to initialize..."
sleep 30

# Start application services
docker compose -f compose/production/services.yaml up -d

# Wait for services to start
echo "Waiting 20 seconds for services to start..."
sleep 20

# Start monitoring
docker compose -f compose/production/monitoring.yaml up -d
EOF

echo -e "${GREEN}✓ Services restarted${NC}"

echo ""
echo -e "${BLUE}Step 6: Verifying deployment${NC}"
echo "----------------------------"

# Check running containers
ssh ${REMOTE_USER}@${REMOTE_HOST} "docker ps --format 'table {{.Names}}\t{{.Status}}' | grep -E '(apa-vault|apa-minio|apa-postgres|apa-svc-)'"

echo ""
echo -e "${GREEN}========================================${NC}"
echo -e "${GREEN}Rollback Complete${NC}"
echo -e "${GREEN}========================================${NC}"
echo ""
echo -e "${YELLOW}Next Steps:${NC}"
echo "1. Verify services are running: ./scripts/verify-deployment.sh"
echo "2. Check application: https://app.harkon.co.uk"
echo "3. Review logs if needed: ssh ${REMOTE_USER}@${REMOTE_HOST} 'docker logs <container>'"
echo ""
