name: ci-cd

on:
    pull_request:
        branches:
            - 'dev'
    push:
        branches:
            - 'dev'

permissions:
    contents: read
    packages: read

jobs:
    npm-clean-install:
        runs-on: builder
        container:
            image: node:24
            options: --user 1000

        steps:
            - name: Check-out
              uses: actions/checkout@v7
              with:
                  fetch-depth: 1

            - name: Install
              uses: autonomous-testing/npm-install-action@v3
              with:
                  npm-token: ${{ github.token }}

    static-check:
        needs: [npm-clean-install]
        if: github.event_name == 'pull_request'
        runs-on: builder
        container:
            image: node:24
            options: --user 1000

        steps:
            - name: Check-out
              uses: actions/checkout@v7
              with:
                  fetch-depth: 0

            - name: Install
              uses: autonomous-testing/npm-install-action@v3
              with:
                  npm-token: ${{ github.token }}

            - name: Commitlint
              if: ${{ always() }}
              shell: bash
              run: |
                  npx commitlint \
                    --from ${{ github.event.pull_request.base.sha }} \
                    --to ${{ github.event.pull_request.head.sha }} \
                    --verbose

            - name: Eslint
              if: ${{ always() }}
              shell: bash
              run: make eslint

            - name: Prettier
              if: ${{ always() }}
              shell: bash
              run: make prettier.check

            - name: Audit (production dependencies)
              if: ${{ always() }}
              shell: bash
              run: npm audit --omit=dev --audit-level=high

    before-build-test:
        needs: [npm-clean-install]
        runs-on: builder
        container:
            image: node:24
            options: --user 1000

        steps:
            - name: Check-out
              uses: actions/checkout@v7
              with:
                  fetch-depth: 1

            - name: Install
              uses: autonomous-testing/npm-install-action@v3
              with:
                  npm-token: ${{ github.token }}

            - name: Prepare .env
              shell: bash
              run: |
                  echo ${{ secrets.INT_TESTS_API_KEY }} >> .env 
                  echo ${{ secrets.INT_TESTS_API_URL }} >> .env
                  echo ${{ secrets.INT_TESTS_BRANCH_NAME }} >> .env
                  echo ${{ secrets.INT_TESTS_PROJECT_UUID }} >> .env

            - name: Unit tests
              shell: bash
              run: make test.unit

            - name: Int tests
              shell: bash
              run: |
                  echo no-test
                  # make test.int

    build:
        needs: [npm-clean-install]
        runs-on: builder
        container:
            image: node:24
            options: --user 1000

        steps:
            - name: Check-out
              uses: actions/checkout@v7
              with:
                  fetch-depth: 1

            - name: Install
              uses: autonomous-testing/npm-install-action@v3
              with:
                  npm-token: ${{ github.token }}

            - name: Build
              run: make build

    after-build-test:
        needs: [build]
        runs-on: builder
        container:
            image: node:24
            options: --user 1000

        steps:
            - name: Check-out
              uses: actions/checkout@v7
              with:
                  fetch-depth: 1

            - name: Install
              uses: autonomous-testing/npm-install-action@v3
              with:
                  npm-token: ${{ github.token }}

            - name: E2E test
              run: echo no-test

    release:
        needs: [before-build-test, after-build-test]
        if: github.event_name == 'push'
        runs-on: builder
        permissions:
            contents: write
            issues: write
            pull-requests: write
            packages: write
        container:
            image: node:24
            options: --user 1000

        steps:
            - name: Check-out
              uses: actions/checkout@v7
              with:
                  fetch-depth: 1

            - name: Install
              uses: autonomous-testing/npm-install-action@v3
              with:
                  npm-token: ${{ github.token }}

            - name: Release
              shell: bash
              env:
                  GH_TOKEN: ${{ github.token }}
                  NPM_TOKEN: ${{ github.token }}
              run: make release
