#!/usr/bin/env bash
set -euo pipefail

# StreamWeave Release Script
# This script creates a git tag which triggers the GitHub Actions publish workflow

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

cd "$PROJECT_DIR"

echo "🚀 StreamWeave Release Script"
echo "=============================="
echo ""

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

# Check if we're in a git repository
if ! git rev-parse --git-dir > /dev/null 2>&1; then
    echo -e "${RED}❌ Error: Not in a git repository${NC}"
    exit 1
fi

# Check if there are uncommitted changes
if ! git diff-index --quiet HEAD --; then
    echo -e "${RED}❌ Error: You have uncommitted changes.${NC}"
    echo "   Please commit or stash them first."
    git status --short
    exit 1
fi

# Check if we're on main branch
current_branch=$(git branch --show-current)
if [ "$current_branch" != "main" ]; then
    echo -e "${YELLOW}⚠️  Warning: You're not on the main branch.${NC}"
    echo "   Current branch: $current_branch"
    read -p "   Continue anyway? (y/N): " -n 1 -r
    echo
    if [[ ! $REPLY =~ ^[Yy]$ ]]; then
        echo -e "${RED}❌ Aborted.${NC}"
        exit 1
    fi
fi

# Get version from Cargo.toml
version=$(grep '^version = ' Cargo.toml | head -1 | cut -d '"' -f 2)
echo -e "${BLUE}📦 Version from Cargo.toml:${NC} $version"

# Check if version tag already exists
if git tag -l | grep -q "^v$version$"; then
    echo -e "${RED}❌ Error: Version tag v$version already exists.${NC}"
    echo "   Either:"
    echo "   1. Update the version in Cargo.toml"
    echo "   2. Delete the existing tag: git tag -d v$version && git push origin :refs/tags/v$version"
    exit 1
fi

# Run pre-publish checks
echo ""
echo -e "${BLUE}🔍 Running pre-publish checks...${NC}"

echo "  → cargo check..."
cargo check --all-targets --all-features --quiet

echo "  → cargo test..."
cargo test --all-targets --all-features --quiet

echo "  → cargo fmt --check..."
cargo fmt --all -- --check

echo "  → cargo clippy..."
cargo clippy --all-targets --all-features --quiet -- -D warnings

echo "  → cargo doc..."
RUSTDOCFLAGS='-D warnings' cargo doc --all-features --no-deps --quiet

echo "  → cargo publish --dry-run..."
cargo publish --dry-run --quiet

echo ""
echo -e "${GREEN}✅ All checks passed!${NC}"

# Summary
echo ""
echo -e "${BLUE}📋 Release Summary:${NC}"
echo "   Version: v$version"
echo "   Branch:  $current_branch"
echo "   Commit:  $(git rev-parse --short HEAD)"
echo ""
echo -e "${YELLOW}This will:${NC}"
echo "   1. Create git tag v$version"
echo "   2. Push the tag to GitHub"
echo "   3. Trigger the GitHub Actions publish workflow"
echo "   4. Publish to crates.io automatically"
echo "   5. Create a GitHub Release"
echo ""

# Ask for confirmation
read -p "🤔 Ready to release v$version? (y/N): " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
    echo -e "${RED}❌ Release cancelled.${NC}"
    exit 1
fi

# Create and push tag
echo ""
echo -e "${BLUE}🏷️  Creating tag v$version...${NC}"
git tag -a "v$version" -m "Release v$version"

echo -e "${BLUE}📤 Pushing tag to GitHub...${NC}"
git push origin "v$version"

echo ""
echo -e "${GREEN}✅ Tag v$version created and pushed!${NC}"
echo ""
echo -e "${BLUE}🔄 GitHub Actions is now handling the release:${NC}"
echo "   → Building and testing"
echo "   → Publishing to crates.io"
echo "   → Creating GitHub Release"
echo "   → Deploying documentation"
echo ""
echo -e "${BLUE}📊 Track progress:${NC}"
echo "   https://github.com/Industrial/streamweave/actions"
echo ""
echo -e "${GREEN}🎉 Release v$version initiated!${NC}"
