#!/bin/bash
set -euo pipefail

# Usage: ./release.sh [--dry-run] [patch|minor|major] [release notes]

DRY_RUN=false
if [[ "${1:-}" == "--dry-run" ]]; then
  DRY_RUN=true
  shift
fi

BUMP="${1:-patch}"
NOTES="${2:-}"

# Validate bump type
if [[ ! "$BUMP" =~ ^(patch|minor|major)$ ]]; then
  echo "Error: invalid bump type '$BUMP'. Use: patch, minor, or major"
  exit 1
fi

# Read current version from Cargo.toml
CURRENT_VERSION=$(grep '^version = ' Cargo.toml | head -1 | sed 's/version = "\(.*\)"/\1/')
if [[ -z "$CURRENT_VERSION" ]]; then
  echo "Error: could not read version from Cargo.toml"
  exit 1
fi

# Parse version components
IFS='.' read -r MAJOR MINOR PATCH <<< "$CURRENT_VERSION"

# Bump version
case "$BUMP" in
  patch) PATCH=$((PATCH + 1)) ;;
  minor) MINOR=$((MINOR + 1)); PATCH=0 ;;
  major) MAJOR=$((MAJOR + 1)); MINOR=0; PATCH=0 ;;
esac

NEW_VERSION="${MAJOR}.${MINOR}.${PATCH}"
TAG="v${NEW_VERSION}"

# Check if tag already exists
if git tag -l "$TAG" | grep -q "^${TAG}$"; then
  echo "Error: tag $TAG already exists"
  exit 1
fi

# Check for uncommitted changes
if [[ -n $(git status --porcelain) ]]; then
  echo "Error: uncommitted changes detected. Commit or stash before releasing."
  exit 1
fi

echo "Bumping version: $CURRENT_VERSION -> $NEW_VERSION ($BUMP)"

if $DRY_RUN; then
  echo ""
  echo "Dry run — nothing will be changed:"
  echo "  Would update Cargo.toml: $CURRENT_VERSION -> $NEW_VERSION"
  echo "  Would run: git commit -m \"chore: bump version to $NEW_VERSION\""
  echo "  Would run: git tag $TAG"
  echo "  Would run: git push origin main --tags"
  echo "  Would run: cargo publish --allow-dirty"
  echo "  Would run: cargo build --release"
  echo "  Would run: gh release create $TAG --title \"$TAG\" $([ -n "$NOTES" ] && echo "--notes \"$NOTES\"") muthr-${TAG}-bin-macos-arm64.tar.gz SHA256SUMS"
  echo ""
  echo "Run without --dry-run to execute."
  exit 0
fi

# Update Cargo.toml
sed -i '' "s/version = \"$CURRENT_VERSION\"/version = \"$NEW_VERSION\"/" Cargo.toml

# Commit, tag, push
git add Cargo.toml
git commit -m "chore: bump version to $NEW_VERSION"
git tag -a "$TAG" -m "$TAG"
git push origin main --tags

# Build release binary first (before publishing)
echo "Building release binary..."
cargo build --release

# Package the binary
BINARY="target/release/muthr"
ARCHIVE="muthr-${TAG}-bin-macos-arm64.tar.gz"
CHECKSUM="SHA256SUMS"

echo "Packaging ${BINARY}..."
tar czf "$ARCHIVE" -C target/release muthr
sha256sum "$ARCHIVE" > "$CHECKSUM"

# Publish to crates.io
echo "Publishing to crates.io..."
cargo publish --allow-dirty

# Create GitHub release
echo "Creating GitHub release $TAG..."
if [[ -n "$NOTES" ]]; then
  gh release create "$TAG" \
    --title "$TAG" \
    --notes "$NOTES" \
    "$ARCHIVE" \
    "$CHECKSUM"
else
  gh release create "$TAG" \
    --title "$TAG" \
    "$ARCHIVE" \
    "$CHECKSUM"
fi

# Cleanup
rm -f "$ARCHIVE" "$CHECKSUM"

echo "Done! Release $TAG is live on crates.io and GitHub."
