name: Claude Check Workflows

on:
  schedule:
    # Daily at 13:00 UTC (5 AM PST / 6 AM PDT)
    - cron: "0 13 * * *"
  workflow_dispatch:
    inputs:
      hours:
        description: "Number of hours to look back for workflow runs"
        required: false
        default: "24"
        type: string

jobs:
  check-workflows:
    environment: ai-bots
    runs-on:
      - self-hosted
      - macOS
      - ARM64
    permissions:
      contents: read # checkout repository
      issues: write # create issues for workflow failures
    steps:
      - name: Create GitHub App token
        id: app-token
        uses: actions/create-github-app-token@v2
        with:
          app-id: ${{ vars.DYAD_GITHUB_APP_ID }}
          private-key: ${{ secrets.DYAD_GITHUB_APP_PRIVATE_KEY }}
          permission-actions: read
          permission-issues: write

      - name: Checkout repository
        uses: actions/checkout@v5

      - name: Check workflow health
        uses: anthropics/claude-code-action@v1
        env:
          GH_TOKEN: ${{ steps.app-token.outputs.token }}
          CLAUDE_CODE_MAX_OUTPUT_TOKENS: 48000
        with:
          claude_code_oauth_token: ${{ secrets.CLAUDE_CODE_OAUTH_TOKEN }}
          github_token: ${{ steps.app-token.outputs.token }}
          claude_args: --model claude-sonnet-4-5-20250929
          display_report: true
          prompt: |
            /dyad:check-workflows ${{ inputs.hours || '24' }}
      - name: Cleanup (self-hosted macOS)
        if: always()
        run: bash scripts/ci-cleanup-macos.sh
