---
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: 2025 Open Networking Foundation Contributors

name: BBSim Tests Example

on:
  # Run on push to main branches
  push:
    branches:
      - master
      - "voltha-*"

  # Run on pull requests
  pull_request:
    branches:
      - master
      - "voltha-*"

  # Allow manual triggering with custom parameters
  workflow_dispatch:
    inputs:
      branch:
        description: "Branch to test"
        required: true
        default: "master"
        type: choice
        options:
          - master
          - voltha-2.15
      workflow_type:
        description: "Workflow to test"
        required: true
        default: "all"
        type: choice
        options:
          - all
          - dt
          - att
          - tt
      log_level:
        description: "Log level"
        required: false
        default: "WARN"
        type: choice
        options:
          - DEBUG
          - INFO
          - WARN
          - ERROR
      with_monitoring:
        description: "Enable monitoring"
        required: false
        default: false
        type: boolean

  # Run nightly
  schedule:
    - cron: "0 2 * * *" # 2 AM UTC daily

jobs:
  # -----------------------------------------------------------------------
  # Job: DT Workflow Tests
  # -----------------------------------------------------------------------
  dt-tests:
    name: DT Workflow Tests
    runs-on: ubuntu-latest
    timeout-minutes: 240
    if: |
      github.event_name == 'schedule' ||
      (github.event_name == 'workflow_dispatch' &&
       (github.event.inputs.workflow_type == 'dt' || github.event.inputs.workflow_type == 'all')) ||
      github.event_name == 'push' ||
      github.event_name == 'pull_request'

    steps:
      - name: Free up disk space
        run: |
          echo "Disk space before cleanup:"
          df -h

          # Remove unnecessary software
          sudo rm -rf /usr/share/dotnet
          sudo rm -rf /opt/ghc
          sudo rm -rf /usr/local/share/boost
          sudo rm -rf "$AGENT_TOOLSDIRECTORY"

          echo "Disk space after cleanup:"
          df -h

      - name: Run DT BBSim Tests
        uses: opencord/shared-workflows/.github/actions/bbsim-tests@main
        with:
          branch: ${{ github.event.inputs.branch || 'master' }}
          log-level: ${{ github.event.inputs.log_level || 'WARN' }}
          with-monitoring: ${{ github.event.inputs.with_monitoring || false }}
          test-targets: |
            - target: functional-single-kind-dt
              workflow: dt
              flags: ""
              teardown: true
              logging: true
              vgcEnabled: false

      - name: Upload test results
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: dt-test-results-${{ github.run_id }}
          path: logs/
          retention-days: 30

  # -----------------------------------------------------------------------
  # Job: ATT Workflow Tests
  # -----------------------------------------------------------------------
  att-tests:
    name: ATT Workflow Tests
    runs-on: ubuntu-latest
    timeout-minutes: 240
    if: |
      github.event_name == 'schedule' ||
      (github.event_name == 'workflow_dispatch' &&
       (github.event.inputs.workflow_type == 'att' || github.event.inputs.workflow_type == 'all'))

    steps:
      - name: Free up disk space
        run: |
          echo "Disk space before cleanup:"
          df -h

          sudo rm -rf /usr/share/dotnet
          sudo rm -rf /opt/ghc
          sudo rm -rf /usr/local/share/boost
          sudo rm -rf "$AGENT_TOOLSDIRECTORY"

          echo "Disk space after cleanup:"
          df -h

      - name: Run ATT BBSim Tests
        uses: opencord/shared-workflows/.github/actions/bbsim-tests@main
        with:
          branch: ${{ github.event.inputs.branch || 'master' }}
          log-level: ${{ github.event.inputs.log_level || 'WARN' }}
          with-monitoring: ${{ github.event.inputs.with_monitoring || false }}
          test-targets: |
            - target: functional-single-kind-att
              workflow: att
              flags: ""
              teardown: true
              logging: true
              vgcEnabled: false

      - name: Upload test results
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: att-test-results-${{ github.run_id }}
          path: logs/
          retention-days: 30

  # -----------------------------------------------------------------------
  # Job: TT Workflow Tests
  # -----------------------------------------------------------------------
  tt-tests:
    name: TT Workflow Tests
    runs-on: ubuntu-latest
    timeout-minutes: 240
    if: |
      github.event_name == 'schedule' ||
      (github.event_name == 'workflow_dispatch' &&
       (github.event.inputs.workflow_type == 'tt' || github.event.inputs.workflow_type == 'all'))

    steps:
      - name: Free up disk space
        run: |
          echo "Disk space before cleanup:"
          df -h

          sudo rm -rf /usr/share/dotnet
          sudo rm -rf /opt/ghc
          sudo rm -rf /usr/local/share/boost
          sudo rm -rf "$AGENT_TOOLSDIRECTORY"

          echo "Disk space after cleanup:"
          df -h

      - name: Run TT BBSim Tests
        uses: opencord/shared-workflows/.github/actions/bbsim-tests@main
        with:
          branch: ${{ github.event.inputs.branch || 'master' }}
          log-level: ${{ github.event.inputs.log_level || 'WARN' }}
          with-monitoring: ${{ github.event.inputs.with_monitoring || false }}
          test-targets: |
            - target: functional-single-kind-tt
              workflow: tt
              flags: ""
              teardown: true
              logging: true
              vgcEnabled: false

      - name: Upload test results
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: tt-test-results-${{ github.run_id }}
          path: logs/
          retention-days: 30

  # -----------------------------------------------------------------------
  # Job: Multi-workflow Tests (run multiple workflows in sequence)
  # -----------------------------------------------------------------------
  multi-workflow-tests:
    name: Multi-Workflow Tests
    runs-on: ubuntu-latest
    timeout-minutes: 480
    if: github.event_name == 'workflow_dispatch' && github.event.inputs.workflow_type == 'all'

    steps:
      - name: Free up disk space
        run: |
          echo "Disk space before cleanup:"
          df -h

          sudo rm -rf /usr/share/dotnet
          sudo rm -rf /opt/ghc
          sudo rm -rf /usr/local/share/boost
          sudo rm -rf "$AGENT_TOOLSDIRECTORY"

          echo "Disk space after cleanup:"
          df -h

      - name: Run All Workflow Tests
        uses: opencord/shared-workflows/.github/actions/bbsim-tests@main
        with:
          branch: ${{ github.event.inputs.branch || 'master' }}
          log-level: ${{ github.event.inputs.log_level || 'INFO' }}
          with-monitoring: true
          olts: "2"
          test-targets: |
            - target: sanity-single-kind
              workflow: dt
              flags: ""
              teardown: true
              logging: true
              vgcEnabled: false
            - target: functional-single-kind-dt
              workflow: dt
              flags: ""
              teardown: true
              logging: true
              vgcEnabled: false
            - target: functional-single-kind-att
              workflow: att
              flags: ""
              teardown: true
              logging: true
              vgcEnabled: false
            - target: functional-single-kind-tt
              workflow: tt
              flags: ""
              teardown: true
              logging: true
              vgcEnabled: false

      - name: Upload test results
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: multi-workflow-test-results-${{ github.run_id }}
          path: logs/
          retention-days: 30

  # -----------------------------------------------------------------------
  # Job: Test Gerrit Patch
  # -----------------------------------------------------------------------
  test-patch:
    name: Test Gerrit Patch
    runs-on: ubuntu-latest
    timeout-minutes: 240
    # Only run when triggered manually with patch information
    if: |
      github.event_name == 'workflow_dispatch' &&
      github.event.inputs.gerrit_project != '' &&
      github.event.inputs.gerrit_refspec != ''

    steps:
      - name: Free up disk space
        run: |
          sudo rm -rf /usr/share/dotnet
          sudo rm -rf /opt/ghc
          sudo rm -rf /usr/local/share/boost
          sudo rm -rf "$AGENT_TOOLSDIRECTORY"

      - name: Test Patch with BBSim
        uses: opencord/shared-workflows/.github/actions/bbsim-tests@main
        with:
          branch: ${{ github.event.inputs.branch || 'master' }}
          gerrit-project: ${{ github.event.inputs.gerrit_project }}
          gerrit-refspec: ${{ github.event.inputs.gerrit_refspec }}
          log-level: DEBUG
          test-targets: |
            - target: sanity-single-kind
              workflow: dt
              flags: ""
              teardown: true
              logging: true
              vgcEnabled: false

      - name: Upload test results
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: patch-test-results-${{ github.run_id }}
          path: logs/
          retention-days: 30

  # -----------------------------------------------------------------------
  # Job: Report Results
  # -----------------------------------------------------------------------
  report:
    name: Test Results Summary
    runs-on: ubuntu-latest
    needs: [dt-tests, att-tests, tt-tests]
    if: always()

    steps:
      - name: Download all artifacts
        uses: actions/download-artifact@v4
        with:
          path: all-results

      - name: Generate summary report
        run: |
          echo "# BBSim Test Results Summary" >> $GITHUB_STEP_SUMMARY
          echo "" >> $GITHUB_STEP_SUMMARY
          echo "## Test Execution" >> $GITHUB_STEP_SUMMARY
          echo "" >> $GITHUB_STEP_SUMMARY
          echo "| Job | Status |" >> $GITHUB_STEP_SUMMARY
          echo "|-----|--------|" >> $GITHUB_STEP_SUMMARY
          echo "| DT Tests | ${{ needs.dt-tests.result }} |" >> $GITHUB_STEP_SUMMARY
          echo "| ATT Tests | ${{ needs.att-tests.result }} |" >> $GITHUB_STEP_SUMMARY
          echo "| TT Tests | ${{ needs.tt-tests.result }} |" >> $GITHUB_STEP_SUMMARY
          echo "" >> $GITHUB_STEP_SUMMARY

          # List available artifacts
          echo "## Artifacts" >> $GITHUB_STEP_SUMMARY
          echo "" >> $GITHUB_STEP_SUMMARY
          if [ -d "all-results" ]; then
            find all-results -type f -name "*.html" | while read file; do
              echo "- $(basename $file)" >> $GITHUB_STEP_SUMMARY
            done
          fi

      - name: Check overall status
        run: |
          if [[ "${{ needs.dt-tests.result }}" != "success" ]] || \
             [[ "${{ needs.att-tests.result }}" != "success" && "${{ needs.att-tests.result }}" != "skipped" ]] || \
             [[ "${{ needs.tt-tests.result }}" != "success" && "${{ needs.tt-tests.result }}" != "skipped" ]]; then
            echo "One or more test jobs failed"
            exit 1
          fi
          echo "All executed tests passed successfully"
