name: Build and test

on:
  pull_request:
    branches:
      - main

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

jobs:
  kt_lint:
    name: Ktlint
    runs-on: ubuntu-22.04
    env:
      isCI: "true"
      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: Ktlint
        run: ./gradlew ktlintCheck --no-daemon
  ui_jvm_tests:
    name: Jvm UI Compose tests
    runs-on: ubuntu-22.04
    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: Generate signed mac app
        run: ./gradlew packageReleaseDmg
        env:
          SIGN_IDENTITY: ${{ secrets.SIGN_IDENTITY }}
      - name: JVM tests
        run: ./gradlew jvmTest
      - name: upload results
        uses: actions/upload-artifact@v4
        if: failure()
        with:
          name: test-results-jvm
          path: ./application/desktopApp/build/reports/
  general_build:
    name: General - Build debug
    runs-on: ubuntu-22.04
    services:
      postgres:
        image: postgres:latest
        env:
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: postgres
          POSTGRES_DB: writeopia
          JWT_SECRET: "testingsecret"
        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: Build debug
        run: ./gradlew assembleDebug test --no-daemon
  ios_build:
    name: iOS build
    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 debug
        run: ./gradlew compileKotlinIosArm64 --no-daemon
  backend_build_and_test:
    name: Backend - build
    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: Build backend
        run: ./gradlew :backend:gateway:test --no-daemon
      - name: upload results
        uses: actions/upload-artifact@v4
        if: failure()
        with:
          name: test-results-backend
          path: backend/gateway/build/
  web_build:
    name: Web - Build debug
    runs-on: ubuntu-22.04
    env:
      isCI: "true"
      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: Build web debug
        run: ./gradlew jsBrowserDevelopmentWebpack --no-daemon
  test_docusaurus:
    name: Test docusaurus
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 20
          cache: npm
          cache-dependency-path: documentation/writeopia_docs/package-lock.json
      - name: Install dependencies
        working-directory: ./documentation/writeopia_docs
        run: npm ci
      - name: Test build website
        working-directory: ./documentation/writeopia_docs
        run: npm run build


