name: Main merge
on:
  push:
    branches:
      - main

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

jobs:
  android_build_and_test:
    name: Android - Build and Test
    runs-on: ubuntu-22.04
    services:
      postgres:
        image: postgres:latest
        env:
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: postgres
          POSTGRES_DB: writeopia
        ports:
          - 5432:5432
        options: >-
          --health-cmd="pg_isready"
          --health-interval=10s
          --health-timeout=5s
          --health-retries=5
    env:
      isCI: "true"
      IN_MEMORY_DATABASE: 'true'
      DB_USER: 'postgres'
      WRITEOPIA_FIREBASE_ID: 'id'
      WRITEOPIA_CLIENT_BASE_URL: "baseurl"
      JWT_SECRET: "testingsecret"
    steps:
      - name: Check out code
        uses: actions/checkout@v4
      - name: Set up JDK 21
        uses: actions/setup-java@v4
        with:
          distribution: adopt
          java-version: '21'
      - name: Wait for Postgres to be healthy
        run: |
          for i in {1..10}; do
            pg_isready -h localhost -p 5432 && echo "Postgres is ready" && break
            echo "Waiting for postgres..." && sleep 5
          done
      - name: Run SQL init script
        run: |
          sudo apt-get install -y postgresql-client
          psql -h localhost -U postgres -d writeopia -f ./docker/postgres/init.sql
        env:
          PGPASSWORD: postgres
      - name: Main test
        run: ./gradlew assembleDebug test
  desktop_build_and_test:
    name: Desktop - Build, Test and Publish
    runs-on: macos-14
    env:
      isCI: "true"
    steps:
      - name: Check out code
        uses: actions/checkout@v4
      - name: Set up JDK 21
        uses: actions/setup-java@v4
        with:
          distribution: adopt
          java-version: '21'
      - name: import certs
        uses: apple-actions/import-codesign-certs@v3
        with:
          p12-file-base64: ${{ secrets.CERTIFICATES_P12 }}
          p12-password: ${{ secrets.CERTIFICATES_P12_PASSWORD }}
      - name: Generate signed mac app
        run: ./gradlew packageReleaseDmg
        env:
          SIGN_IDENTITY: ${{ secrets.SIGN_IDENTITY }}
      - name: Main test
        run: ./gradlew jvmTest
  web_build_and_publish:
    name: Web - Build and Publish
    runs-on: ubuntu-22.04
    env:
      isCI: "true"
    steps:
      - name: Check out code
        uses: actions/checkout@v4
      - name: Set up JDK 21
        uses: actions/setup-java@v4
        with:
          distribution: adopt
          java-version: '21'
      - name: Build development webpack
        run: ./gradlew jsBrowserDevelopmentWebpack
      - name: Upload JS webpack
        uses: actions/upload-artifact@v4
        with:
          name: writeopia-js-webpack
          path: ./application/web/build/kotlin-webpack/js/developmentExecutable
          if-no-files-found: error
          retention-days: 10
          overwrite: true
