name: CI

on:
  push:
  pull_request:
    branches: ["main", "master"]
  schedule:
    # Nightly stress soak - 03:17 UTC keeps it off the hour to avoid
    # contention with the GitHub Actions hourly stampede.
    - cron: "17 3 * * *"
  workflow_dispatch:

jobs:
  test:
    name: Test
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: recursive

      - name: Set up Go
        uses: actions/setup-go@v5
        with:
          go-version: "1.26.x"

      - name: Run tests
        run: go test -count=1 ./...

  coverage:
    name: Coverage
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: recursive

      - name: Set up Go
        uses: actions/setup-go@v5
        with:
          go-version: "1.26.x"

      - name: Run tests with coverage
        run: go test -count=1 ./... --cover

  race:
    name: Test (-race)
    runs-on: ubuntu-latest
    timeout-minutes: 20
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: recursive

      - name: Set up Go
        uses: actions/setup-go@v5
        with:
          go-version: "1.26.x"

      - name: Run tests with -race
        run: go test -count=1 -race ./...

  real-e2e:
    name: Real E2E (Providers x Transports)
    runs-on: ubuntu-latest
    timeout-minutes: 25
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: recursive

      - name: Set up Go
        uses: actions/setup-go@v5
        with:
          go-version: "1.26.x"

      - name: Install media tools
        run: sudo apt-get update && sudo apt-get install -y ffmpeg

      - name: Run real provider e2e matrix
        run: |
          go test -count=1 -v ./internal/e2e \
            -olcrtc.real-carriers=telemost,wbstream,jitsi \
            -run '^TestRealProviderTransportMatrix$' \
            -olcrtc.real-e2e

  stress-soak:
    name: Real E2E Stress Soak (Nightly)
    # Long-form stress over real carriers: only on schedule or manual
    # dispatch. Push and PR runs stay fast.
    if: github.event_name == 'schedule' || github.event_name == 'workflow_dispatch'
    runs-on: ubuntu-latest
    timeout-minutes: 90
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: recursive

      - name: Set up Go
        uses: actions/setup-go@v5
        with:
          go-version: "1.26.x"

      - name: Install media tools
        run: sudo apt-get update && sudo apt-get install -y ffmpeg

      - name: Run real stress soak
        run: |
          go test -count=1 -v ./internal/e2e \
            -run '^TestRealProviderTransportStress$' \
            -timeout=85m \
            -olcrtc.real-e2e \
            -olcrtc.stress \
            -olcrtc.real-carriers=telemost,wbstream,jitsi \
            -olcrtc.stress-bulk-duration=90s \
            -olcrtc.stress-duration=120s \
            -olcrtc.stress-echo-size=1024 \
            -olcrtc.stress-case-timeout=8m

  lint:
    name: Lint
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: recursive

      - name: Set up Go
        uses: actions/setup-go@v5
        with:
          go-version: "1.26.x"

      - name: golangci-lint
        run: |
          go install github.com/golangci/golangci-lint/v2/cmd/golangci-lint@latest
          $(go env GOPATH)/bin/golangci-lint run --timeout=5m

  build-cli:
    name: Build CLI (Binaries)
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: recursive

      - name: Set up Go
        uses: actions/setup-go@v5
        with:
          go-version: "1.26.x"

      - name: Install Mage
        run: go install github.com/magefile/mage@latest

      - name: Build CLI (Cross)
        run: mage cross

      - name: Upload CLI Artifacts
        uses: actions/upload-artifact@v4
        with:
          name: olcrtc-cli-binaries
          path: build/

  build-android:
    name: Build Android (AAR)
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: recursive

      - name: Set up Go
        uses: actions/setup-go@v5
        with:
          go-version: "1.26.x"

      - name: Set up JDK
        uses: actions/setup-java@v4
        with:
          distribution: "temurin"
          java-version: "17"

      - name: Install gomobile
        run: |
          go install golang.org/x/mobile/cmd/gomobile@latest
          gomobile init

      - name: Install Mage
        run: go install github.com/magefile/mage@latest

      - name: Build Mobile
        run: mage mobile

      - name: Upload Android Artifact
        uses: actions/upload-artifact@v4
        with:
          name: olcrtc-android
          path: build/olcrtc.aar
