name: Main

on:
  push:
    branches:
      - '**'
      - '!main'
    tags:
      - 'v*.*.*'
      - 'V*.*.*'
      - '*.*.*'
  workflow_dispatch:
    inputs:
      bump-type:
        description: 'Type of version bump (major, minor, patch, build)'
        required: false
        default: 'build'
  pull_request:
    branches:
      - 'main'

permissions:
  contents: write
  pull-requests: write
  packages: write
  id-token: write

jobs:
  decision:
    runs-on: ubuntu-latest
    outputs:
      push_image: ${{ steps.decide.outputs.push_image }}
    steps:
      - name: Decide push or load
        id: decide
        run: |
          # Default: do not push
          push_image=false
          if [[ "${GITHUB_EVENT_NAME}" == "push" ]] && { [[ "${GITHUB_REF}" == "refs/heads/main" ]] || [[ "${GITHUB_REF_TYPE}" ==  "tag" ]]; }; then
            push_image=true
          elif [[ "${GITHUB_EVENT_NAME}" == "workflow_dispatch" ]] && [[ "${GITHUB_REF}" == "refs/heads/main" ]] && [[ "${BUMP_TYPE}" != "build" ]]; then
            push_image=true
          elif [[ "${GITHUB_EVENT_NAME}" == "pull_request" ]] && [[ "${GITHUB_REF}" == "refs/heads/main" ]]; then
            push_image=true
          fi
          echo "push_image=${push_image}" >> "$GITHUB_OUTPUT"
      - name: Debug decision
        run: |
          echo "GITHUB_EVENT_NAME=${GITHUB_EVENT_NAME}"
          echo "GITHUB_REF=${GITHUB_REF}"
          echo "GITHUB_REF_TYPE=${GITHUB_REF_TYPE}"
          echo "push_image (output)=${{ steps.decide.outputs.push_image }}"

  validate:
    name: Validate
    concurrency:
      group: validate-group
      cancel-in-progress: true
    runs-on: ubuntu-latest
    steps:
      - name: ⬇️ Checkout Repo
        uses: actions/checkout@v7

      - name: ⬢ Setup Node.js
        uses: actions/setup-node@v6
        with:
          node-version: '*'
          cache: npm

      - name: 📦 Install Packages
        run: npm ci --ignore-scripts --legacy-peer-deps

      - name: 🔧 Setup Husky
        run: npm run prepare

      - name: 🧪 Test
        run: npm test

      - name: 🏗️ Production Build
        run: npm run build

  release:
    name: Release
    runs-on: ubuntu-latest
    needs: [validate, decision]
    if: needs.decision.outputs.push_image == 'true'
    steps:
      - name: ⬇️ Checkout Repo
        uses: actions/checkout@v7

      - name: ⬢ Setup Node.js
        uses: actions/setup-node@v6
        with:
          node-version: '*'
          registry-url: 'https://registry.npmjs.org'

      - name: 📦 Install Packages
        run: npm ci --ignore-scripts --legacy-peer-deps

      - name: 🔧 Setup Husky
        run: npm run prepare

      - name: 🏗️ Production Build
        id: build
        run: |
          OUTPUT=$(npm run build) 
          VERSION=$(echo $OUTPUT | grep -oP 'lawn-mower-card@\K[^ ]+') 
          echo "PACKAGE_VERSION=$VERSION" >> $GITHUB_ENV 
          echo "version=$VERSION" >> $GITHUB_OUTPUT
          echo "$VERSION"

      - name: 🚀 Release
        run: npm publish --access public
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

      - name: Create Release
        run: |
          gh release create v${{ steps.build.outputs.version }} \
            --repo="${GITHUB_REPOSITORY}" \
            --title="${{ steps.build.outputs.version }}" \
            --notes="Release notes for v${{ steps.build.outputs.version }}" \
            --draft
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Upload Release Asset
        run: |
          gh release upload v${{ steps.build.outputs.version }} ./dist/lawn-mower-card.js
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

  dependabot:
    name: Dependabot Auto-merge
    runs-on: ubuntu-latest
    needs: [validate]
    if: (github.event_name == 'pull_request' && github.event.pull_request.merged == true)
    steps:
      - name: 💿 Dependabot metadata
        id: metadata
        uses: dependabot/fetch-metadata@v3
        with:
          github-token: ${{ secrets.GITHUB_TOKEN }}

      - name: ↔️ Enable Auto-merge for minor and patch Dependabot PRs
        run: gh pr merge --auto --squash "$PR_URL"
        if: steps.metadata.outputs.update-type == 'version-update:semver-minor' || steps.metadata.outputs.update-type == 'version-update:semver-patch'
        env:
          PR_URL: ${{ github.event.pull_request.html_url }}
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
