name: CI

on:
  workflow_dispatch: {}
  workflow_call:

permissions:
  contents: read

env:
  MIX_ENV: test

jobs:
  ci:
    # Service containers to run with `container-job`
    services:
      # Label used to access the service container
      postgres:
        # Docker Hub image
        image: postgres
        # Provide the password for postgres
        env:
          POSTGRES_PASSWORD: postgres
        # Set health checks to wait until postgres has started
        options: >-
          --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
        ports:
          - 5432:5432

    name: Run CI on OTP ${{matrix.otp}} / Elixir ${{matrix.elixir}}
    runs-on: ubuntu-latest

    strategy:
      matrix:
        otp: ["27.3"]
        elixir: ["1.18.3"]

    steps:
      - name: Set up Elixir
        uses: erlef/setup-beam@v1
        with:
          elixir-version: ${{matrix.elixir}}
          otp-version: ${{matrix.otp}}

      - name: Checkout code
        uses: actions/checkout@v5

      # Step: Define how to cache deps. Restores existing cache if present.
      - name: Cache deps
        id: cache-deps
        uses: actions/cache@v4
        env:
          cache-name: cache-spazio-solazzo-deps
        with:
          path: spazio-solazzo/deps
          key: ${{ runner.os }}-${{matrix.otp}}-${{matrix.elixir}}-${{ env.cache-name }}-${{ hashFiles('spazio-solazzo/**/mix.lock') }}
          restore-keys: |
            ${{ runner.os }}-${{matrix.otp}}-${{matrix.elixir}}-${{ env.cache-name }}-

      # Step: Define how to cache the `_build` directory. After the first run,
      # this speeds up tests runs a lot. This includes not re-compiling our
      # project's downloaded deps every run.
      - name: Cache compiled build
        id: cache-build
        uses: actions/cache@v4
        env:
          cache-name: cache-spazio-solazzo-compiled-build
        with:
          path: spazio-solazzo/_build
          key: ${{ runner.os }}-${{matrix.otp}}-${{matrix.elixir}}-${{ env.cache-name }}-${{ hashFiles('spazio-solazzo/**/mix.lock') }}
          restore-keys: |
            ${{ runner.os }}-${{matrix.otp}}-${{matrix.elixir}}-${{ env.cache-name }}
            ${{ runner.os }}-${{matrix.otp}}-${{matrix.elixir}}-

      - name: Install dependencies
        run: mix deps.get

      # Step: Compile the project treating any warnings as errors.
      - name: Compiles without warnings
        run: mix compile --warnings-as-errors

      # Step: Check that the checked in code has already been formatted.
      # This step fails if something was found unformatted.
      - name: Check Formatting
        run: mix format --check-formatted

      # Step: Execute Credo
      - name: Run Credo
        run: mix credo

      # Step: Execute the tests.
      - name: Run tests
        run: mix test
