name: Register Prefect Flows

on:
  workflow_dispatch:
    inputs:
      flow_file:
        description: 'Flow file to register (e.g., flows/my_flow.py or bdd/flows/permit.py)'
        required: true
        type: string
      project_name:
        description: 'Prefect Project name'
        required: true
        default: ''
      git_ref:
        description: 'Git reference (branch or commit) for the flow'
        required: true
        default: 'main'
      labels:
        description: 'Labels for the flow (comma-separated)'
        required: false
        default: 'ea-data-manufacturing'

jobs:
  register-flows:
    runs-on: enverus-ubuntu
    
    steps:
    - name: Checkout code
      uses: actions/checkout@v4
      with:
          fetch-depth: 0
          token: ${{ secrets.PAT }}
      
    - name: Set up Python
      uses: actions/setup-python@v4
      with:
        python-version: '3.8'
        
    - name: Install dependencies
      run: |
        pip install -r requirements.txt
        
    - name: Register specific flow
      env:
        PREFECT_SERVER_URL: https://ea-prefectv1-apollo-dev.int.enverus.com/graphql
        PREFECT__CLOUD__API: https://ea-prefectv1-apollo-dev.int.enverus.com/graphql
        PREFECT__BACKEND: server
        PREFECT__CLOUD__AUTH_TOKEN: ""
        PROJECT_NAME: ${{ github.event.inputs.project_name }}
        GIT_REF: ${{ github.event.inputs.git_ref }}
        FLOW_FILE: ${{ github.event.inputs.flow_file }}
        LABELS: ${{ github.event.inputs.labels }}
        GITHUB_ACCESS_TOKEN: ${{ secrets.PAT }}
      run: |
        # Convert comma-separated labels to multiple --labels arguments
        LABEL_ARGS=""
        if [ -n "$LABELS" ]; then
          IFS=',' read -ra LABEL_ARRAY <<< "$LABELS"
          for label in "${LABEL_ARRAY[@]}"; do
            # Trim whitespace and add --labels flag for each label
            trimmed_label=$(echo "$label" | xargs)
            LABEL_ARGS="$LABEL_ARGS --labels \"$trimmed_label\""
          done
        fi
        
        # Use eval to properly handle the quoted arguments
        eval "python utilities/register-flow.py single \
          --project \"$PROJECT_NAME\" \
          --file \"$FLOW_FILE\" \
          --server-url \"$PREFECT_SERVER_URL\" \
          --storage-type \"git\" \
          --git-repo \"enverus-ea/ea.data.mfg.prefectV1\" \
          --git-ref \"$GIT_REF\" \
          $LABEL_ARGS \
          --image \"392865356492.dkr.ecr.us-east-1.amazonaws.com/eashared-prefectv1-dev:pf-12176ef\""

  validate-flow:
    runs-on: ubuntu-latest
    
    steps:
    - name: Checkout code
      uses: actions/checkout@v4
      
    - name: Set up Python
      uses: actions/setup-python@v4
      with:
        python-version: '3.8'
        
    - name: Install dependencies
      run: |
        pip install prefect==1.4.1 click pyyaml
        
    - name: Validate flow syntax
      run: |
        FLOW_FILE="${{ github.event.inputs.flow_file }}"
        if [ -f "$FLOW_FILE" ]; then
          echo "Validating $FLOW_FILE"
          python -m py_compile "$FLOW_FILE"
          echo "✅ Flow syntax is valid"
        else
          echo "❌ Flow file not found: $FLOW_FILE"
          exit 1
        fi
