# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-nodejs

name: Generate ECS definition package

env:
  NODE_VERSION: '22.x'

on:
  workflow_dispatch:
    inputs:
      ecsRef:
        description: 'ECS Ref (version tag like v8.11.0 or branch name)'
        required: false
        default: 'main'
        type: string
  repository_dispatch:
    types: [ecs-release]

jobs:
  generate:
    runs-on: ubuntu-latest
    permissions:
      contents: write
      pull-requests: write

    steps:
    - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
      with:
        fetch-depth: 0

    - name: Setup Node.js
      uses: actions/setup-node@6044e13b5dc448c55e2357c09f80417699197238 # v6.2.0
      with:
        node-version: ${{ env.NODE_VERSION }}
        cache: 'yarn'
    
    - name: Install dependencies
      run: yarn install --frozen-lockfile
    
    - name: Determine ECS version
      id: ecs_version
      run: |
        # Use input if provided, otherwise use payload from repository_dispatch
        if [ "${{ github.event_name }}" = "repository_dispatch" ]; then
          ECS_REF="${{ github.event.client_payload.version }}"
        else
          ECS_REF="${{ inputs.ecsRef }}"
        fi
        
        echo "ecs_ref=${ECS_REF}" >> $GITHUB_OUTPUT
        
        # Extract version number (remove 'v' prefix if present)
        VERSION="${ECS_REF#v}"
        echo "version=${VERSION}" >> $GITHUB_OUTPUT
        echo "ECS Reference: ${ECS_REF}"
        echo "Version: ${VERSION}"
    
    - name: Update package.json version
      run: |
        VERSION="${{ steps.ecs_version.outputs.version }}"
        # Update version in package.json
        npm version "${VERSION}" --no-git-tag-version --allow-same-version
        echo "Updated package.json to version ${VERSION}"
    
    - name: Generate ECS types
      run: yarn generate-ecs-types -r ${{ steps.ecs_version.outputs.ecs_ref }}
    
    - name: Run tests
      run: yarn test:integration
    
    - name: Build
      run: yarn build
    
    - name: Create Pull Request
      id: cpr
      uses: peter-evans/create-pull-request@c0f553fe549906ede9cf27b5156039d195d2ece0 # v8.1.0
      with:
        commit-message: 'Update to ECS ${{ steps.ecs_version.outputs.version }}'
        branch: ecs-update-${{ steps.ecs_version.outputs.version }}
        delete-branch: true
        title: 'Update to ECS ${{ steps.ecs_version.outputs.version }}'
        body: |
          ## Automated ECS Update
          
          This PR updates the ECS TypeScript definitions to version **${{ steps.ecs_version.outputs.version }}**.
          
          **ECS Reference:** `${{ steps.ecs_version.outputs.ecs_ref }}`
          **Triggered by:** ${{ github.event_name }}
          
          ### Changes
          - Updated package.json version to ${{ steps.ecs_version.outputs.version }}
          - Regenerated TypeScript definitions from ECS schema
          - Tests passed
          - Build succeeded
          
          ### Next Steps
          After merging this PR, the `publish.yml` workflow will automatically:
          1. Create a git tag (v${{ steps.ecs_version.outputs.version }})
          2. Publish to npm registry
          3. Create a GitHub release
        labels: |
          automated
          enhancement
          Team:obs-exploration
