name: Release

on:
  push:
    tags:
      - 'v*'

permissions:
  contents: write

jobs:
  test:
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest, macos-latest, windows-latest]
    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - uses: Swatinem/rust-cache@v2
      - run: cargo fmt -- --check
      - run: cargo clippy --all-targets --all-features -- -D warnings
      - run: cargo test
      - run: cargo test --no-default-features

  build:
    needs: test
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: ubuntu-latest
            target: x86_64-unknown-linux-gnu
            binary_name: kagi
            asset_name: kagi-x86_64-unknown-linux-gnu
          - os: macos-latest
            target: aarch64-apple-darwin
            binary_name: kagi
            asset_name: kagi-aarch64-apple-darwin
          - os: windows-latest
            target: x86_64-pc-windows-msvc
            binary_name: kagi.exe
            asset_name: kagi-x86_64-pc-windows-msvc.exe
    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
        with:
          targets: ${{ matrix.target }}
      - uses: Swatinem/rust-cache@v2
      - run: cargo build --release --target ${{ matrix.target }}
      - name: Package
        shell: bash
        run: |
          mkdir -p dist
          cp target/${{ matrix.target }}/release/${{ matrix.binary_name }} dist/${{ matrix.asset_name }}
      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.asset_name }}
          path: dist/${{ matrix.asset_name }}

  github-release:
    needs: build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Download artifacts
        uses: actions/download-artifact@v4
        with:
          path: dist
          merge-multiple: true
      - name: Create release
        uses: softprops/action-gh-release@v2
        with:
          files: dist/*
          generate_release_notes: true

  crates-io:
    needs: test
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - uses: Swatinem/rust-cache@v2

      - name: Publish kagi-domain
        run: cargo publish -p kagi-domain --token ${{ secrets.CARGO_REGISTRY_TOKEN }}

      - name: Wait for index propagation
        run: sleep 45

      - name: Publish kagi-crypto
        run: cargo publish -p kagi-crypto --token ${{ secrets.CARGO_REGISTRY_TOKEN }}

      - name: Wait for index propagation
        run: sleep 45

      - name: Publish kagi-sync
        run: cargo publish -p kagi-sync --token ${{ secrets.CARGO_REGISTRY_TOKEN }}

      - name: Wait for index propagation
        run: sleep 45

      - name: Publish kagi-store
        run: cargo publish -p kagi-store --token ${{ secrets.CARGO_REGISTRY_TOKEN }}

      - name: Wait for index propagation
        run: sleep 45

      - name: Publish kagi-server
        run: cargo publish -p kagi-server --token ${{ secrets.CARGO_REGISTRY_TOKEN }}

      - name: Wait for index propagation
        run: sleep 45

      - name: Publish kagi-app
        run: cargo publish -p kagi-app --token ${{ secrets.CARGO_REGISTRY_TOKEN }}

      - name: Wait for index propagation
        run: sleep 45

      - name: Publish kagi-vault
        run: cargo publish -p kagi-vault --token ${{ secrets.CARGO_REGISTRY_TOKEN }}
