#!/bin/bash
# =============================================
# restore-myapps.sh — Complete Restore Script
# =============================================

set -uo pipefail

# --------------------------------------------------
# Parse arguments
# --------------------------------------------------
REMOTE_MODE=false
REMOTE_IP=""
REMOTE_USER="root"
SSH_KEY=""
USE_PASSWORD=false
SSH_PASS=""

while [[ $# -gt 0 ]]; do
    case "$1" in
        --remote)
            REMOTE_MODE=true
            REMOTE_IP="$2"
            REMOTE_USER="${3:-root}"
            shift 3
            ;;
        --key)
            SSH_KEY="$2"
            shift 2
            ;;
        --password)
            USE_PASSWORD=true
            shift
            ;;
        *)
            shift
            ;;
    esac
done

# --------------------------------------------------
# If remote mode
# --------------------------------------------------
if [ "$REMOTE_MODE" = true ]; then
    if [ -z "$REMOTE_IP" ]; then
        echo "❌ --remote requires an IP address."
        exit 1
    fi

    SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
    REMOTE_DEST="/tmp/restore-session-$(date +%s)"

    SSH_OPTS="-o StrictHostKeyChecking=no -o ConnectTimeout=15"
    if [ -n "$SSH_KEY" ]; then
        SSH_OPTS="$SSH_OPTS -i $SSH_KEY"
    fi

    if [ "$USE_PASSWORD" = true ]; then
        if ! command -v sshpass &>/dev/null; then
            apt install sshpass -y
        fi
        read -s -p "SSH password for ${REMOTE_USER}@${REMOTE_IP}: " SSH_PASS
        echo ""
        SSH_CMD="sshpass -p '$SSH_PASS' ssh $SSH_OPTS ${REMOTE_USER}@${REMOTE_IP}"
        SCP_CMD="sshpass -p '$SSH_PASS' scp $SSH_OPTS"
    else
        SSH_CMD="ssh $SSH_OPTS ${REMOTE_USER}@${REMOTE_IP}"
        SCP_CMD="scp $SSH_OPTS"
    fi

    echo "========================================="
    echo "📡 REMOTE RESTORE to ${REMOTE_USER}@${REMOTE_IP}"
    echo "========================================="

    $SSH_CMD "mkdir -p $REMOTE_DEST"
    $SCP_CMD -r "$SCRIPT_DIR/." "${REMOTE_USER}@${REMOTE_IP}:${REMOTE_DEST}/"
    $SSH_CMD "chmod +x $REMOTE_DEST/restore-myapps.sh && cd $REMOTE_DEST && bash restore-myapps.sh"
    $SSH_CMD "rm -rf $REMOTE_DEST"

    echo "✅ Remote restore complete"
    exit 0
fi

# ===================================================
# LOCAL RESTORE
# ===================================================

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"

VM_IP=$(ip -4 addr show | grep -oP '(?<=inet\s)\d+(\.\d+){3}' | grep -v 127.0.0.1 | head -1)

echo "========================================="
echo "🔄 FULL RESTORE — LOCAL MODE"
echo "   Machine IP: $VM_IP"
echo "   Backup dir: $SCRIPT_DIR"
echo "========================================="

# --------------------------------------------------
# STEP 1: Restore Volumes
# --------------------------------------------------
echo ""
echo "📦 STEP 1 — Restoring Volumes"
echo "========================================="

if [ -d "$SCRIPT_DIR/volumes" ]; then
    cd "$SCRIPT_DIR/volumes"
    for backup in *.tar.gz; do
        [ -f "$backup" ] || continue
        volume=$(basename "$backup" .tar.gz)
        echo -n "   📁 Restoring $volume ... "
        docker volume create "$volume" &>/dev/null || true
        docker run --rm \
            -v "${volume}:/target" \
            -v "$(pwd):/backup" \
            alpine \
            sh -c "cd /target && tar xzf /backup/$backup" \
        && echo "✅" || echo "⚠️ FAILED"
    done
    cd "$SCRIPT_DIR"
fi

# --------------------------------------------------
# STEP 2: Start Containers
# --------------------------------------------------
echo ""
echo "🚀 STEP 2 — Starting Containers"
echo "========================================="

