#!/bin/bash
# ============================================
# BreakPilot Code Promotion
# ============================================
# Promotes code between environments via Git branches
#
# Usage: ./scripts/promote.sh [dev-to-staging|staging-to-prod]
#
# Branch Structure:
#   develop  -> Daily development work
#   staging  -> Tested and approved code
#   main     -> Production-ready releases
# ============================================

set -e

ACTION=$1
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ROOT_DIR="$(dirname "$SCRIPT_DIR")"

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

cd "$ROOT_DIR"

# Check if git repo
if [ ! -d ".git" ]; then
    echo -e "${RED}Error: Not a git repository.${NC}"
    echo "Run: git init"
    exit 1
fi

case $ACTION in
    dev-to-staging)
        echo -e "${BLUE}========================================${NC}"
        echo -e "${BLUE}  Promoting: develop -> staging${NC}"
        echo -e "${BLUE}========================================${NC}"
        echo ""

        # Ensure working directory is clean
        if ! git diff-index --quiet HEAD -- 2>/dev/null; then
            echo -e "${YELLOW}Warning: You have uncommitted changes.${NC}"
            read -p "Stash changes and continue? (yes/no): " STASH
            if [ "$STASH" = "yes" ]; then
                git stash
                STASHED=true
            else
                echo "Aborted. Commit or stash your changes first."
                exit 1
            fi
        fi

        # Update develop
        echo -e "${YELLOW}Updating develop branch...${NC}"
        git checkout develop
        git pull origin develop 2>/dev/null || true

        # Merge to staging
        echo -e "${YELLOW}Merging into staging...${NC}"
        git checkout staging
        git pull origin staging 2>/dev/null || true
        git merge develop -m "Promote: develop -> staging ($(date +%Y-%m-%d_%H:%M))"

        # Return to develop
        git checkout develop

        # Restore stash if applicable
        if [ "$STASHED" = "true" ]; then
            git stash pop
        fi

        echo ""
        echo -e "${GREEN}✓ Merged develop into staging${NC}"
        echo ""
        echo "Next steps:"
        echo "  1. Review changes on staging branch"
        echo "  2. Test staging environment: ./scripts/start.sh staging"
        echo "  3. Push when ready: git push origin staging"
        ;;

    staging-to-prod)
        echo -e "${RED}========================================${NC}"
        echo -e "${RED}  WARNING: Promoting to PRODUCTION${NC}"
        echo -e "${RED}========================================${NC}"
        echo ""

        read -p "Have tests passed on staging? (yes/no): " TESTED
        if [ "$TESTED" != "yes" ]; then
            echo "Please test on staging first."
            exit 0
        fi

        read -p "Are you sure you want to promote to production? (yes/no): " CONFIRM
        if [ "$CONFIRM" != "yes" ]; then
            echo "Aborted."
            exit 0
        fi

        # Update staging
        echo -e "${YELLOW}Updating staging branch...${NC}"
        git checkout staging
        git pull origin staging 2>/dev/null || true

        # Merge to main
        echo -e "${YELLOW}Merging into main (production)...${NC}"
        git checkout main
        git pull origin main 2>/dev/null || true
        git merge staging -m "Release: staging -> main ($(date +%Y-%m-%d_%H:%M))"

        # Return to develop
        git checkout develop

        echo ""
        echo -e "${GREEN}✓ Merged staging into main (production)${NC}"
        echo ""
        echo "Next steps:"
        echo "  1. Review changes on main branch"
        echo "  2. Push when ready: git push origin main"
        echo "  3. Create a release tag: git tag -a v1.x.x -m 'Release v1.x.x'"
        ;;

    status)
        echo -e "${BLUE}========================================${NC}"
        echo -e "${BLUE}  Branch Status${NC}"
        echo -e "${BLUE}========================================${NC}"
        echo ""

        CURRENT=$(git branch --show-current)
        echo -e "Current branch: ${GREEN}$CURRENT${NC}"
        echo ""
        echo "Branches:"
        git branch -v
        echo ""
        echo "Recent commits:"
        git log --oneline -5
        ;;

    *)
        echo "Usage: $0 [dev-to-staging|staging-to-prod|status]"
        echo ""
        echo "Commands:"
        echo "  dev-to-staging   - Merge develop into staging"
        echo "  staging-to-prod  - Merge staging into main (production)"
        echo "  status           - Show branch status"
        echo ""
        echo "Branch workflow:"
        echo "  develop (daily work) -> staging (tested) -> main (production)"
        exit 1
        ;;
esac
