#!/bin/bash

# AtlasOS - Calypso Rebuild and Restart Script
# This script rebuilds both backend and frontend, then restarts the services

set -e  # Exit on error

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

# Configuration
PROJECT_ROOT="/src/calypso"
BACKEND_DIR="${PROJECT_ROOT}/backend"
FRONTEND_DIR="${PROJECT_ROOT}/frontend"
INSTALL_DIR="/opt/calypso"
SERVICE_NAME="calypso-api"

echo -e "${GREEN}========================================${NC}"
echo -e "${GREEN}AtlasOS - Calypso Rebuild & Restart${NC}"
echo -e "${GREEN}========================================${NC}"
echo ""

# Check if running as root
if [ "$EUID" -ne 0 ]; then 
    echo -e "${YELLOW}Warning: This script requires sudo privileges for some operations${NC}"
    echo -e "${YELLOW}Some commands will be run with sudo${NC}"
    echo ""
fi

# Step 1: Rebuild Backend
echo -e "${GREEN}[1/4] Rebuilding Backend...${NC}"
cd "${BACKEND_DIR}"
echo "Building Go binary..."
if go build -o "${INSTALL_DIR}/bin/calypso-api" ./cmd/calypso-api; then
    echo -e "${GREEN}✓ Backend build successful${NC}"
else
    echo -e "${RED}✗ Backend build failed${NC}"
    exit 1
fi

# Set permissions for backend binary
echo "Setting permissions..."
sudo chmod +x "${INSTALL_DIR}/bin/calypso-api"
sudo chown calypso:calypso "${INSTALL_DIR}/bin/calypso-api"
echo -e "${GREEN}✓ Backend binary ready${NC}"
echo ""

# Step 2: Rebuild Frontend
echo -e "${GREEN}[2/4] Rebuilding Frontend...${NC}"
cd "${FRONTEND_DIR}"
echo "Installing dependencies (if needed)..."
npm install --silent 2>&1 | grep -E "(added|removed|changed|up to date)" || true

echo "Building frontend..."
if npm run build; then
    echo -e "${GREEN}✓ Frontend build successful${NC}"
else
    echo -e "${RED}✗ Frontend build failed${NC}"
    exit 1
fi
echo ""

# Step 3: Deploy Frontend
echo -e "${GREEN}[3/4] Deploying Frontend...${NC}"
echo "Copying frontend files to ${INSTALL_DIR}/web/..."
sudo rm -rf "${INSTALL_DIR}/web/"*
sudo cp -r "${FRONTEND_DIR}/dist/"* "${INSTALL_DIR}/web/"
sudo chown -R www-data:www-data "${INSTALL_DIR}/web"
echo -e "${GREEN}✓ Frontend deployed${NC}"
echo ""

# Step 4: Restart Services
echo -e "${GREEN}[4/4] Restarting Services...${NC}"

# Restart Calypso API service
echo "Restarting ${SERVICE_NAME} service..."
if sudo systemctl restart "${SERVICE_NAME}.service"; then
    echo -e "${GREEN}✓ ${SERVICE_NAME} service restarted${NC}"
    
    # Wait a moment for service to start
    sleep 2
    
    # Check service status
    if sudo systemctl is-active --quiet "${SERVICE_NAME}.service"; then
        echo -e "${GREEN}✓ ${SERVICE_NAME} service is running${NC}"
    else
        echo -e "${YELLOW}⚠ ${SERVICE_NAME} service may not be running properly${NC}"
        echo "Check status with: sudo systemctl status ${SERVICE_NAME}.service"
    fi
else
    echo -e "${RED}✗ Failed to restart ${SERVICE_NAME} service${NC}"
    exit 1
fi

# Reload Nginx (to ensure frontend is served correctly)
echo "Reloading Nginx..."
if sudo systemctl reload nginx 2>/dev/null || sudo systemctl reload nginx.service 2>/dev/null; then
    echo -e "${GREEN}✓ Nginx reloaded${NC}"
else
    echo -e "${YELLOW}⚠ Nginx reload failed (may not be installed)${NC}"
fi

echo ""

# Summary
echo -e "${GREEN}========================================${NC}"
echo -e "${GREEN}Rebuild and Restart Complete!${NC}"
echo -e "${GREEN}========================================${NC}"
echo ""
echo "Backend binary: ${INSTALL_DIR}/bin/calypso-api"
echo "Frontend files: ${INSTALL_DIR}/web/"
echo ""
echo "Service status:"
sudo systemctl status "${SERVICE_NAME}.service" --no-pager -l | head -10
echo ""
echo -e "${GREEN}All done!${NC}"
