name: Quality & Testing

on:
  push:
    branches: [main, dev]
  pull_request:
    branches: [main, dev]
  workflow_dispatch:

env:
  NODE_VERSION: "22"
  PNPM_VERSION: "10.26.2"

permissions:
  actions: write
  contents: read

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  auto-format:
    name: Auto-fix Formatting
    runs-on: ubuntu-latest
    if: github.event_name == 'push'
    permissions:
      contents: write
    steps:
      - uses: actions/checkout@v6
        with:
          token: ${{ secrets.GITHUB_TOKEN }}

      - name: Setup Development Environment
        uses: ./.github/actions/setup-env
        with:
          mode: node

      - name: Auto-fix Prettier formatting
        run: npx prettier --write . --ignore-unknown --cache

      - name: Auto-fix Rust formatting
        run: cargo fmt --all --manifest-path src-tauri/Cargo.toml

      - name: Commit formatting fixes
        run: |
          git config --local user.email "action@github.com"
          git config --local user.name "GitHub Action"
          git add .
          if ! git diff --staged --quiet; then
            git commit -m "Auto-fix formatting issues"
            git push
          else
            echo "No formatting changes to commit"
          fi


  rust-quality:
    name: Rust Code Quality
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false

    defaults:
      run:
        shell: bash
        working-directory: src-tauri
    steps:
      - uses: actions/checkout@v6

      - name: Setup Rust Environment
        uses: ./.github/actions/setup-env
        with:
          mode: build

      - name: Install Rust components
        shell: bash
        run: rustup component add rustfmt clippy

      - uses: rui314/setup-mold@v1

      - name: Install cargo-hack
        uses: taiki-e/install-action@v2
        with:
          tool: cargo-hack

      - name: Check Rust formatting
        run: cargo fmt --all -- --color=always --check

      - name: Run Clippy lints
        run: cargo hack --feature-powerset --exclude-features cli-build --no-dev-deps clippy # cspell:disable-line

  # Fast lane: runs on every PR and push. Skips the heavy real Tauri build
  # (kept under tests/index.js) but still covers Vitest + integration suites
  # so feedback stays under ~5 minutes per OS instead of 20+.
  validation-fast:
    name: CLI Fast Validation (${{ matrix.os }})
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, windows-latest, macos-latest]
      fail-fast: false
    steps:
      - name: Checkout repository
        uses: actions/checkout@v6

      - name: Setup Build Environment
        uses: ./.github/actions/setup-env
        with:
          mode: build

      - name: Build CLI
        run: pnpm run cli:build

      - name: Run Fast Test Suite
        shell: bash
        run: PAKE_CREATE_APP=1 node tests/index.js --no-build
        timeout-minutes: 15
        env:
          CI: true
          NODE_ENV: test

      - name: Test CLI Integration (smoke)
        shell: bash
        run: |
          echo "Testing CLI integration..."
          if [[ "$RUNNER_OS" == "Windows" ]]; then
            timeout 60s PAKE_CREATE_APP=1 node dist/cli.js https://weekly.tw93.fun --name "CITest" --debug --iterative-build || true
          else
            timeout 30s PAKE_CREATE_APP=1 node dist/cli.js https://weekly.tw93.fun --name "CITest" --debug --iterative-build || true
          fi

  # Full lane: only runs after merge to main (push event) or manual dispatch.
  # Keeps the real Tauri build coverage off the PR critical path.
  validation-full:
    name: Full Tauri Build (${{ matrix.os }})
    if: github.event_name == 'push' || github.event_name == 'workflow_dispatch'
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, windows-latest, macos-latest]
      fail-fast: false
    steps:
      - name: Checkout repository
        uses: actions/checkout@v6

      - name: Setup Build Environment
        uses: ./.github/actions/setup-env
        with:
          mode: build

      - name: Run Full Test Suite (with real build)
        run: pnpm test
        timeout-minutes: 30
        env:
          CI: true
          NODE_ENV: test

  summary:
    name: Quality Summary
    runs-on: ubuntu-latest
    needs: [auto-format, rust-quality, validation-fast, validation-full]
    if: always()
    steps:
      - name: Generate Summary
        run: |
          {
            echo "# Quality & Testing Summary"
            echo ""
            echo "| Check | Status |"
            echo "|-------|--------|"
            echo "| Auto Formatting | ${{ needs.auto-format.result == 'success' && 'PASSED' || needs.auto-format.result == 'skipped' && 'SKIPPED' || 'FAILED' }} |"
            echo "| Rust Quality | ${{ needs.rust-quality.result == 'success' && 'PASSED' || 'FAILED' }} |"
            echo "| CLI Fast Validation | ${{ needs.validation-fast.result == 'success' && 'PASSED' || 'FAILED' }} |"
            echo "| Full Tauri Build | ${{ needs.validation-full.result == 'success' && 'PASSED' || needs.validation-full.result == 'skipped' && 'SKIPPED (PR fast lane)' || 'FAILED' }} |"
          } >> $GITHUB_STEP_SUMMARY
