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: Unit tests
        shell: bash
        run: make test.unit

  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
        env:
          WOPEE_API_KEY: ${{secrets.WOPEE_API_KEY}}
          WOPEE_API_URL: ${{vars.WOPEE_API_URL}}
          WOPEE_BRANCH_NAME: master
          WOPEE_CUSTOM_TAGS: custom-tag
          WOPEE_ENABLE_SOFT_ASSERT: true
          WOPEE_PIXEL_TO_PIXEL_DIFF_TOLERANCE: 0.1
          WOPEE_PROJECT_UUID: ${{vars.WOPEE_PROJECT_UUID}}
          WOPEE_SCREENSHOT_VALIDATION_ENABLED: true
        run: |
          echo no-test
          # make after.build.test

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

    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: Release
        shell: bash
        env:
          GH_TOKEN: ${{ github.token }}
          NPM_TOKEN: ${{ github.token }}
        run: make release
