#!/bin/bash
# AutonetSellCar Remote Deployment Script
# Use this script from the development server (server4) to manage deployments
#
# Usage:
#   ./deploy-remote.sh push      - Push to staging
#   ./deploy-remote.sh promote   - Promote staging to production
#   ./deploy-remote.sh rollback  - Rollback production
#   ./deploy-remote.sh status    - Check deployment status

SERVER="192.168.0.202"
USER="${DEPLOY_USER:-$(whoami)}"  # Override with DEPLOY_USER env var if needed

case "$1" in
    push)
        echo "Pushing to staging server..."
        git push staging main
        ;;

    promote)
        echo "Promoting staging to production..."
        ssh "$USER@$SERVER" "/opt/autonet/scripts/deploy.sh promote"
        ;;

    rollback)
        echo "Rolling back production..."
        ssh "$USER@$SERVER" "/opt/autonet/scripts/deploy.sh rollback"
        ;;

    rollback-to)
        if [ -z "$2" ]; then
            echo "Usage: ./deploy-remote.sh rollback-to <release_timestamp>"
            ssh "$USER@$SERVER" "ls -1 /opt/autonet/releases"
            exit 1
        fi
        echo "Rolling back to $2..."
        ssh "$USER@$SERVER" "/opt/autonet/scripts/deploy.sh rollback-to $2"
        ;;

    status)
        ssh "$USER@$SERVER" "/opt/autonet/scripts/deploy.sh status"
        ;;

    logs-staging)
        ssh "$USER@$SERVER" "docker logs -f autonet-backend-staging --tail=100"
        ;;

    logs-prod)
        ssh "$USER@$SERVER" "docker logs -f autonet-backend --tail=100"
        ;;

    cleanup)
        echo "Cleaning up old releases..."
        ssh "$USER@$SERVER" "/opt/autonet/scripts/deploy.sh cleanup"
        ;;

    setup)
        echo "Copying setup script to server2..."
        scp deploy/setup-server2.sh "$USER@$SERVER:/tmp/"
        echo "Running setup script..."
        ssh "$USER@$SERVER" "chmod +x /tmp/setup-server2.sh && /tmp/setup-server2.sh"
        ;;

    *)
        echo "AutonetSellCar Remote Deployment"
        echo ""
        echo "Usage: ./deploy-remote.sh <command>"
        echo ""
        echo "Commands:"
        echo "  setup                Setup server2 infrastructure (run once)"
        echo "  push                 Push code to staging (auto-deploys)"
        echo "  promote              Promote staging to production"
        echo "  rollback             Rollback to previous release"
        echo "  rollback-to <ts>     Rollback to specific release"
        echo "  status               Show deployment status"
        echo "  logs-staging         Show staging backend logs"
        echo "  logs-prod            Show production backend logs"
        echo "  cleanup              Remove old releases"
        echo ""
        echo "Quick Start:"
        echo "  1. ./deploy-remote.sh setup    # One-time server setup"
        echo "  2. ./deploy-remote.sh push     # Deploy to staging"
        echo "  3. Test at http://192.168.0.202:3001"
        echo "  4. ./deploy-remote.sh promote  # Deploy to production"
        echo ""
        ;;
esac
