name: CI

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]
  workflow_dispatch:

concurrency:
  group: ci-${{ github.ref }}
  cancel-in-progress: true

jobs:
  code-formatting:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6

      - name: Setup JDK 21
        uses: actions/setup-java@v5
        with:
          java-version: 21
          distribution: zulu
          cache: 'gradle'

      - name: Check Spotless
        run: ./gradlew spotlessCheck

  db-migrations:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6

      - name: Setup JDK 21
        uses: actions/setup-java@v5
        with:
          java-version: 21
          distribution: zulu
          cache: 'gradle'

      - name: DB Migrations Check
        run: |
          ./gradlew generateCommonMainReaderDatabaseSchema;
          ./gradlew verifySqlDelightMigration

  common-tests:
    runs-on: macos-26
    steps:
      - uses: actions/checkout@v6

      - name: Setup JDK 21
        uses: actions/setup-java@v5
        with:
          java-version: 21
          distribution: zulu
          cache: 'gradle'

      - name: Common Tests
        run: ./gradlew :core:base:allTests :core:network:allTests :core:data:allTests

  android-tests:
    runs-on: ubuntu-latest
    timeout-minutes: 30
    steps:
      - uses: actions/checkout@v6

      - name: Enable KVM group perms
        run: |
          echo 'KERNEL=="kvm", GROUP="kvm", MODE="0666", OPTIONS+="static_node=kvm"' | sudo tee /etc/udev/rules.d/99-kvm4all.rules
          sudo udevadm control --reload-rules
          sudo udevadm trigger --name-match=kvm

      - name: Setup JDK 21
        uses: actions/setup-java@v5
        with:
          java-version: 21
          distribution: zulu
          cache: 'gradle'

      - name: Android Integration Tests
        uses: reactivecircus/android-emulator-runner@v2
        with:
          api-level: 31
          arch: x86_64
          disable-animations: true
          script: ./gradlew :shared:connectedAndroidTest
