name: Publish

on:
  release:
    types: [published]
  workflow_dispatch:
    inputs:
      dry_run:
        description: 'Dry run (do not publish)'
        required: false
        default: 'true'

env:
  CARGO_TERM_COLOR: always

jobs:
  publish-check:
    name: Pre-publish Checks
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

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

      - name: Check if version exists on crates.io
        id: check_version
        run: |
          VERSION=$(cargo metadata --no-deps --format-version 1 | jq -r '.packages[0].version')
          echo "version=$VERSION" >> $GITHUB_OUTPUT
          
          if cargo search daedra | grep -q "daedra = \"$VERSION\""; then
            echo "Version $VERSION already exists on crates.io"
            echo "exists=true" >> $GITHUB_OUTPUT
          else
            echo "Version $VERSION does not exist on crates.io"
            echo "exists=false" >> $GITHUB_OUTPUT
          fi

      - name: Run cargo publish --dry-run
        if: steps.check_version.outputs.exists == 'false'
        run: cargo publish --dry-run

    outputs:
      version: ${{ steps.check_version.outputs.version }}
      exists: ${{ steps.check_version.outputs.exists }}

  publish:
    name: Publish to crates.io
    needs: publish-check
    if: needs.publish-check.outputs.exists == 'false'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

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

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

      - name: Build release
        run: cargo build --release

      - name: Run tests
        run: cargo test --release -- --skip integration

      - name: Publish to crates.io
        if: github.event_name == 'release' || github.event.inputs.dry_run == 'false'
        run: cargo publish
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}

      - name: Dry run (skip actual publish)
        if: github.event.inputs.dry_run == 'true'
        run: |
          echo "Dry run mode - skipping actual publish"
          cargo publish --dry-run

  build-binaries:
    name: Build Release Binaries
    needs: publish-check
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        include:
          - os: ubuntu-latest
            target: x86_64-unknown-linux-gnu
            artifact_name: daedra
            asset_name: daedra-linux-x86_64
          - os: ubuntu-latest
            target: x86_64-unknown-linux-musl
            artifact_name: daedra
            asset_name: daedra-linux-x86_64-musl
          - os: macos-latest
            target: x86_64-apple-darwin
            artifact_name: daedra
            asset_name: daedra-macos-x86_64
          - os: macos-latest
            target: aarch64-apple-darwin
            artifact_name: daedra
            asset_name: daedra-macos-aarch64
          - os: windows-latest
            target: x86_64-pc-windows-msvc
            artifact_name: daedra.exe
            asset_name: daedra-windows-x86_64.exe

    steps:
      - uses: actions/checkout@v4

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

      - name: Install musl-tools (Linux musl)
        if: matrix.target == 'x86_64-unknown-linux-musl'
        run: sudo apt-get update && sudo apt-get install -y musl-tools

      - name: Build release binary
        run: cargo build --release --target ${{ matrix.target }}

      - name: Strip binary (Linux/macOS)
        if: runner.os != 'Windows'
        run: strip target/${{ matrix.target }}/release/${{ matrix.artifact_name }}

      - name: Create archive (Unix)
        if: runner.os != 'Windows'
        run: |
          cd target/${{ matrix.target }}/release
          tar czvf ../../../${{ matrix.asset_name }}.tar.gz ${{ matrix.artifact_name }}
          cd ../../..
          sha256sum ${{ matrix.asset_name }}.tar.gz > ${{ matrix.asset_name }}.tar.gz.sha256

      - name: Create archive (Windows)
        if: runner.os == 'Windows'
        shell: pwsh
        run: |
          cd target/${{ matrix.target }}/release
          Compress-Archive -Path ${{ matrix.artifact_name }} -DestinationPath ../../../${{ matrix.asset_name }}.zip
          cd ../../..
          Get-FileHash ${{ matrix.asset_name }}.zip -Algorithm SHA256 | Format-List > ${{ matrix.asset_name }}.zip.sha256

      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.asset_name }}
          path: |
            ${{ matrix.asset_name }}.*

      - name: Upload release assets
        if: github.event_name == 'release'
        uses: softprops/action-gh-release@v2
        with:
          files: |
            ${{ matrix.asset_name }}.*
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

  docker:
    name: Build and Push Docker Image
    needs: publish-check
    runs-on: ubuntu-latest
    if: github.event_name == 'release'
    steps:
      - uses: actions/checkout@v4

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3

      - name: Login to GitHub Container Registry
        uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Build and push
        uses: docker/build-push-action@v5
        with:
          context: .
          push: true
          tags: |
            ghcr.io/${{ github.repository }}:latest
            ghcr.io/${{ github.repository }}:${{ needs.publish-check.outputs.version }}
          cache-from: type=gha
          cache-to: type=gha,mode=max
