name: Release Artifacts

on:
  release:
    types: [published]
  workflow_dispatch:
    inputs:
      tag:
        description: "Tag to publish artifacts for (example: v0.26.0)"
        required: true
        type: string

permissions:
  contents: read

concurrency:
  group: release-artifacts-${{ github.event.release.tag_name || inputs.tag }}
  cancel-in-progress: false

jobs:
  resolve-tag:
    runs-on: ubuntu-latest
    outputs:
      tag: ${{ steps.resolve.outputs.tag }}
    steps:
      - name: Resolve release tag
        id: resolve
        shell: bash
        run: |
          tag="${{ github.event.release.tag_name || inputs.tag }}"
          if [ -z "${tag}" ]; then
            echo "Unable to resolve release tag."
            exit 1
          fi
          echo "tag=${tag}" >> "${GITHUB_OUTPUT}"

  build:
    needs: resolve-tag
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: ubuntu-latest
            target: x86_64-unknown-linux-gnu
            artifact_name: romm-cli-linux-x86_64
            archive: tar.gz
          - os: ubuntu-latest
            target: aarch64-unknown-linux-gnu
            artifact_name: romm-cli-linux-aarch64
            archive: tar.gz
          - os: windows-latest
            target: x86_64-pc-windows-msvc
            artifact_name: romm-cli-windows-x86_64
            archive: zip
          - os: macos-latest
            target: x86_64-apple-darwin
            artifact_name: romm-cli-macos-x86_64
            archive: tar.gz
          - os: macos-latest
            target: aarch64-apple-darwin
            artifact_name: romm-cli-macos-aarch64
            archive: tar.gz
    runs-on: ${{ matrix.os }}
    env:
      CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER: aarch64-linux-gnu-gcc
    steps:
      - uses: actions/checkout@v4
        with:
          ref: ${{ needs.resolve-tag.outputs.tag }}

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

      - name: Cache cargo registry and target
        uses: Swatinem/rust-cache@v2
        with:
          key: release-${{ matrix.target }}

      - name: Install cross-compiler (Linux aarch64)
        if: ${{ matrix.target == 'aarch64-unknown-linux-gnu' }}
        run: sudo apt-get update && sudo apt-get install -y gcc-aarch64-linux-gnu

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

      - name: Prepare artifact directory
        run: |
          mkdir -p artifact
          TGT="target/${{ matrix.target }}/release"
          if [ "${{ matrix.os }}" = "windows-latest" ]; then
            cp "$TGT/romm-cli.exe" artifact/
            cp "$TGT/romm-tui.exe" artifact/
          else
            cp "$TGT/romm-cli" artifact/
            cp "$TGT/romm-tui" artifact/
          fi
        shell: bash

      - name: Fix permissions (Unix)
        if: ${{ matrix.os != 'windows-latest' }}
        run: chmod +x artifact/romm-cli artifact/romm-tui

      - name: Create archive (Unix)
        if: ${{ matrix.os != 'windows-latest' }}
        run: |
          cd artifact && tar czvf ../${{ matrix.artifact_name }}.${{ matrix.archive }} * && cd ..
        shell: bash

      - name: Create archive (Windows)
        if: ${{ matrix.os == 'windows-latest' }}
        run: Compress-Archive -Path artifact\* -DestinationPath ${{ matrix.artifact_name }}.${{ matrix.archive }}
        shell: pwsh

      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.artifact_name }}
          path: ${{ matrix.artifact_name }}.${{ matrix.archive }}

  upload-assets:
    needs: [resolve-tag, build]
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - name: Download all artifacts
        uses: actions/download-artifact@v4
        with:
          path: artifacts
          merge-multiple: false

      - name: Prepare release assets
        run: |
          mkdir -p release-assets
          cp artifacts/*/* release-assets/

      - name: Generate checksums
        run: |
          cd release-assets
          sha256sum * > checksums.txt
          cat checksums.txt

      - name: Upload assets to release
        uses: softprops/action-gh-release@v2
        with:
          tag_name: ${{ needs.resolve-tag.outputs.tag }}
          files: release-assets/*
          overwrite_files: true
          fail_on_unmatched_files: true
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

  publish-crates-io:
    needs: [resolve-tag, build, upload-assets]
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          ref: ${{ needs.resolve-tag.outputs.tag }}
      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable
      - name: Verify crates.io package
        run: cargo publish --dry-run
      - name: Publish to crates.io
        run: cargo publish --token ${{ secrets.CARGO_REGISTRY_TOKEN }}
