#!/bin/bash

# View Build Details Script
# Shows detailed information about a specific build

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'
CYAN='\033[0;36m'
NC='\033[0m' # No Color

echo -e "${BLUE}=== Build Details Viewer ===${NC}"
echo ""

# Check if build tag is provided
if [ -z "$1" ]; then
    echo -e "${YELLOW}Usage: $0 <build-tag>${NC}"
    echo ""
    echo -e "${YELLOW}Available builds:${NC}"
    ./list-builds.sh | grep -E "^\[[0-9]+\]" | head -10
    echo ""
    echo -e "${YELLOW}Example: $0 build-v1-20260112-103710${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 build information
COMMIT=$(git rev-parse --short "$BUILD_TAG^{commit}")
FULL_COMMIT=$(git rev-parse "$BUILD_TAG^{commit}")
DATE=$(git log -1 --format="%ai" "$BUILD_TAG^{commit}")
AUTHOR=$(git log -1 --format="%an <%ae>" "$BUILD_TAG^{commit}")
MESSAGE=$(git log -1 --format="%s" "$BUILD_TAG^{commit}")
TAG_MESSAGE=$(git tag -l --format="%(contents)" "$BUILD_TAG" 2>/dev/null || echo "N/A")

# Get files changed
FILES_CHANGED=$(git diff-tree --no-commit-id --name-only -r "$BUILD_TAG^{commit}" | wc -l | tr -d ' ')

# Get commit stats
STATS=$(git show --stat "$BUILD_TAG^{commit}" | tail -1)

echo -e "${CYAN}Build Tag:${NC} ${BUILD_TAG}"
echo -e "${CYAN}Commit Hash:${NC} ${COMMIT} (${FULL_COMMIT})"
echo -e "${CYAN}Build Date:${NC} ${DATE}"
echo -e "${CYAN}Author:${NC} ${AUTHOR}"
echo -e "${CYAN}Commit Message:${NC} ${MESSAGE}"
if [ "$TAG_MESSAGE" != "N/A" ] && [ -n "$TAG_MESSAGE" ]; then
    echo -e "${CYAN}Tag Message:${NC} ${TAG_MESSAGE}"
fi
echo ""
echo -e "${CYAN}Changes:${NC}"
echo "${STATS}"
echo -e "${CYAN}Files Changed:${NC} ${FILES_CHANGED}"
echo ""

# Show what changed in this build
echo -e "${BLUE}=== Files Changed in This Build ===${NC}"
git diff-tree --no-commit-id --name-status -r "$BUILD_TAG^{commit}" | head -20
if [ "$FILES_CHANGED" -gt 20 ]; then
    echo -e "${YELLOW}... and $((FILES_CHANGED - 20)) more files${NC}"
fi
echo ""

# Show recent commits before this build
echo -e "${BLUE}=== Recent Commits Before This Build ===${NC}"
git log --oneline --decorate -5 "$BUILD_TAG^{commit}" | head -5
echo ""

# Show what's different from current version
CURRENT_COMMIT=$(git rev-parse --short HEAD)
if [ "$COMMIT" != "$CURRENT_COMMIT" ]; then
    echo -e "${BLUE}=== Differences from Current Version ===${NC}"
    echo -e "${YELLOW}Current commit: ${CURRENT_COMMIT}${NC}"
    echo -e "${YELLOW}Build commit: ${COMMIT}${NC}"
    echo ""
    COMMITS_AHEAD=$(git rev-list --count "$BUILD_TAG^{commit}..HEAD" 2>/dev/null || echo "0")
    COMMITS_BEHIND=$(git rev-list --count "HEAD..$BUILD_TAG^{commit}" 2>/dev/null || echo "0")
    
    if [ "$COMMITS_AHEAD" -gt 0 ]; then
        echo -e "${GREEN}Current version is ${COMMITS_AHEAD} commit(s) ahead${NC}"
    fi
    if [ "$COMMITS_BEHIND" -gt 0 ]; then
        echo -e "${YELLOW}Current version is ${COMMITS_BEHIND} commit(s) behind${NC}"
    fi
    if [ "$COMMITS_AHEAD" -eq 0 ] && [ "$COMMITS_BEHIND" -eq 0 ]; then
        echo -e "${GREEN}You are currently on this build!${NC}"
    fi
    echo ""
fi

# Restore option
echo -e "${BLUE}=== Restore Options ===${NC}"
echo ""
echo -e "${GREEN}To restore to this build, run:${NC}"
echo -e "${CYAN}  ./restore-build.sh ${BUILD_TAG}${NC}"
echo ""
echo -e "${YELLOW}This will:${NC}"
echo "  - Checkout the code from this build"
echo "  - Rebuild backend and frontend"
echo "  - Restart services (if using PM2)"
echo ""
