# .github/workflows/release.yml

name: Tag Version and Build Release

on:
  push:
    branches:
      - main

permissions:
  contents: write

jobs:
  create_tag:
    runs-on: ubuntu-latest
    outputs:
      version: ${{ steps.get_version.outputs.version }}
      version_changed: ${{ steps.check_version.outputs.version_changed }}
    steps:
      - name: Checkout Code
        uses: actions/checkout@v3
        with:
          fetch-depth: 0

      - name: Get Previous Version
        id: get_prev_version
        run: |
          git fetch --tags
          PREV_VERSION=$(git tag --sort=-v:refname | head -n 1 | sed 's/^v//')
          echo "prev_version=${PREV_VERSION}" >> $GITHUB_OUTPUT

      - name: Get Current Version
        id: get_version
        run: |
          VERSION=$(cargo metadata --format-version 1 | jq -r '.packages[] | select(.name=="nebulous") | .version')
          echo "version=${VERSION}" >> $GITHUB_OUTPUT

      - name: Check if Version Changed
        id: check_version
        run: |
          if [ "${{ steps.get_prev_version.outputs.prev_version }}" != "${{ steps.get_version.outputs.version }}" ]; then
            echo "version_changed=true" >> $GITHUB_OUTPUT
          else
            echo "version_changed=false" >> $GITHUB_OUTPUT
          fi

      - name: Create Tag
        if: ${{ steps.check_version.outputs.version_changed == 'true' }}
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"
          git tag -a "v${{ steps.get_version.outputs.version }}" -m "Release version ${{ steps.get_version.outputs.version }}"
          git push origin "v${{ steps.get_version.outputs.version }}"

  build_and_release:
    needs: create_tag
    if: ${{ needs.create_tag.outputs.version_changed == 'true' }}
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        include:
          - os: macos-14
            target: aarch64-apple-darwin
            arch: arm64
            platform: darwin
          - os: ubuntu-22.04
            target: x86_64-unknown-linux-gnu
            arch: amd64
            platform: linux

    steps:
      - name: Checkout Code
        uses: actions/checkout@v3
        with:
          fetch-depth: 0

      # Build steps
      - name: Install Rust
        uses: actions-rs/toolchain@v1
        with:
          toolchain: stable
          target: ${{ matrix.target }}
          override: true

      - name: Install Dependencies
        run: |
          if [ "${{ matrix.platform }}" == "darwin" ]; then
            brew install openssl pkg-config
            echo "PKG_CONFIG_PATH=$(brew --prefix openssl)/lib/pkgconfig" >> $GITHUB_ENV
          else
            sudo apt-get update && sudo apt-get install -y \
              musl-tools \
              pkg-config \
              libssl-dev
          fi
        shell: bash

      - name: Build Binary
        env:
          TAG_NAME: "v${{ needs.create_tag.outputs.version }}"
        run: |
          echo "Building version $TAG_NAME..."
          cargo build --release --target ${{ matrix.target }}

      - name: Prepare Binary
        env:
          TAG_NAME: "v${{ needs.create_tag.outputs.version }}"
        run: |
          BINARY_NAME=nebu
          SOURCE_NAME=nebulous
          TARGET_DIR="target/${{ matrix.target }}/release"
          BINARY_FILENAME="${BINARY_NAME}-${TAG_NAME}-${{ matrix.platform }}-${{ matrix.arch }}"
          
          # Copy the binary with the versioned name and rename to nebu
          cp "${TARGET_DIR}/${SOURCE_NAME}" "${BINARY_FILENAME}"
          
          echo "Generating checksum..."
          if [[ "${{ matrix.platform }}" == "darwin" ]]; then
            shasum -a 256 "${BINARY_FILENAME}" >> checksums.txt
          else
            sha256sum "${BINARY_FILENAME}" >> checksums.txt
          fi

      - name: Upload Artifact
        uses: actions/upload-artifact@v4
        with:
          name: binaries-${{ matrix.platform }}-${{ matrix.arch }}
          path: |
            nebu-*
            checksums.txt
          retention-days: 1

      - name: Create GitHub Release
        uses: softprops/action-gh-release@v1
        with:
          tag_name: "v${{ needs.create_tag.outputs.version }}"
          files: "nebu-*"
          body: "Release version ${{ needs.create_tag.outputs.version }}"

      - name: Configure AWS credentials
        uses: aws-actions/configure-aws-credentials@v4
        with:
          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          aws-region: ${{ secrets.AWS_REGION }}

      - name: Verify AWS Authentication
        run: |
          aws s3 ls
        shell: bash

      - name: Upload to S3
        run: |
          # Upload versioned binary to version-specific directory
          aws s3 cp nebu-v${{ needs.create_tag.outputs.version }}-${{ matrix.platform }}-${{ matrix.arch }} s3://nebulous-artifacts/releases/v${{ needs.create_tag.outputs.version }}/
          aws s3 cp checksums.txt s3://nebulous-artifacts/releases/v${{ needs.create_tag.outputs.version }}/
          
          # Create and upload "latest" named binary
          cp nebu-v${{ needs.create_tag.outputs.version }}-${{ matrix.platform }}-${{ matrix.arch }} nebu-latest-${{ matrix.platform }}-${{ matrix.arch }}
          
          # Generate new checksums for latest binary
          rm checksums.txt
          if [[ "${{ matrix.platform }}" == "darwin" ]]; then
            shasum -a 256 nebu-latest-${{ matrix.platform }}-${{ matrix.arch }} >> checksums.txt
          else
            sha256sum nebu-latest-${{ matrix.platform }}-${{ matrix.arch }} >> checksums.txt
          fi
          
          # Upload latest binary
          aws s3 cp nebu-latest-${{ matrix.platform }}-${{ matrix.arch }} s3://nebulous-artifacts/releases/latest/
          aws s3 cp checksums.txt s3://nebulous-artifacts/releases/latest/
        shell: bash