name: Build, Test, and Release

permissions:
  contents: write # Required for creating releases and uploading assets

on:
  push:
    paths-ignore:
      - 'plugin/**'
    branches: [ "main" ]
    tags:
      - 'v[0-9]+.[0-9]+.[0-9]+*' # Trigger on version tags like v0.1.0
  pull_request:
    paths-ignore:
      - 'plugin/**'
    branches: [ "main" ]
  workflow_dispatch:

env:
  CARGO_TERM_COLOR: always

jobs:
  # Job 1: Runs on every push/PR
  build_and_test:
    name: Build and Test
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Check cargo formatting
        run: cargo fmt -- --check

      - name: Build
        run: cargo build

      - name: Run tests
        run: cargo test

      - name: Install choreo
        run: cargo install choreo

      - name: Install tbdflow on PATH
        run: cargo install --path .

      - name: Configure git for BDD tests
        run: |
          git config --global user.email "ci@tbdflow.dev"
          git config --global user.name "tbdflow CI"
          git config --global init.defaultBranch main

      - name: Run choreo BDD tests
        run: |
          choreo run --file tests/tbdflow_branch.chor
          choreo run --file tests/tbdflow_commit_types.chor
          choreo run --file tests/tbdflow_commit.chor
          choreo run --file tests/tbdflow_complete.chor
          choreo run --file tests/tbdflow_sync_status.chor
          choreo run --file tests/tbdflow_undo.chor
          choreo run --file tests/tbdflow_radar.chor

  # Job 2: Creates the release on GitHub. Only runs on new tags.
  create_release:
    name: Create GitHub Release
    runs-on: ubuntu-latest
    needs: build_and_test
    if: startsWith(github.ref, 'refs/tags/v')
    outputs:
      upload_url: ${{ steps.create_release.outputs.upload_url }}
    steps:
      - name: Create Release
        id: create_release
        uses: actions/create-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: ${{ github.ref_name }}
          release_name: Release ${{ github.ref_name }}
          draft: false
          prerelease: false

  # Job 3: Builds binaries for multiple platforms and uploads them. Only runs on new tags.
  build_and_upload_assets:
    name: Build & Upload Release Assets
    needs: create_release
    strategy:
      matrix:
        include:
          - target: x86_64-unknown-linux-gnu
            os: ubuntu-latest
          - target: aarch64-unknown-linux-gnu
            os: ubuntu-latest
          - target: x86_64-apple-darwin
            os: macos-latest
          - target: aarch64-apple-darwin
            os: macos-latest
          - target: x86_64-pc-windows-msvc
            os: windows-latest
    runs-on: ${{ matrix.os }}
    steps:
      - name: Checkout sources
        uses: actions/checkout@v4

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

      - name: Install aarch64 linker (for cross-compiling on Ubuntu)
        if: matrix.target == 'aarch64-unknown-linux-gnu'
        run: |
          sudo apt-get update
          sudo apt-get install -y gcc-aarch64-linux-gnu

      - name: Build binary
        env:
          # Set linker for aarch64 cross-compile
          CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER: aarch64-linux-gnu-gcc
        run: cargo build --verbose --release --target ${{ matrix.target }}

      - name: Package binary for release
        shell: bash
        run: |
          # The binary will be in the release directory
          cd target/${{ matrix.target }}/release
          
          # Rename the binary to include the target triple
          if [ "${{ matrix.os }}" = "windows-latest" ]; then
            mv tbdflow.exe tbdflow-${{ matrix.target }}.exe
            echo "ASSET_NAME=tbdflow-${{ matrix.target }}.exe" >> $GITHUB_ENV
          else
            mv tbdflow tbdflow-${{ matrix.target }}
            echo "ASSET_NAME=tbdflow-${{ matrix.target }}" >> $GITHUB_ENV
          fi

      - name: Upload Release Asset
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ needs.create_release.outputs.upload_url }}
          asset_path: ./target/${{ matrix.target }}/release/${{ env.ASSET_NAME }}
          asset_name: ${{ env.ASSET_NAME }}
          asset_content_type: application/octet-stream

  # Job 4: Publishes to crates.io. Only runs on new tags.
  publish_to_crates:
    name: Publish to crates.io
    needs: build_and_test
    runs-on: ubuntu-latest
    if: startsWith(github.ref, 'refs/tags/v')
    steps:
      - name: Checkout sources
        uses: actions/checkout@v4

      - name: Publish to crates.io
        run: cargo publish --allow-dirty --token ${{ secrets.CRATES_TOKEN }}
