name: CI

on: [push, pull_request]

permissions:
  contents: read

jobs:
  format:
    name: Format
    runs-on: ubuntu-22.04
    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          persist-credentials: false

      - name: Install OTP and Elixir
        uses: erlef/setup-beam@fc68ffb90438ef2936bbb3251622353b3dcb2f93 # v1.24.0
        with:
          otp-version: 28.1
          elixir-version: 1.18.4
          version-type: strict

      - name: Install dependencies
        run: mix deps.get

      - name: Check mix format
        run: mix format --check-formatted

  test:
    name: Test
    runs-on: ubuntu-22.04

    services:
      postgres:
        image: postgres:14
        env:
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: postgres
          POSTGRES_DB: postgres
        ports:
          - 5432:5432
        options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5

    strategy:
      fail-fast: false
      matrix:
        pair:
          # - erlang: maint
          #   elixir: main

          - erlang: 28.1
            elixir: 1.18.4
          - erlang: 27.2
            elixir: 1.17.3
          - erlang: 26.2
            elixir: 1.16.1
          - erlang: 25.3
            elixir: 1.15.7
          - erlang: 25.3
            elixir: 1.14.5
          - erlang: 25.3
            elixir: 1.13.4
          - erlang: 24.3
            elixir: 1.12.3

    env:
      HEXPM_OTP: OTP-28.1
      HEXPM_ELIXIR: v1.18.4
      HEXPM_BRANCH: main
      HEXPM_PATH: hexpm
      HEXPM_ELIXIR_PATH: hexpm_elixir
      HEXPM_OTP_PATH: hexpm_otp
      HEXPM_MIX_HOME: hexpm_mix
      HEXPM_MIX_ARCHIVES: hexpm_mix

    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          persist-credentials: false

      - name: Install OTP and Elixir
        uses: erlef/setup-beam@fc68ffb90438ef2936bbb3251622353b3dcb2f93 # v1.24.0
        with:
          otp-version: ${{matrix.pair.erlang}}
          elixir-version: ${{matrix.pair.elixir}}
          version-type: strict

      - name: Install OTP and Elixir for hexpm
        run: |
          wget https://repo.hex.pm/builds/otp/ubuntu-22.04/${HEXPM_OTP}.tar.gz
          mkdir -p ${HEXPM_OTP_PATH}
          tar -xf ${HEXPM_OTP}.tar.gz -C ${HEXPM_OTP_PATH} --strip-components=1
          ${HEXPM_OTP_PATH}/Install -minimal $(pwd)/${HEXPM_OTP_PATH}
          wget https://repo.hex.pm/builds/elixir/${HEXPM_ELIXIR}.zip
          unzip -d ${HEXPM_ELIXIR_PATH} ${HEXPM_ELIXIR}.zip
          mkdir -p ${HEXPM_MIX_HOME}
          PATH=$(pwd)/${HEXPM_ELIXIR_PATH}/bin:$(pwd)/${HEXPM_OTP_PATH}/bin:${PATH} MIX_HOME=$(pwd)/${HEXPM_MIX_HOME} MIX_ARCHIVES=$(pwd)/${HEXPM_MIX_HOME} mix local.hex --force
          PATH=$(pwd)/${HEXPM_ELIXIR_PATH}/bin:$(pwd)/${HEXPM_OTP_PATH}/bin:${PATH} MIX_HOME=$(pwd)/${HEXPM_MIX_HOME} MIX_ARCHIVES=$(pwd)/${HEXPM_MIX_HOME} mix local.rebar --force

      - name: Set up hexpm
        run: |
          git clone -b ${HEXPM_BRANCH} https://github.com/hexpm/hexpm.git hexpm
          cd hexpm; PATH=$(pwd)/../${HEXPM_ELIXIR_PATH}/bin:$(pwd)/../${HEXPM_OTP_PATH}/bin:${PATH} MIX_HOME=$(pwd)/../${HEXPM_MIX_HOME} MIX_ARCHIVES=$(pwd)/../${HEXPM_MIX_HOME} MIX_ENV=hex ../${HEXPM_ELIXIR_PATH}/bin/mix deps.get; cd ..
          cd hexpm; PATH=$(pwd)/../${HEXPM_ELIXIR_PATH}/bin:$(pwd)/../${HEXPM_OTP_PATH}/bin:${PATH} MIX_HOME=$(pwd)/../${HEXPM_MIX_HOME} MIX_ARCHIVES=$(pwd)/../${HEXPM_MIX_HOME} MIX_ENV=hex ../${HEXPM_ELIXIR_PATH}/bin/mix compile; cd ..

      - name: Install dependencies
        run: |
          sudo rm -rf /usr/local/bin/rebar3
          mix deps.get
          mix deps.compile

      - name: Run tests
        run: mix test
