name: CI and Release

on:
    push:
        branches: [main]
        tags: ['v*']
    pull_request: {}

env:
    CARGO_TERM_COLOR: always

jobs:
    fmt:
        runs-on: ubuntu-latest
        steps:
            - uses: actions/checkout@v4
            - uses: dtolnay/rust-toolchain@stable
              with:
                  components: rustfmt
            - uses: Swatinem/rust-cache@v2
            - name: cargo fmt
              run: cargo fmt --all -- --check

    test:
        runs-on: ubuntu-latest
        needs: fmt
        steps:
            - uses: actions/checkout@v4
            - name: Install Linux dependencies
              run: sudo apt-get update && sudo apt-get install -y libdbus-1-dev
            - uses: dtolnay/rust-toolchain@stable
            - uses: Swatinem/rust-cache@v2
            - name: Unit + integration tests
              run: cargo test --locked --all-features --all-targets

    e2e:
        runs-on: ubuntu-latest
        needs: test
        if: startsWith(github.ref, 'refs/tags/')
        steps:
            - uses: actions/checkout@v4
            - name: Install Linux dependencies
              run: sudo apt-get update && sudo apt-get install -y libdbus-1-dev
            - uses: dtolnay/rust-toolchain@stable
            - uses: Swatinem/rust-cache@v2
            - name: End-to-end tests (GitHub target)
              env:
                RUN_E2E: '1'
                GITHUB_PAT: ${{ secrets.E2E_GITHUB_TEST_TOKEN }}
                E2E_GITHUB_REPO: ${{ vars.E2E_GITHUB_REPO }}
              run: cargo test --locked --all-features tests::e2e_github_target
            - name: End-to-end tests (Resend source)
              env:
                RUN_E2E: '1'
                RESEND_API_KEY: ${{ secrets.RESEND_API_KEY }}
              run: cargo test --locked --all-features tests::e2e_resend_source -- --test-threads=1

    build-binaries:
        name: Build ${{ matrix.target }}
        needs: e2e
        if: startsWith(github.ref, 'refs/tags/')
        strategy:
            fail-fast: false
            matrix:
                include:
                    - os: ubuntu-latest
                      target: x86_64-unknown-linux-gnu
                      bin_ext: ''
                    - os: macos-14
                      target: aarch64-apple-darwin
                      bin_ext: ''
                    - os: macos-latest
                      target: x86_64-apple-darwin
                      bin_ext: ''
                    - os: windows-latest
                      target: x86_64-pc-windows-msvc
                      bin_ext: '.exe'
        runs-on: ${{ matrix.os }}
        steps:
            - uses: actions/checkout@v4
            - name: Install Linux dependencies
              if: runner.os == 'Linux'
              run: sudo apt-get update && sudo apt-get install -y libdbus-1-dev
            - uses: dtolnay/rust-toolchain@stable
              with:
                  targets: ${{ matrix.target }}
            - uses: Swatinem/rust-cache@v2
            - name: Build release binary
              run: cargo build --locked --release --target ${{ matrix.target }}
            - name: Package artifact
              shell: bash
              run: |
                  BIN="target/${{ matrix.target }}/release/cred${{ matrix.bin_ext }}"
                  OUT="cred-${GITHUB_REF_NAME}-${{ matrix.target }}${{ matrix.bin_ext }}"
                  mkdir -p dist
                  cp "$BIN" "dist/$OUT"
            - name: Upload artifact
              uses: actions/upload-artifact@v4
              with:
                  name: cred-${{ matrix.target }}
                  path: dist/*
                  if-no-files-found: error

    publish-release:
        runs-on: ubuntu-latest
        needs: build-binaries
        if: startsWith(github.ref, 'refs/tags/')
        steps:
            - uses: actions/checkout@v4
            - uses: actions/download-artifact@v4
              with:
                  pattern: cred-*
                  merge-multiple: true
                  path: dist
            - name: Create GitHub release
              uses: softprops/action-gh-release@v2
              with:
                  files: dist/cred-*
              env:
                  GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

    publish-crate:
        runs-on: ubuntu-latest
        needs: build-binaries
        if: startsWith(github.ref, 'refs/tags/')
        steps:
            - uses: actions/checkout@v4
            - name: Install Linux dependencies
              run: sudo apt-get update && sudo apt-get install -y libdbus-1-dev
            - uses: dtolnay/rust-toolchain@stable
            - uses: Swatinem/rust-cache@v2
            - name: Publish to crates.io
              env:
                CARGO_REGISTRY_TOKEN: ${{ secrets.CRATES_IO_TOKEN }}
              run: cargo publish --locked
