# Example GitHub Actions workflow for camera integration tests
# Save as .github/workflows/camera-tests.yml

name: Camera Integration Tests

on:
  # Run on manual trigger
  workflow_dispatch:
    inputs:
      camera_endpoint:
        description: 'Camera ONVIF endpoint'
        required: true
        default: 'http://192.168.1.201/onvif/device_service'
      camera_username:
        description: 'Camera username'
        required: true
        default: 'service'

  # Or run on schedule (daily at 2 AM)
  schedule:
    - cron: '0 2 * * *'

jobs:
  test-bosch-flexidome:
    name: Test Bosch FLEXIDOME
    runs-on: ubuntu-latest
    
    # Only run if secrets are configured
    if: ${{ secrets.ONVIF_TEST_PASSWORD != '' }}
    
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Set up Go
        uses: actions/setup-go@v4
        with:
          go-version: '1.21'

      - name: Cache Go modules
        uses: actions/cache@v3
        with:
          path: |
            ~/.cache/go-build
            ~/go/pkg/mod
          key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
          restore-keys: |
            ${{ runner.os }}-go-

      - name: Download dependencies
        run: go mod download

      - name: Run Bosch FLEXIDOME tests
        env:
          ONVIF_TEST_ENDPOINT: ${{ github.event.inputs.camera_endpoint || secrets.ONVIF_TEST_ENDPOINT }}
          ONVIF_TEST_USERNAME: ${{ github.event.inputs.camera_username || secrets.ONVIF_TEST_USERNAME }}
          ONVIF_TEST_PASSWORD: ${{ secrets.ONVIF_TEST_PASSWORD }}
        run: |
          echo "Testing camera at: $ONVIF_TEST_ENDPOINT"
          go test -v -run TestBoschFLEXIDOMEIndoor5100iIR -timeout 5m

      - name: Run benchmarks
        if: success()
        env:
          ONVIF_TEST_ENDPOINT: ${{ github.event.inputs.camera_endpoint || secrets.ONVIF_TEST_ENDPOINT }}
          ONVIF_TEST_USERNAME: ${{ github.event.inputs.camera_username || secrets.ONVIF_TEST_USERNAME }}
          ONVIF_TEST_PASSWORD: ${{ secrets.ONVIF_TEST_PASSWORD }}
        run: |
          go test -bench=BenchmarkBoschFLEXIDOMEIndoor5100iIR -benchmem -run=^$ | tee benchmark.txt

      - name: Upload benchmark results
        if: success()
        uses: actions/upload-artifact@v3
        with:
          name: benchmark-results
          path: benchmark.txt

      - name: Generate test coverage
        if: success()
        env:
          ONVIF_TEST_ENDPOINT: ${{ github.event.inputs.camera_endpoint || secrets.ONVIF_TEST_ENDPOINT }}
          ONVIF_TEST_USERNAME: ${{ github.event.inputs.camera_username || secrets.ONVIF_TEST_USERNAME }}
          ONVIF_TEST_PASSWORD: ${{ secrets.ONVIF_TEST_PASSWORD }}
        run: |
          go test -coverprofile=coverage.out -run TestBoschFLEXIDOMEIndoor5100iIR
          go tool cover -html=coverage.out -o coverage.html

      - name: Upload coverage report
        if: success()
        uses: actions/upload-artifact@v3
        with:
          name: coverage-report
          path: coverage.html

      - name: Comment test results
        if: always() && github.event_name == 'workflow_dispatch'
        uses: actions/github-script@v6
        with:
          script: |
            const outcome = '${{ job.status }}' === 'success' ? '✅ PASSED' : '❌ FAILED';
            console.log(`Camera integration tests: ${outcome}`);

# Configuration Instructions:
# 
# 1. Add secrets to your GitHub repository:
#    - Go to Settings > Secrets and variables > Actions
#    - Add the following secrets:
#      * ONVIF_TEST_ENDPOINT (camera URL)
#      * ONVIF_TEST_USERNAME (camera username)
#      * ONVIF_TEST_PASSWORD (camera password)
#
# 2. Ensure your GitHub Actions runner can reach the camera:
#    - Use self-hosted runner on same network as camera
#    - Or use VPN/tunnel to access camera from GitHub-hosted runner
#
# 3. Run manually:
#    - Go to Actions tab
#    - Select "Camera Integration Tests"
#    - Click "Run workflow"
#    - Optionally override endpoint/username
