#!/bin/bash

# BMAD Enhanced - Stable Publishing Script
# Publishes stable production versions to NPM with latest tag

set -e

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

echo -e "${BLUE}🚀 BMAD Enhanced - Stable Publishing${NC}"
echo "====================================="
echo ""

# Get current version from package.json
CURRENT_VERSION=$(node -p "require('./package.json').version")
echo "📦 Current version: $CURRENT_VERSION"

# Confirm this is a stable release (no pre-release identifiers)
if [[ $CURRENT_VERSION == *"-"* ]]; then
    echo -e "${RED}⚠️  Error: Version contains pre-release identifier: $CURRENT_VERSION${NC}"
    echo "   Stable releases should not contain - (dash) in version"
    echo ""
    echo "   To publish a stable version:"
    echo "   1. Update version with: npm version patch/minor/major"
    echo "   2. Ensure version has no pre-release suffix"
    echo "   3. Run this script again"
    exit 1
fi

# Confirm with user
echo -e "${YELLOW}⚠️  Warning: You are about to publish a STABLE version${NC}"
echo "   This will be the default version users get with:"
echo "   - npm install @cloudkinetix/bmad-enhanced"
echo "   - npx @cloudkinetix/bmad-enhanced install"
echo ""
read -p "Are you sure you want to publish $CURRENT_VERSION as stable? (y/N): " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
    echo "❌ Cancelled by user"
    exit 1
fi

# Check beta was tested
echo ""
echo -e "${YELLOW}Pre-flight checklist:${NC}"
read -p "✓ Has a beta version been tested? (y/N): " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
    echo "❌ Please test a beta version first with: npm run publish:beta"
    exit 1
fi

read -p "✓ Have all tests passed? (y/N): " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
    echo "❌ Please ensure all tests pass before publishing stable"
    exit 1
fi

read -p "✓ Is the CHANGELOG updated? (y/N): " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
    echo "❌ Please update CHANGELOG.md before publishing stable"
    exit 1
fi

# First run pre-publish validation
echo ""
echo -e "${YELLOW}📋 Running pre-publish validation...${NC}"
if ! bash ck-layer/scripts/03-pre-publish-validation.sh; then
    echo -e "${RED}❌ Pre-publish validation failed!${NC}"
    echo "Please fix the issues before publishing."
    exit 1
fi

echo -e "${GREEN}✅ Pre-publish validation passed!${NC}"
echo ""

# Load environment variables from .env
if [ -f .env ]; then
    export $(grep -v '^#' .env | xargs)
    echo "✅ Loaded environment variables from .env"
else
    echo "❌ Error: .env file not found"
    echo "   Please create .env with NPM_TOKEN=your_token"
    exit 1
fi

# Check if NPM_TOKEN exists
if [ -z "$NPM_TOKEN" ]; then
    echo "❌ Error: NPM_TOKEN not found in .env file"
    echo "   Please add NPM_TOKEN=your_token to .env"
    exit 1
fi

echo "✅ NPM token found in environment"

# Set npm auth token
npm config set //registry.npmjs.org/:_authToken=$NPM_TOKEN

# Publish stable (latest tag)
echo ""
echo "🚀 Publishing stable version to NPM..."
npm publish --tag latest --access public

echo ""
echo -e "${GREEN}✅ Stable version published successfully!${NC}"
echo "📦 Package: @cloudkinetix/bmad-enhanced@$CURRENT_VERSION"
echo "🏷️  Tag: latest"
echo ""
echo "Users can now install with:"
echo "  npm install @cloudkinetix/bmad-enhanced"
echo "  npx @cloudkinetix/bmad-enhanced install"
echo ""
echo -e "${YELLOW}📋 Post-publish checklist:${NC}"
echo "  □ Create GitHub release with tag v$CURRENT_VERSION"
echo "  □ Update documentation if needed"
echo "  □ Announce release in appropriate channels"
echo "  □ Monitor for any user-reported issues"