name: Release

on:
  push:
    tags:
      - 'v*'

env:
  CARGO_TERM_COLOR: always
  RUST_BACKTRACE: 1

permissions:
  contents: write

jobs:
  build-release:
    name: Build Release (${{ matrix.target }})
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: ubuntu-latest
            target: x86_64-unknown-linux-gnu
            artifact-name: synor-linux-x86_64
            archive-ext: tar.gz
          - os: ubuntu-latest
            target: aarch64-unknown-linux-gnu
            artifact-name: synor-linux-aarch64
            archive-ext: tar.gz
            cross: true
          - os: macos-latest
            target: x86_64-apple-darwin
            artifact-name: synor-macos-x86_64
            archive-ext: tar.gz
          - os: macos-latest
            target: aarch64-apple-darwin
            artifact-name: synor-macos-aarch64
            archive-ext: tar.gz

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Install Rust toolchain
        uses: dtolnay/rust-action@stable
        with:
          targets: ${{ matrix.target }}

      - name: Install cross-compilation tools
        if: matrix.cross
        run: |
          sudo apt-get update
          sudo apt-get install -y gcc-aarch64-linux-gnu g++-aarch64-linux-gnu

      - name: Install system dependencies (Linux)
        if: runner.os == 'Linux' && !matrix.cross
        run: |
          sudo apt-get update
          sudo apt-get install -y libclang-dev llvm-dev

      - name: Cache cargo registry
        uses: actions/cache@v4
        with:
          path: |
            ~/.cargo/bin/
            ~/.cargo/registry/index/
            ~/.cargo/registry/cache/
            ~/.cargo/git/db/
          key: ${{ runner.os }}-${{ matrix.target }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: |
            ${{ runner.os }}-${{ matrix.target }}-cargo-registry-

      - name: Cache cargo target
        uses: actions/cache@v4
        with:
          path: target
          key: ${{ runner.os }}-${{ matrix.target }}-cargo-target-release-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: |
            ${{ runner.os }}-${{ matrix.target }}-cargo-target-release-

      - name: Build release binaries
        env:
          TARGET: ${{ matrix.target }}
          CROSS: ${{ matrix.cross }}
        run: |
          if [[ "$CROSS" == "true" ]]; then
            export CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER=aarch64-linux-gnu-gcc
            export CC_aarch64_unknown_linux_gnu=aarch64-linux-gnu-gcc
            export CXX_aarch64_unknown_linux_gnu=aarch64-linux-gnu-g++
          fi
          cargo build --release --workspace --target "$TARGET"

      - name: Prepare release archive
        env:
          TARGET: ${{ matrix.target }}
          ARTIFACT_NAME: ${{ matrix.artifact-name }}
        run: |
          mkdir -p release

          # Copy binaries
          cp "target/$TARGET/release/synord" release/ 2>/dev/null || true
          cp "target/$TARGET/release/synor-cli" release/ 2>/dev/null || true
          cp "target/$TARGET/release/synor-faucet" release/ 2>/dev/null || true
          cp "target/$TARGET/release/synor-explorer" release/ 2>/dev/null || true

          # Copy documentation
          cp README.md release/ 2>/dev/null || true
          cp LICENSE* release/ 2>/dev/null || true
          cp CHANGELOG.md release/ 2>/dev/null || true

          # Create archive
          cd release
          tar czvf "../$ARTIFACT_NAME.tar.gz" *

      - name: Upload release artifact
        uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.artifact-name }}
          path: ${{ matrix.artifact-name }}.tar.gz
          retention-days: 1

  create-release:
    name: Create GitHub Release
    runs-on: ubuntu-latest
    needs: build-release

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Download all artifacts
        uses: actions/download-artifact@v4
        with:
          path: artifacts

      - name: Generate changelog
        id: changelog
        env:
          GIT_REF: ${{ github.ref }}
        run: |
          # Get the current tag from the ref (safe - only used after validation)
          CURRENT_TAG="${GIT_REF#refs/tags/}"

          # Validate tag format (only allow v followed by semver-like pattern)
          if [[ ! "$CURRENT_TAG" =~ ^v[0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z0-9.]+)?$ ]]; then
            echo "Invalid tag format: $CURRENT_TAG"
            exit 1
          fi

          echo "current_tag=$CURRENT_TAG" >> "$GITHUB_OUTPUT"

          # Get the previous tag
          PREVIOUS_TAG=$(git describe --tags --abbrev=0 "$CURRENT_TAG^" 2>/dev/null || echo "")

          echo "## What's Changed" > CHANGELOG_BODY.md
          echo "" >> CHANGELOG_BODY.md

          if [ -n "$PREVIOUS_TAG" ]; then
            echo "Changes since $PREVIOUS_TAG:" >> CHANGELOG_BODY.md
            echo "" >> CHANGELOG_BODY.md

            # Generate changelog from commits (commit messages are from our own repo)
            git log "$PREVIOUS_TAG..$CURRENT_TAG" --pretty=format:"- %s (%h)" --no-merges >> CHANGELOG_BODY.md
          else
            echo "Initial release" >> CHANGELOG_BODY.md
            echo "" >> CHANGELOG_BODY.md
            git log --pretty=format:"- %s (%h)" --no-merges -20 >> CHANGELOG_BODY.md
          fi

          echo "" >> CHANGELOG_BODY.md
          echo "" >> CHANGELOG_BODY.md
          echo "## Installation" >> CHANGELOG_BODY.md
          echo "" >> CHANGELOG_BODY.md
          echo "Download the appropriate archive for your platform and extract it:" >> CHANGELOG_BODY.md
          echo "" >> CHANGELOG_BODY.md
          echo '```bash' >> CHANGELOG_BODY.md
          echo "tar xzf synor-<platform>.tar.gz" >> CHANGELOG_BODY.md
          echo "./synord --help" >> CHANGELOG_BODY.md
          echo '```' >> CHANGELOG_BODY.md
          echo "" >> CHANGELOG_BODY.md
          echo "## Checksums" >> CHANGELOG_BODY.md
          echo "" >> CHANGELOG_BODY.md
          echo '```' >> CHANGELOG_BODY.md
          cd artifacts
          find . -name "*.tar.gz" -exec sha256sum {} \; | sed 's|./[^/]*/||' >> ../CHANGELOG_BODY.md
          echo '```' >> CHANGELOG_BODY.md

      - name: Create GitHub Release
        uses: softprops/action-gh-release@v2
        with:
          name: Synor ${{ steps.changelog.outputs.current_tag }}
          body_path: CHANGELOG_BODY.md
          draft: false
          prerelease: ${{ contains(github.ref, 'alpha') || contains(github.ref, 'beta') || contains(github.ref, 'rc') }}
          files: |
            artifacts/**/*.tar.gz
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

  # Optional: Publish to crates.io
  publish-crates:
    name: Publish to crates.io
    runs-on: ubuntu-latest
    needs: create-release
    if: ${{ !contains(github.ref, 'alpha') && !contains(github.ref, 'beta') && !contains(github.ref, 'rc') }}

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Install Rust toolchain
        uses: dtolnay/rust-action@stable

      - name: Install system dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y libclang-dev llvm-dev

      - name: Publish crates
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
        run: |
          # Publish crates in dependency order
          # Skip if CARGO_REGISTRY_TOKEN is not set
          if [ -z "$CARGO_REGISTRY_TOKEN" ]; then
            echo "CARGO_REGISTRY_TOKEN not set, skipping crates.io publish"
            exit 0
          fi

          echo "Publishing to crates.io..."
          # Add --dry-run to test first, remove for actual publish
          # cargo publish -p synor-types --dry-run
          # cargo publish -p synor-crypto --dry-run
          # ... etc
          echo "Crate publishing configured but commented out - uncomment when ready"
