# .github/workflows/deploy.yml
name: Install + Build + Deploy

# ----------------------------------------------------------------------------------
# 🔧 CI Workflow: Install, Lint, Build, Test (Optional: Format & Type Check)
#
# This workflow runs on push or pull request to `main`, `dev`, or `staging` branches.
# It can also be called as a reusable workflow from other repositories.
#
# It ensures:
# 1. Dependency installation using `pnpm`
# 2. Static checks like linting and unit testing
# 3. Builds the project using your defined build script
#
# Optional steps:
# - Format check: pnpm format:check
# - Type check: pnpm type:check
#
# ✅ Recommended for all PRs and deployments to ensure build and code quality.
# ----------------------------------------------------------------------------------

on:
  push:
    branches: [main, dev, staging]
  pull_request:
    branches: [main, dev, staging]
  
  # Allow being called as reusable workflow from other repos
  workflow_call:
    inputs:
      node_version:
        description: 'Node.js version to use'
        required: false
        type: string
        default: '22.4.x'
      pnpm_version:
        description: 'pnpm version to use'
        required: false
        type: string
        default: '8'
      skip_format_check:
        description: 'Skip format checking step'
        required: false
        type: boolean
        default: false
      skip_type_check:
        description: 'Skip type checking step'
        required: false
        type: boolean
        default: false
    secrets:
      NPM_PRIVATE_PACKAGE_TOKEN:
        required: false

# Minimal read access required unless write/publish is added later
permissions:
  contents: read

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      # 📥 Checkout code
      - uses: actions/checkout@v4

      # 🧰 Setup pnpm (preferred package manager)
      - uses: pnpm/action-setup@v2
        with:
          version: ${{ inputs.pnpm_version || '8' }}

      # 📦 Setup Node.js
      - uses: actions/setup-node@v4
        with:
          node-version: ${{ inputs.node_version || '22.4.x' }}
          cache: 'pnpm'

      # 📦 Install dependencies
      - name: Install dependencies
        run: pnpm install
        env:
          NPM_TOKEN: ${{ secrets.NPM_PRIVATE_PACKAGE_TOKEN }}

      # 🛠️ Build project
      - name: Build project
        run: pnpm build

      # ✅ Lint codebase
      - name: Lint codebase
        run: pnpm lint

      # 🧪 Run tests
      - name: Run tests
        run: pnpm test

      # 🧹 Check code formatting (optional)
      - name: Check code formatting
        if: ${{ !inputs.skip_format_check }}
        run: pnpm format:check

      # 🧠 Type checking (optional)
      - name: Type checking
        if: ${{ !inputs.skip_type_check }}
        run: pnpm type:check