name: Lint and tests

"on":
  push:
    branches:
      - main
  pull_request:
    branches:
      - '*'

env:
  COMPOSE_BAKE: true

jobs:

  lint-back:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v6
      - name: Create env files
        run: make create-env-files
      - name: Run linting checks
        run: make lint-back


  test-back:
    runs-on: ubuntu-latest
    env:
      DOCKER_USER: 1001
    steps:
      - name: Checkout repository
        uses: actions/checkout@v6
      - name: Create env files
        run: make create-env-files
      - name: Setup data directories
        run: |
          mkdir -p data/static
          chown -R 1001:1001 data
      - name: Collect static files
        run: make collectstatic
      - name: Create msg-imports bucket
        run: make import-bucket
      - name: Run backend tests
        run: make test-back

  test-front:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v6
      - name: Create env files
        run: make create-env-files
      - name: Install frontend dependencies
        run: make install-frozen-front
      - name: Run frontend tests
        run: make test-front

  test-e2e:
    runs-on: ubuntu-latest
    env:
      DOCKER_USER: 1001
      PW_TEST_HTML_REPORT_OPEN: 'never'
    steps:
      - name: Checkout repository
        uses: actions/checkout@v6
      - name: Create env files
        run: make create-env-files
      - name: Run E2E tests only for Chromium browser to speed up the tests
        run: make test-e2e-ci args="--project chromium"

  lint-front:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v6
      - name: Create env files
        run: make create-env-files
      - name: Install frontend dependencies
        run: make install-frozen-front
      - name: Run frontend linting
        run: make lint-front
      - name: Run frontend check
        run: make typecheck-front

  build-front:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v6
      - name: Create env files
        run: make create-env-files
      - name: Install frontend dependencies
        run: make install-frozen-front
      - name: Build frontend
        run: make build-front

  check-api-state:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v6
      - name: Create env files
        run: make create-env-files
      - name: Install frontend dependencies
        run: make install-frozen-front
      - name: Run API update
        run: make api-update
      - name: Check for changes
        run: |
          git diff --quiet || \
          (echo "API changes detected. Please run 'make api-update' then commit the changes." && exit 1)
