#!/bin/bash
set -e

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

echo -e "${GREEN}🚀 SamCloud Music App - Production Deployment${NC}"
echo -e "${YELLOW}This will deploy both frontend and backend to your live server${NC}\n"

# Check for required commands
for cmd in docker docker-compose; do
    if ! command -v $cmd &> /dev/null; then
        echo -e "${RED}✗ $cmd is not installed. Please install it and try again.${NC}"
        exit 1
    fi
done

# Load production environment
if [ -f ".env.production" ]; then
    echo -e "${GREEN}✓ Loading production environment from .env.production${NC}"
    export $(grep -v '^#' .env.production | xargs)
else
    echo -e "${RED}✗ .env.production not found. Please create it first.${NC}"
    exit 1
fi

# Stop any existing containers
echo -e "\n${YELLOW}🛑 Stopping existing containers...${NC}"
docker-compose -f docker-compose.prod.yml down --remove-orphans || true

# Build and start production services
echo -e "\n${GREEN}🔨 Building and starting production services...${NC}"
docker-compose -f docker-compose.prod.yml up -d --build

# Wait for services to be ready
echo -e "\n${YELLOW}⏳ Waiting for services to start...${NC}"
sleep 10

# Check container status
echo -e "\n${GREEN}📊 Container status:${NC}"
docker-compose -f docker-compose.prod.yml ps

# Test backend health
echo -e "\n${GREEN}🔍 Testing backend health...${NC}"
if curl -f http://localhost:5140/status &>/dev/null; then
    echo -e "${GREEN}✓ Backend is healthy${NC}"
else
    echo -e "${YELLOW}⚠ Backend health check failed, but continuing...${NC}"
fi

# Test API endpoint
echo -e "\n${GREEN}🔍 Testing API endpoint...${NC}"
if curl -f http://localhost/api/discover &>/dev/null; then
    echo -e "${GREEN}✓ API endpoint is accessible${NC}"
else
    echo -e "${YELLOW}⚠ API endpoint test failed, but continuing...${NC}"
fi

# Show logs for a few seconds
echo -e "\n${GREEN}📋 Recent logs:${NC}"
docker-compose -f docker-compose.prod.yml logs --tail=10

echo -e "\n${GREEN}✅ Production deployment complete!${NC}"
echo -e "\nYour application should now be running at:"
echo -e "  ${GREEN}Frontend: http://localhost (or your domain)${NC}"
echo -e "  ${GREEN}Backend API: http://localhost/api${NC}"
echo -e "  ${GREEN}WebSocket: http://localhost/socket.io${NC}"

echo -e "\n${YELLOW}Next steps:${NC}"
echo -e "1. Configure your domain (music.samcloud.ca) to point to this server"
echo -e "2. Set up SSL certificates for HTTPS (optional but recommended)"
echo -e "3. Configure firewall to allow ports 80 and 443"

echo -e "\n${YELLOW}Useful production commands:${NC}"
echo -e "  ${YELLOW}docker-compose -f docker-compose.prod.yml logs -f${NC}     - View logs"
echo -e "  ${YELLOW}docker-compose -f docker-compose.prod.yml down${NC}        - Stop all services"
echo -e "  ${YELLOW}docker-compose -f docker-compose.prod.yml restart${NC}     - Restart services"
echo -e "  ${YELLOW}docker-compose -f docker-compose.prod.yml ps${NC}          - Check status"

echo -e "\n${GREEN}🎵 Your SamCloud Music App with Discover feature is now live!${NC}\n"

exit 0
