# template-ci.yml — Pillar 3: CI pipeline for the template repo
# Runs on every push/PR to validate all template combinations

name: Template CI — Validate & Golden Tests

on:
  push:
    branches: [main, develop]
  pull_request:
    branches: [main]

jobs:
  # ── Job 1: Validate all brief combinations ──────────────────────────────
  validate-templates:
    name: Validate template rendering
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - uses: dart-lang/setup-dart@v1
        with:
          sdk: stable

      - name: Install dependencies
        working-directory: generator
        run: dart pub get

      - name: Run generator test suite
        working-directory: generator
        run: dart test test/generator_test.dart --reporter=expanded

      - name: Verify no unreplaced placeholders
        working-directory: generator
        run: |
          dart test test/generator_test.dart \
            --name "Rendered output has no unreplaced" \
            --reporter=expanded

  # ── Job 2: Golden file comparison ───────────────────────────────────────
  golden-tests:
    name: Golden file tests
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dart-lang/setup-dart@v1
        with:
          sdk: stable
      - name: Install dependencies
        working-directory: generator
        run: dart pub get
      - name: Run golden tests
        working-directory: generator
        run: dart test test/generator_test.dart --name "Golden" --reporter=expanded

  # ── Job 3: Lint template files ──────────────────────────────────────────
  lint-templates:
    name: Lint template files
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Check for orphaned placeholders in static files
        run: |
          # Verify no .mdc files (non-template) have unresolved {{PLACEHOLDERS}}
          # These should only be in .mdc.tmpl files
          if find templates/ -name "*.mdc" -not -name "*.tmpl" | xargs grep -l '{{[A-Z_]*}}' 2>/dev/null; then
            echo "ERROR: Found unreplaced placeholders in non-template .mdc files"
            exit 1
          fi
          echo "✔ No orphaned placeholders in static files"

      - name: Check for duplicate alwaysApply: true
        run: |
          # Warn if more than 5 rules have alwaysApply: true (context bloat)
          count=$(grep -r "alwaysApply: true" templates/ | wc -l)
          echo "Files with alwaysApply: true: $count"
          if [ "$count" -gt 10 ]; then
            echo "WARNING: $count files have alwaysApply: true — review for context bloat"
          fi

  # ── Job 4: Validate brief schema ────────────────────────────────────────
  validate-schema:
    name: Validate reference project-brief
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dart-lang/setup-dart@v1
        with:
          sdk: stable
      - name: Install dependencies
        working-directory: generator
        run: dart pub get
      - name: Validate reference project brief
        working-directory: generator
        run: |
          brief="../../example-project/project-brief.yaml"
          if [ ! -f "$brief" ]; then
            echo "ERROR: Missing reference brief at $brief (expected monorepo layout)."
            exit 1
          fi
          echo "Validating: $brief"
          dart run bin/cursor_gen.dart --validate --brief "$brief"
