name: Release

on:
  push:
    tags:
      - "v*"

permissions:
  contents: write
  packages: write

env:
  CARGO_TERM_COLOR: always

jobs:
  # ── Tests ──
  test:
    name: Test
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - uses: Swatinem/rust-cache@v2
      - name: Web parity checklist gate (stable web tags)
        if: startsWith(github.ref_name, 'web-stable-')
        run: |
          scripts/check_web_parity_checklist.sh \
            22 25 26 27 28 29 30 34 37 38 40 41 42 43 44 45 46 47 49 50 51
      - uses: actions/setup-node@v4
        with:
          node-version: "20"
          cache: "npm"
      - run: npm ci
      - run: npx playwright install --with-deps chromium
      - run: npm run test:web
      - run: cargo test --all-features
      - run: cargo clippy --all-targets -- -D warnings

  # ── Build binaries ──
  build:
    name: Build (${{ matrix.target }})
    runs-on: ${{ matrix.os }}
    needs: [test]
    strategy:
      fail-fast: false
      matrix:
        include:
          - target: x86_64-unknown-linux-gnu
            os: ubuntu-latest
            artifact: pftui-x86_64-linux
            binary: pftui
          - target: aarch64-unknown-linux-gnu
            os: ubuntu-latest
            artifact: pftui-aarch64-linux
            binary: pftui
          - target: x86_64-apple-darwin
            os: macos-latest
            artifact: pftui-x86_64-macos
            binary: pftui
          - target: aarch64-apple-darwin
            os: macos-latest
            artifact: pftui-aarch64-macos
            binary: pftui
          - target: x86_64-pc-windows-msvc
            os: windows-latest
            artifact: pftui-x86_64-windows.exe
            binary: pftui.exe
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
        with:
          targets: ${{ matrix.target }}
      - uses: Swatinem/rust-cache@v2
        with:
          key: ${{ matrix.target }}

      - name: Install aarch64 cross-compile tools
        if: matrix.target == 'aarch64-unknown-linux-gnu'
        run: |
          sudo apt-get update
          sudo apt-get install -y gcc-aarch64-linux-gnu
          echo "CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER=aarch64-linux-gnu-gcc" >> $GITHUB_ENV

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

      - name: Package binary
        shell: bash
        run: |
          mkdir -p dist
          cp target/${{ matrix.target }}/release/${{ matrix.binary }} dist/${{ matrix.artifact }}
          cd dist && sha256sum ${{ matrix.artifact }} > ${{ matrix.artifact }}.sha256 || shasum -a 256 ${{ matrix.artifact }} > ${{ matrix.artifact }}.sha256

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

  # ── Build .deb ──
  deb:
    name: Build .deb
    runs-on: ubuntu-latest
    needs: [test]
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - uses: Swatinem/rust-cache@v2
      - run: cargo install cargo-deb
      - run: cargo deb
      - name: Upload .deb
        uses: actions/upload-artifact@v4
        with:
          name: deb-package
          path: target/debian/*.deb

  # ── Build .rpm ──
  rpm:
    name: Build .rpm
    runs-on: ubuntu-latest
    needs: [test]
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - uses: Swatinem/rust-cache@v2
      - run: cargo install cargo-generate-rpm
      - run: cargo build --release
      - run: cargo generate-rpm
      - name: Upload .rpm
        uses: actions/upload-artifact@v4
        with:
          name: rpm-package
          path: target/generate-rpm/*.rpm

  # ── GitHub Release ──
  github-release:
    name: GitHub Release
    runs-on: ubuntu-latest
    needs: [build, deb, rpm]
    if: always() && needs.build.result != 'cancelled'
    steps:
      - uses: actions/checkout@v4
      - uses: actions/download-artifact@v4
        with:
          path: artifacts
          pattern: "pftui-*"
          merge-multiple: false
        continue-on-error: true
      - uses: actions/download-artifact@v4
        with:
          path: artifacts
          pattern: "*-package"
          merge-multiple: false
        continue-on-error: true

      - name: Collect release files
        run: |
          mkdir -p release
          for dir in artifacts/pftui-*; do
            cp "$dir"/* release/ 2>/dev/null || true
          done
          cp artifacts/deb-package/*.deb release/ 2>/dev/null || true
          cp artifacts/rpm-package/*.rpm release/ 2>/dev/null || true
          ls -la release/

      - name: Create GitHub Release
        uses: softprops/action-gh-release@v2
        with:
          generate_release_notes: true
          files: release/*
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

  # ── crates.io ──
  crates-io:
    name: Publish to crates.io
    runs-on: ubuntu-latest
    needs: [test]
    continue-on-error: true
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - run: cargo publish
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}

  # ── Homebrew ──
  homebrew:
    name: Update Homebrew
    runs-on: ubuntu-latest
    needs: [github-release]
    continue-on-error: true
    if: always() && needs.github-release.result == 'success'
    steps:
      - name: Update Homebrew formula
        uses: dawidd6/action-homebrew-bump-formula@v4
        continue-on-error: true
        with:
          token: ${{ secrets.HOMEBREW_TAP_TOKEN }}
          tap: skylarsimoncelli/homebrew-tap
          formula: pftui
          tag: ${{ github.ref_name }}


  # ── APT/YUM Repository (GitHub Pages) ──
  package-repo:
    name: Update Package Repository
    runs-on: ubuntu-latest
    needs: [github-release]
    continue-on-error: true
    if: always() && needs.github-release.result == 'success'
    permissions:
      pages: write
      id-token: write
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - uses: actions/download-artifact@v4
        with:
          path: artifacts
          pattern: "*-package"
        continue-on-error: true

      - name: Copy website files to site root
        run: |
          mkdir -p site
          cp -r website/* site/

      - name: Build APT repository
        run: |
          sudo apt-get update && sudo apt-get install -y dpkg-dev
          mkdir -p site/apt/pool site/apt/dists/stable/main/binary-amd64
          cp artifacts/deb-package/*.deb site/apt/pool/ 2>/dev/null || true
          cd site/apt
          dpkg-scanpackages pool /dev/null | gzip -9c > dists/stable/main/binary-amd64/Packages.gz
          dpkg-scanpackages pool /dev/null > dists/stable/main/binary-amd64/Packages
          cat > dists/stable/Release << RELEASE
          Origin: pftui
          Label: pftui
          Suite: stable
          Codename: stable
          Architectures: amd64
          Components: main
          Description: pftui package repository
          RELEASE
          cd ../..

      - name: Build RPM repository
        run: |
          sudo apt-get install -y createrepo-c
          mkdir -p site/rpm/packages
          cp artifacts/rpm-package/*.rpm site/rpm/packages/ 2>/dev/null || true
          cd site/rpm && createrepo_c . && cd ../..

      - name: Upload Pages artifact
        uses: actions/upload-pages-artifact@v3
        with:
          path: site/

      - name: Deploy to GitHub Pages
        id: deploy
        uses: actions/deploy-pages@v4

  # ── Docker ──
  docker:
    name: Push Docker image
    runs-on: ubuntu-latest
    needs: [test]
    continue-on-error: true
    steps:
      - uses: actions/checkout@v4
      - uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}
      - uses: docker/build-push-action@v6
        with:
          push: true
          tags: |
            ghcr.io/skylarsimoncelli/pftui:${{ github.ref_name }}
            ghcr.io/skylarsimoncelli/pftui:latest
