#!/usr/bin/env bash
#MISE description="Execute complete 5-phase release pipeline: preflight → version → build+push → pypi publish → verify. All local, no GitHub Actions."
set -euo pipefail

echo ""
echo "╔═══════════════════════════════════════════════════════════╗"
echo "║  atr-adaptive-laguerre Full Release Workflow              ║"
echo "╚═══════════════════════════════════════════════════════════╝"
echo ""

# Phase 1: Preflight
mise run release:preflight

# Capture version BEFORE semantic-release
VERSION_BEFORE=$(grep '^version' pyproject.toml | head -1 | sed 's/.*"\(.*\)".*/\1/')

# Phase 2: Version (semantic-release: bump pyproject.toml, CHANGELOG, git tag, GitHub release)
mise run release:version

# Detect "no new release" — if semantic-release published nothing, abort cleanly
VERSION_AFTER=$(grep '^version' pyproject.toml | head -1 | sed 's/.*"\(.*\)".*/\1/')
if [[ "$VERSION_BEFORE" == "$VERSION_AFTER" ]]; then
    echo ""
    echo "  ⚠ semantic-release did not create a new version."
    echo "    No releasable conventional commits found."
    echo "    Aborting (nothing to sync/publish)."
    exit 0
fi

# Phase 3: Sync (uv build + push tag + push main)
mise run release:sync

# Phase 3b: PyPI Publish (uv publish via 1Password token — no GitHub Actions)
mise run release:pypi

# Phase 4: Verify
mise run release:verify

echo ""
echo "╔═══════════════════════════════════════════════════════════╗"
echo "║  ✓ Release workflow complete!                             ║"
echo "╚═══════════════════════════════════════════════════════════╝"
echo ""
