name: Generate CRDs

on:
  workflow_dispatch:
  schedule:
    - cron: "0 3 * * 1"

jobs:
  generate-crds:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repo
        uses: actions/checkout@v4
        with:
          # VITAL: This token makes the push trigger the NEXT workflow
          token: ${{ secrets.GH_PAT }}

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'

      - name: Install dependencies
        run: |
          npm install -g typescript
          npm install

      - name: Fetch binaries & Generate
        run: |
          # (Simplified for brevity - keep your existing binary fetch logic here)
          CRD2PULUMI_VERSION=1.6.0
          curl -L "https://github.com/pulumi/crd2pulumi/releases/download/v${CRD2PULUMI_VERSION}/crd2pulumi-v${CRD2PULUMI_VERSION}-linux-amd64.tar.gz" -o crd2pulumi.tar.gz
          tar -xzf crd2pulumi.tar.gz
          chmod +x crd2pulumi
          sudo mv crd2pulumi /usr/local/bin/crd2pulumi
          
          node generate-crds.js

      - name: Check for changes
        id: git-check
        run: |
          git diff --exit-code || echo "changes=true" >> $GITHUB_OUTPUT

      - name: Bump Version and Push
        if: steps.git-check.outputs.changes == 'true'
        run: |
          git config --global user.name "github-actions[bot]"
          git config --global user.email "github-actions[bot]@users.noreply.github.com"
          
          # Bump version in package.json
          npm version patch --no-git-tag-version
          
          git add .
          git commit -m "Update CRDs and bump version"
          
          # This push (authenticated by GH_PAT) will trigger the publish.yml workflow
          git push