#!/bin/bash

# Echoes of the Ash - Release Script
# Generates package-lock.json, commits changes, and creates a new release tag

set -e  # Exit on error

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

echo -e "${BLUE}========================================${NC}"
echo -e "${BLUE}  Echoes of the Ash - Release Script${NC}"
echo -e "${BLUE}========================================${NC}"
echo ""

# Get the project root directory
PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
cd "$PROJECT_ROOT"

# Step 1: Generate package-lock.json using Docker
echo -e "${YELLOW}[1/5]${NC} Generating package-lock.json with Docker..."
docker run --rm -v "$PROJECT_ROOT/pwa:/app" -w /app node:20-alpine npm install

# Optional: Remove node_modules to keep local directory clean
echo -e "${YELLOW}[2/5]${NC} Cleaning up node_modules..."
#rm -rf "$PROJECT_ROOT/pwa/node_modules"

echo -e "${GREEN}✓${NC} package-lock.json generated successfully"
echo ""

# Step 2: Get current version from git tags
echo -e "${YELLOW}[3/5]${NC} Determining version..."
CURRENT_VERSION=$(git describe --tags --abbrev=0 2>/dev/null || echo "v0.0.0")
echo -e "Current version: ${GREEN}$CURRENT_VERSION${NC}"

# Parse version and suggest next version
if [[ $CURRENT_VERSION =~ v([0-9]+)\.([0-9]+)\.([0-9]+) ]]; then
    MAJOR="${BASH_REMATCH[1]}"
    MINOR="${BASH_REMATCH[2]}"
    PATCH="${BASH_REMATCH[3]}"
    NEXT_PATCH=$((PATCH + 1))
    SUGGESTED_VERSION="v${MAJOR}.${MINOR}.${NEXT_PATCH}"
else
    SUGGESTED_VERSION="v0.1.0"
fi

echo -e "Suggested next version: ${BLUE}$SUGGESTED_VERSION${NC}"
echo ""
read -p "Enter version (or press Enter for $SUGGESTED_VERSION): " NEW_VERSION

# Use suggested version if user didn't provide one
if [ -z "$NEW_VERSION" ]; then
    NEW_VERSION="$SUGGESTED_VERSION"
fi

# Ensure version starts with 'v'
if [[ ! $NEW_VERSION =~ ^v ]]; then
    NEW_VERSION="v$NEW_VERSION"
fi

echo -e "Using version: ${GREEN}$NEW_VERSION${NC}"
echo ""

# Step 3: Git add and commit
echo -e "${YELLOW}[4/5]${NC} Committing changes..."
git add -A

# Check if there are changes to commit
if git diff --cached --quiet; then
    echo -e "${YELLOW}No changes to commit${NC}"
else
    git commit -m "Release $NEW_VERSION: Update package-lock.json and CI config"
    echo -e "${GREEN}✓${NC} Changes committed"
fi

# Push to main
echo -e "${YELLOW}Pushing to main branch...${NC}"
git push origin main
echo -e "${GREEN}✓${NC} Pushed to main"
echo ""

# Step 4: Create and push tag
echo -e "${YELLOW}[5/5]${NC} Creating release tag..."

# Delete tag if it already exists (locally and remotely)
if git tag -l | grep -q "^$NEW_VERSION$"; then
    echo -e "${YELLOW}Tag $NEW_VERSION already exists, deleting...${NC}"
    git tag -d "$NEW_VERSION"
    git push origin ":refs/tags/$NEW_VERSION" 2>/dev/null || true
fi

# Create new tag
git tag "$NEW_VERSION"
echo -e "${GREEN}✓${NC} Tag $NEW_VERSION created"

# Push tag
git push origin "$NEW_VERSION"
echo -e "${GREEN}✓${NC} Tag pushed to remote"
echo ""

# Step 5: Summary
echo -e "${BLUE}========================================${NC}"
echo -e "${GREEN}✓ Release $NEW_VERSION completed!${NC}"
echo -e "${BLUE}========================================${NC}"
echo ""
echo -e "Pipeline URL:"
echo -e "${BLUE}http://gitlab.kingstudio.es/patacuack/echoes-of-the-ash/-/pipelines${NC}"
echo ""
echo -e "The CI/CD pipeline will now build:"
echo -e "  • Linux AppImage and .deb (compressed)"
echo -e "  • Windows .exe installer (compressed)"
echo ""
echo -e "Build time: ~10-20 minutes"
echo ""
