name: Publish crate

on:
  push:
    branches:
      - master
    tags:
      - "v*"

permissions:
  contents: read

concurrency:
  group: publish-crate-${{ github.ref }}
  cancel-in-progress: false

jobs:
  package:
    name: Build and package
    runs-on: ubuntu-latest

    steps:
      - name: Checkout
        uses: actions/checkout@v4

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

      - name: Cache cargo artifacts
        uses: Swatinem/rust-cache@v2

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

      - name: Validate release tag
        if: startsWith(github.ref, 'refs/tags/v')
        shell: bash
        run: |
          release_tag="${GITHUB_REF_NAME}"
          crate_version="$(cargo metadata --no-deps --format-version 1 | python3 -c 'import json,sys; print(json.load(sys.stdin)["packages"][0]["version"])')"
          tag_version="${release_tag#v}"
          if [[ "$crate_version" != "$tag_version" ]]; then
            echo "Tag $release_tag does not match Cargo.toml version $crate_version" >&2
            exit 1
          fi

      - name: Build release binary
        run: cargo build --locked --release --verbose

      - name: Run tests
        run: cargo test --locked --verbose

      - name: Package crate
        run: cargo package --locked --verbose

      - name: Validate publish
        if: startsWith(github.ref, 'refs/tags/v')
        run: cargo publish --locked --dry-run

  publish:
    name: Publish to crates.io
    needs: package
    if: startsWith(github.ref, 'refs/tags/v')
    runs-on: ubuntu-latest

    steps:
      - name: Checkout
        uses: actions/checkout@v4

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

      - name: Cache cargo artifacts
        uses: Swatinem/rust-cache@v2

      - name: Publish crate
        run: cargo publish --locked
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}

  release-assets:
    name: Build release assets
    needs: package
    if: startsWith(github.ref, 'refs/tags/v')
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: ubuntu-latest
            target: x86_64-unknown-linux-gnu
          - os: macos-latest
            target: aarch64-apple-darwin
          - os: windows-latest
            target: x86_64-pc-windows-msvc
    runs-on: ${{ matrix.os }}

    steps:
      - name: Checkout
        uses: actions/checkout@v4

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

      - name: Cache cargo artifacts
        uses: Swatinem/rust-cache@v2

      - name: Build release binary
        run: cargo build --locked --release --verbose

      - name: Package macOS binary
        if: runner.os == 'macOS'
        shell: bash
        run: |
          version="$(cargo metadata --no-deps --format-version 1 | python3 -c 'import json,sys; print(json.load(sys.stdin)["packages"][0]["version"])')"
          asset="thufs-${version}-${{ matrix.target }}.tar.gz"
          mkdir -p dist
          cp target/release/thufs dist/thufs
          tar -C dist -czf "dist/${asset}" thufs
          rm dist/thufs
          shasum -a 256 "dist/${asset}" > "dist/${asset}.sha256"

      - name: Package Linux binary
        if: runner.os == 'Linux'
        shell: bash
        run: |
          version="$(cargo metadata --no-deps --format-version 1 | python3 -c 'import json,sys; print(json.load(sys.stdin)["packages"][0]["version"])')"
          asset="thufs-${version}-${{ matrix.target }}.tar.gz"
          mkdir -p dist
          cp target/release/thufs dist/thufs
          tar -C dist -czf "dist/${asset}" thufs
          rm dist/thufs
          (cd dist && sha256sum "${asset}" > "${asset}.sha256")

      - name: Package Windows binary
        if: runner.os == 'Windows'
        shell: pwsh
        run: |
          $metadata = cargo metadata --no-deps --format-version 1 | ConvertFrom-Json
          $version = $metadata.packages[0].version
          $asset = "thufs-$version-${{ matrix.target }}.zip"
          New-Item -ItemType Directory -Force dist | Out-Null
          Copy-Item target/release/thufs.exe dist/thufs.exe
          Compress-Archive -Path dist/thufs.exe -DestinationPath "dist/$asset" -Force
          Remove-Item dist/thufs.exe
          $hash = (Get-FileHash "dist/$asset" -Algorithm SHA256).Hash.ToLower()
          "$hash  $asset" | Out-File -FilePath "dist/$asset.sha256" -Encoding ascii

      - name: Upload release asset
        uses: actions/upload-artifact@v4
        with:
          name: release-asset-${{ matrix.target }}
          path: dist/*
          if-no-files-found: error

  github-release:
    name: Create GitHub Release
    needs:
      - package
      - publish
      - release-assets
    if: always() && startsWith(github.ref, 'refs/tags/v') && needs.package.result == 'success' && needs.publish.result == 'success' && needs.release-assets.result == 'success'
    runs-on: ubuntu-latest
    permissions:
      contents: write

    steps:
      - name: Download release assets
        uses: actions/download-artifact@v4
        with:
          pattern: release-asset-*
          path: dist
          merge-multiple: true

      - name: Create or update release
        shell: bash
        run: |
          release_tag="${GITHUB_REF_NAME}"
          version="${release_tag#v}"
          {
            echo "## thufs ${version}"
            echo
            echo "Highlights:"
            echo
            echo "- download now accepts THU Cloud share URLs and \`--share <hashcode>\`, and ignores query strings like \`?dl=1\` when extracting the token"
            echo "- minimum supported Rust version is now 1.85, with CI coverage to keep that floor stable"
            echo "- GitHub Releases include a prebuilt macOS archive for Apple Silicon; Intel Macs should use cargo install"
            echo
            echo "Install from crates.io:"
            echo
            echo '```bash'
            echo "cargo install thufs --version ${version} --locked"
            echo '```'
            echo
            echo "Crate: https://crates.io/crates/thufs/${version}"
            echo
            echo "Release assets:"
            for asset in dist/*; do
              echo "- $(basename "$asset")"
            done
          } > release-notes.md

          if gh release view "$release_tag" --repo "$GITHUB_REPOSITORY" >/dev/null 2>&1; then
            gh release edit "$release_tag" \
              --repo "$GITHUB_REPOSITORY" \
              --title "$release_tag" \
              --notes-file release-notes.md
          else
            gh release create "$release_tag" \
              --repo "$GITHUB_REPOSITORY" \
              --title "$release_tag" \
              --notes-file release-notes.md
          fi

          gh release upload "$release_tag" dist/* \
            --repo "$GITHUB_REPOSITORY" \
            --clobber

          gh release view "$release_tag" \
            --repo "$GITHUB_REPOSITORY" \
            --json tagName,name,url,isDraft,isPrerelease,publishedAt,assets \
            --jq '{tag: .tagName, name, url, draft: .isDraft, prerelease: .isPrerelease, publishedAt, assets: [.assets[].name]}'
        env:
          GH_TOKEN: ${{ github.token }}
