name: Check Segment Editor Plugin

on:
  schedule:
    - cron: "0 6 * * *" # Daily at 06:00 UTC
  workflow_dispatch: # Allow manual trigger

jobs:
  check-and-update:
    runs-on: ubuntu-latest
    permissions:
      contents: write

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          token: ${{ secrets.GITHUB_TOKEN }}

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: "lts/*"

      - name: Check for new version and update manifest
        id: update
        run: node .github/scripts/check-segment-editor.js
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Validate JSON files
        if: steps.update.outputs.updated == 'true'
        run: python .github/scripts/validate-json.py

      - name: Commit and push
        if: success() && steps.update.outputs.updated == 'true'
        run: |
          git config --global user.name "github-actions[bot]"
          git config --global user.email "github-actions[bot]@users.noreply.github.com"

          VERSION="${{ steps.update.outputs.version }}"
          git add .

          if git diff --staged --quiet; then
            echo "No changes to commit."
            exit 0
          fi

          git commit -m "Update Segment Editor to v${VERSION}"
          git push
