---
name: Release Binaries on Tag

on:
  push:
    tags:
      - v*
  workflow_dispatch:

env:
  RUSTFLAGS: "--deny warnings"

jobs:
  format:
    name: Rust Format Check
    runs-on: ubuntu-latest

    permissions:
      contents: read
      pull-requests: read

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

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

      - name: Format Check
        uses: mbrobbel/rustfmt-check@master
        with:
          token: ${{ secrets.GITHUB_TOKEN }}

  clippy:
    name: Rust Clippy Check
    runs-on: ubuntu-latest

    permissions:
      contents: read
      security-events: write

    steps:
      - name: Checkout code
        uses: actions/checkout@v6

      - name: Rust Toolchain setup
        uses: dtolnay/rust-toolchain@stable
        with:
          components: cargo, clippy

      - name: Run rust-clippy
        run: cargo clippy --all-features
        continue-on-error: true

  upload:
    name: Deploy to the Production Environment
    runs-on: ubuntu-latest
    needs: [format, clippy]

    permissions:
      contents: read

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

      - name: Rust Toolchain setup
        uses: dtolnay/rust-toolchain@stable
        with:
          target: wasm32-unknown-unknown

      - name: Build rust code
        run: cargo build

      - name: Setup Node
        uses: actions/setup-node@v6

      - name: Wrangler setup
        run: npm install -g wrangler

      - name: Build wasm and deploy
        env:
          CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
        run: |
          wrangler versions upload --name=cookie-worker --tag="$GITHUB_REF_NAME"
          export WORKER_VERSION=$(wrangler versions list --name=cookie-worker --json | jq -r '.[-1].id')
          echo "cookie-worker version $GITHUB_REF_NAME uploaded as ${WORKER_VERSION}, to deploy this version run:\n \`wrangler versions deploy --name=cookie-worker --version-id=${WORKER_VERSION} --yes\`"

  publish:
    name: Publish to crates.io
    runs-on: ubuntu-latest
    needs: [format, clippy]

    permissions:
      contents: read

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

      - name: Rust Toolchain setup
        uses: dtolnay/rust-toolchain@stable

      - name: Publish to crates.io
        run: cargo publish --token ${{ secrets.CARGO_REGISTRY_TOKEN }}

  release:
    name: Tag Release Package
    runs-on: ubuntu-latest
    needs: [format, clippy, upload, publish]
    permissions:
      contents: write
      pull-requests: read

    steps:
      - name: Checkout
        uses: actions/checkout@v6
        with:
          fetch-depth: 0

      - name: Generate a changelog
        uses: orhun/git-cliff-action@v4
        id: git-cliff
        with:
          config: cliff.toml
        env:
          OUTPUT: CHANGELOG.md
          GITHUB_REPO: ${{ github.repository }}
          args: --verbose
          github-token: ${{ secrets.GH_PAT }}

      - name: Create CLI Release
        uses: softprops/action-gh-release@v2
        with:
          body_path: CHANGELOG.md
          name: "Release ${{ github.ref_name }}"
          files: |
            LICENSE
