#!/bin/bash

# Restore Build Script
# This script restores the project to a specific build tag

set -e  # Exit on error

PROJECT_DIR="/var/www/html/erp_codearya"
cd "$PROJECT_DIR"

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

echo -e "${BLUE}=== Restore Build Script ===${NC}"

# Check if Git is initialized
if [ ! -d ".git" ]; then
    echo -e "${RED}Error: Git repository not initialized!${NC}"
    echo -e "${YELLOW}Run build-with-backup.sh first to initialize Git.${NC}"
    exit 1
fi

# Check if build tag is provided
if [ -z "$1" ]; then
    echo -e "${YELLOW}Available build tags:${NC}"
    echo ""
    git tag -l "build-*" | sort -V | tail -20
    echo ""
    echo -e "${YELLOW}Usage: $0 <build-tag>${NC}"
    echo -e "${YELLOW}Example: $0 build-v1-20240101-120000${NC}"
    exit 1
fi

BUILD_TAG="$1"

# Verify tag exists
if ! git rev-parse "$BUILD_TAG" >/dev/null 2>&1; then
    echo -e "${RED}Error: Build tag '${BUILD_TAG}' not found!${NC}"
    echo -e "${YELLOW}Available tags:${NC}"
    git tag -l "build-*" | sort -V
    exit 1
fi

# Get current status
CURRENT_COMMIT=$(git rev-parse --short HEAD)
TARGET_COMMIT=$(git rev-parse --short "$BUILD_TAG")

echo -e "${YELLOW}Current commit: ${CURRENT_COMMIT}${NC}"
echo -e "${YELLOW}Target build tag: ${BUILD_TAG}${NC}"
echo -e "${YELLOW}Target commit: ${TARGET_COMMIT}${NC}"
echo ""

# Confirm restore
read -p "Are you sure you want to restore to build ${BUILD_TAG}? (y/N): " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
    echo -e "${YELLOW}Restore cancelled.${NC}"
    exit 0
fi

# Check for uncommitted changes
if [ -n "$(git status --porcelain)" ]; then
    echo -e "${YELLOW}Warning: You have uncommitted changes!${NC}"
    read -p "Do you want to stash them before restoring? (y/N): " -n 1 -r
    echo
    if [[ $REPLY =~ ^[Yy]$ ]]; then
        echo -e "${GREEN}Stashing changes...${NC}"
        git stash push -m "Stashed before restore to $BUILD_TAG"
    else
        echo -e "${RED}Restore cancelled. Please commit or stash your changes first.${NC}"
        exit 1
    fi
fi

# Restore to the build tag
echo -e "${GREEN}Restoring to build ${BUILD_TAG}...${NC}"
git checkout "$BUILD_TAG" || {
    echo -e "${RED}Failed to checkout build tag!${NC}"
    exit 1
}

# Rebuild after restore
echo -e "${GREEN}Rebuilding after restore...${NC}"

# Build Backend
if [ -d "backend" ] && [ -f "backend/Cargo.toml" ]; then
    echo -e "${GREEN}Building backend...${NC}"
    cd backend
    cargo build --release || {
        echo -e "${YELLOW}Backend build failed. You may need to rebuild manually.${NC}"
    }
    cd ..
fi

# Build Frontend
if [ -d "frontend" ] && [ -f "frontend/package.json" ]; then
    echo -e "${GREEN}Building frontend...${NC}"
    cd frontend
    npm install || {
        echo -e "${YELLOW}Frontend dependencies installation failed.${NC}"
    }
    npm run build || {
        echo -e "${YELLOW}Frontend build failed. You may need to rebuild manually.${NC}"
    }
    cd ..
fi

# Restart services (if using PM2)
if command -v pm2 &> /dev/null; then
    echo -e "${GREEN}Restarting services...${NC}"
    pm2 restart erp-backend || true
    pm2 restart erp-frontend || true
fi

echo -e "${GREEN}=== Restore completed successfully! ===${NC}"
echo -e "${GREEN}Project restored to build: ${BUILD_TAG}${NC}"
echo -e "${YELLOW}Current commit: $(git rev-parse --short HEAD)${NC}"
