#!/bin/bash

# -------------------------------
# Script: deploy_nest_aws.sh
# Purpose: Setup NestJS + MySQL + Redis on Ubuntu EC2
# -------------------------------

# Update system
echo "Updating system..."
sudo apt update && sudo apt upgrade -y

# Install Docker
echo "Installing Docker..."
sudo apt install -y docker.io
sudo systemctl enable docker
sudo systemctl start docker

# Add current user to docker group
sudo usermod -aG docker $USER
newgrp docker

# Install Docker Compose
echo "Installing Docker Compose..."
sudo apt install -y docker-compose

# Verify installation
docker --version
docker-compose --version

# Clone project repo
echo "Cloning project..."
read -p "Enter your Git repo URL: " REPO_URL
git clone $REPO_URL
PROJECT_DIR=$(basename "$REPO_URL" .git)
cd $PROJECT_DIR || exit

# Make sure docker-compose.yml exists
if [ ! -f docker-compose.yml ]; then
    echo "docker-compose.yml not found! Please add it to the project."
    exit 1
fi

# Build and start containers
echo "Starting Docker containers..."
docker-compose up --build -d

# Show container status
docker ps

echo "Deployment completed! Your NestJS app should be accessible on EC2 public IP."
