name: job-lint
run-name: "[job-lint] ${{ inputs.branch }}"

on:
  workflow_call:
    inputs:
      branch:
        description: "Branch to checkout"
        required: true
        type: string
    secrets:
      BUN_VERSION:
        required: false
      KEY_SSH:
        required: true
      KEY_GPG:
        required: true
      GIT_EMAIL:
        required: true

jobs:
  lint:
    name: Lint & Auto-fix
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          ref: ${{ inputs.branch }}
          fetch-depth: 0
          ssh-key: ${{ secrets.KEY_SSH }}

      - name: Setup Bun Project
        uses: ./.github/actions/setup-bun-project
        with:
          bun-version: ${{ secrets.BUN_VERSION || 'latest' }}

      - name: Run Lint
        id: lint
        run: |
          if bun run lint:github; then
            echo "status=clean" >> $GITHUB_OUTPUT
          else
            bun run lint:fix
            if bun run lint:github; then
              echo "status=fixed" >> $GITHUB_OUTPUT
            else
              echo "status=failed" >> $GITHUB_OUTPUT
              exit 1
            fi
          fi

      - name: Setup Git
        if: steps.lint.outputs.status == 'fixed'
        uses: ./.github/actions/setup-git
        with:
          gpg-key: ${{ secrets.KEY_GPG }}
          git-email: ${{ secrets.GIT_EMAIL }}

      - name: Commit Changes
        if: steps.lint.outputs.status == 'fixed'
        run: |
          if git diff --name-only | grep -E '\.(js|ts|jsx|tsx|css|scss|json|md|yml|yaml)$'; then
            git add .
            git commit -m "style(🎨): Auto-fix lint issues"
          else
            echo "No lint-related changes to commit."
          fi

      - name: Push Changes
        if: steps.lint.outputs.status == 'fixed'
        run: git push origin HEAD:${{ inputs.branch }}