declare -A APP_DIRS=(
    ["Frappe"]="frappe-setup"
    ["Odoo"]="odoo-clean"
    ["Nextcloud"]="nextcloud-setup"
    ["Mautic"]="mautic-setup"
    ["n8n"]="n8n-setup"
)

if [ -d "$SCRIPT_DIR/compose-files" ]; then
    cd "$SCRIPT_DIR/compose-files"
    for app in Frappe Odoo Nextcloud Mautic n8n; do
        dir="${APP_DIRS[$app]}"
        if [ -d "$dir" ]; then
            echo "   🚀 Starting $app..."
            cd "$dir"
            docker-compose up -d 2>&1 | tail -3
            cd ..
        fi
    done
    cd "$SCRIPT_DIR"
fi

echo ""
echo "⏳ Waiting 60s for containers to initialize..."
sleep 60

# --------------------------------------------------
# STEP 3: Post-Restore Fixes
# --------------------------------------------------
echo ""
echo "🔧 STEP 3 — Applying Post-Restore Fixes"
echo "========================================="

# ---- NEXTCLOUD ----
echo ""
echo "📌 Nextcloud — Trusted domains..."
if docker ps | grep -q nextcloud-app; then
    docker exec nextcloud-app php /var/www/html/occ config:system:delete trusted_domains 1 2>/dev/null || true
    docker exec nextcloud-app php /var/www/html/occ config:system:delete trusted_domains 2 2>/dev/null || true
    docker exec nextcloud-app php /var/www/html/occ config:system:set trusted_domains 0 --value="localhost"
    docker exec nextcloud-app php /var/www/html/occ config:system:set trusted_domains 1 --value="$VM_IP"
    docker exec nextcloud-app php /var/www/html/occ config:system:set trusted_domains 2 --value="${VM_IP}:8082"
    docker exec nextcloud-app php /var/www/html/occ config:system:set trusted_domains 3 --value="localhost:8082"
    docker restart nextcloud-app
    echo "   ✅ Nextcloud fixed"
fi

# ---- MAUTIC ----
echo ""
echo "📌 Mautic — Config + admin password..."
if docker ps | grep -q mautic-app; then
    # Create config file
    cat > /tmp/local.php << EOF
<?php
\$parameters = array(
    'db_driver' => 'pdo_mysql',
    'db_host' => 'mautic-mariadb',
    'db_port' => '3306',
    'db_name' => 'mautic',
    'db_user' => 'mautic',
    'db_password' => 'mautic123',
    'site_url' => 'http://${VM_IP}:8081'
);
EOF
    docker cp /tmp/local.php mautic-app:/var/www/html/config/local.php 2>/dev/null
    docker exec mautic-app touch /var/www/html/var/.installed 2>/dev/null
    docker exec mautic-app chown -R www-data:www-data /var/www/html/var 2>/dev/null
    docker exec mautic-app chown -R www-data:www-data /var/www/html/config 2>/dev/null
    docker restart mautic-app
    sleep 10
    HASH=$(docker exec mautic-app php -r "echo password_hash('Admin!Password123', PASSWORD_BCRYPT);" 2>/dev/null)
    if [ ! -z "$HASH" ]; then
        docker exec mautic-mariadb mysql -uroot -pmautic_root_password -e "USE mautic; UPDATE users SET password = '$HASH' WHERE username = 'admin';" 2>/dev/null
        echo "   ✅ Admin password reset to: Admin!Password123"
    fi
    docker restart mautic-app
    echo "   ✅ Mautic fixed → http://${VM_IP}:8081 (admin/Admin!Password123)"
else
    echo "   ⚠️ mautic-app not running"
fi

# ---- ODOO ----
echo ""
echo "📌 Odoo — Assets + DB user..."
if docker ps | grep -q odoo-clean-db-1; then
    docker exec odoo-clean-db-1 psql -U odoo -d odoo -c "DELETE FROM ir_attachment WHERE url LIKE '/web/assets/%';" 2>/dev/null
    docker exec odoo-clean-db-1 psql -U odoo -c "ALTER USER odoo WITH PASSWORD 'odoo';" 2>/dev/null
    docker exec odoo-clean-odoo-1 bash -c "grep -q 'filestore_check_missing' /etc/odoo/odoo.conf || echo 'filestore_check_missing = False' >> /etc/odoo/odoo.conf" 2>/dev/null
    docker restart odoo-clean-odoo-1
    echo "   ✅ Odoo fixed → http://${VM_IP}:8069/web"
