on:
  workflow_call:

jobs:
  eslint-and-prettier:
    runs-on: ubuntu-22.04

    steps:
      - name: checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 2

      - name: setup environment
        uses: ./.github/actions/setup
        with:
          actor: lint
          cacheTurbo: false

      - name: lint .env.template files
        run: pnpm lint:env-template

      - name: Cache ESLint and Prettier
        uses: actions/cache@v4
        with:
          path: |
            .eslintcache
            node_modules/.cache/prettier
          key:
            lint-cache-${{ hashFiles('**/pnpm-lock.yaml', 'packages/web/*/tailwind.config.ts',
            'tsconfig.eslint.json', 'packages/web/app/tsconfig.json') }}-${{github.sha}}
          restore-keys: |
            lint-cache-${{ hashFiles('**/pnpm-lock.yaml', 'packages/web/*/tailwind.config.ts',
            'tsconfig.eslint.json', 'packages/web/app/tsconfig.json') }}-

      - name: eslint
        id: eslint
        run: |
          pnpm lint --cache-strategy content || \
          ( \
            echo "" && \
            echo "To fix it, run" && \
            echo "$ pnpm run lint:fix" && \
            echo "" && \
            exit 1 \
          )
        env:
          TIMING: 'true'

      - name: prettier
        id: prettier
        shell: sh
        run: |
          pnpm lint:prettier || \
          ( \
            echo "" && \
            echo "To fix it, run" && \
            echo "$ pnpm run prettier" && \
            echo "" && \
            exit 1 \
          )

      - name: Generate Types
        run: pnpm graphql:generate

      - name: Operation Check
        run: |
          npx graphql-inspector validate \
            "packages/web/app/src/**/*.{graphql,tsx}|packages/libraries/cli/**/*.graphql|packages/web/app/src/lib/**/*.ts" \
            "packages/**/module.graphql.ts" \
            --maxDepth=20 \
            --maxAliasCount=20 \
            --maxDirectiveCount=20 \
            --maxTokenCount=1000