fi

# ---- FRAPPE ----
echo ""
echo "📌 Frappe — Full fix..."
if docker ps | grep -q frappe-erpnext && docker ps | grep -q frappe-mariadb; then
    DB_NAME=$(docker exec frappe-erpnext cat /home/frappe/frappe-bench/sites/erpnext.navitrends.ovh/site_config.json 2>/dev/null | grep -o '"db_name": *"[^"]*"' | cut -d'"' -f4)
    DB_PASS=$(docker exec frappe-erpnext cat /home/frappe/frappe-bench/sites/erpnext.navitrends.ovh/site_config.json 2>/dev/null | grep -o '"db_password": *"[^"]*"' | cut -d'"' -f4)
    
    if [ -n "$DB_NAME" ] && [ -n "$DB_PASS" ]; then
        echo "   DB: $DB_NAME"
        docker exec frappe-mariadb mysql -uroot -p123 -e "
            GRANT ALL PRIVILEGES ON *.* TO '${DB_NAME}'@'%' IDENTIFIED BY '${DB_PASS}' WITH GRANT OPTION;
            GRANT ALL PRIVILEGES ON *.* TO '${DB_NAME}'@'172.%' IDENTIFIED BY '${DB_PASS}' WITH GRANT OPTION;
            GRANT ALL PRIVILEGES ON *.* TO '${DB_NAME}'@'localhost' IDENTIFIED BY '${DB_PASS}' WITH GRANT OPTION;
            FLUSH PRIVILEGES;
        " 2>/dev/null && echo "   ✅ DB permissions fixed"
        
        docker exec frappe-erpnext bash -c "
            cd /home/frappe/frappe-bench
            if [ ! -d 'apps/hrms' ]; then
                bench get-app --branch version-15 https://github.com/frappe/hrms
            fi
            bench --site erpnext.navitrends.ovh install-app hrms
        " 2>/dev/null && echo "   ✅ HRMS installed"
        
        docker exec frappe-erpnext bash -c "
            cd /home/frappe/frappe-bench
            bench --site erpnext.navitrends.ovh set-config redis_cache 'redis://frappe-redis:6379'
            bench --site erpnext.navitrends.ovh set-config redis_queue 'redis://frappe-redis:6379'
            bench --site erpnext.navitrends.ovh set-config enable_scheduler 1
            bench --site erpnext.navitrends.ovh set-config site_url 'http://${VM_IP}:8080'
            bench --site erpnext.navitrends.ovh migrate
            bench --site erpnext.navitrends.ovh clear-cache
            bench use erpnext.navitrends.ovh
        " 2>/dev/null && echo "   ✅ Frappe configured"
        
        # Start bench serve in background inside container
        docker exec -d frappe-erpnext bash -c "cd /home/frappe/frappe-bench && nohup bench serve --port 8000 > /tmp/bench.log 2>&1 &"
        docker restart frappe-erpnext
        echo "   ✅ Frappe fixed → http://${VM_IP}:8080"
    else
        echo "   ⚠️ Could not get Frappe credentials"
    fi
fi

# ---- N8N ----
echo ""
echo "📌 n8n — Network check..."
docker network inspect integration-network &>/dev/null || docker network create integration-network
echo "   ✅ n8n → http://${VM_IP}:5678"

# --------------------------------------------------
# Summary
# --------------------------------------------------
echo ""
echo "========================================="
echo "✅ RESTORE COMPLETE"
echo "========================================="
echo "  Nextcloud → http://${VM_IP}:8082"
echo "  Mautic    → http://${VM_IP}:8081 (admin/Admin!Password123)"
echo "  Odoo      → http://${VM_IP}:8069/web"
echo "  n8n       → http://${VM_IP}:5678"
echo "  Frappe    → http://${VM_IP}:8080"
echo "========================================="
