name: CI - Main Pipeline

permissions:
  contents: read

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

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

jobs:
  # ========================================
  # 1. Docker SSoT (bleibt wie ist)
  # ========================================
  docker-ssot:
    name: Docker SSoT Validation
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6

      - name: Validate Docker SSoT (compat mode)
        if: ${{ hashFiles('scripts/docker-versions-update.sh') != '' && hashFiles('scripts/generate-compose-files.sh') != '' && hashFiles('scripts/validate-docker-consistency.sh') != '' }}
        run: |
          bash scripts/docker-versions-update.sh sync
          bash scripts/generate-compose-files.sh all development
          bash scripts/validate-docker-consistency.sh all

      - name: Validate Docker SSoT (envless mode)
        if: ${{ hashFiles('scripts/generate-compose-files.sh') != '' && hashFiles('scripts/validate-docker-consistency.sh') != '' }}
        run: |
          DOCKER_SSOT_MODE=envless bash scripts/generate-compose-files.sh all development
          DOCKER_SSOT_MODE=envless bash scripts/validate-docker-consistency.sh all

      - name: Lint docker-compose (fallback)
        if: ${{ hashFiles('scripts/validate-docker-consistency.sh') == '' }}
        run: docker compose -f docker/docker-compose.yaml config

  # ========================================
  # 2. OpenAPI Validation (nur Lint)
  # ========================================
  validate-openapi:
    name: Validate OpenAPI Specs
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6

      - name: Setup Node.js
        uses: actions/setup-node@v6
        with:
          node-version: '20'

      - name: Install Spectral
        run: npm install -g @stoplight/spectral-cli

      - name: Validate OpenAPI
        run: |
          spectral lint backend/gateway/src/main/resources/openapi/documentation.yaml \
            --ruleset .spectral.yaml \
            --fail-severity error

  # ========================================
  # 3. Markdown Docs (nur kritische)
  # ========================================
  validate-docs:
    name: Validate Essential Docs
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6

      - name: Lint Critical Markdown
        uses: DavidAnson/markdownlint-cli2-action@v21
        with:
          globs: |
            README.md
            docs/README.md
            docs/adr/**/*.md
            docs/how-to/start-local.md

      - name: Check Links in ADRs
        uses: gaurav-nelson/github-action-markdown-link-check@v1
        with:
          config-file: '.github/markdown-link-check.json'
          folder-path: 'docs/adr/'
          use-quiet-mode: 'yes'

  # ========================================
  # 4. Build & Test
  # ========================================
  build-test:
    name: Build and Test
    runs-on: ubuntu-latest
    needs: [ docker-ssot, validate-openapi, validate-docs ]

    steps:
      - uses: actions/checkout@v6

      - name: Setup JDK 21
        uses: actions/setup-java@v5
        with:
          java-version: '21'
          distribution: 'temurin'
          cache: gradle

      - name: Build
        run: ./gradlew staticAnalysis build -x test

      - name: Check Bundle Budgets
        run: ./gradlew checkBundleBudget

      - name: Test
        run: ./gradlew test

      - name: Upload OpenAPI Specs (Artifact)
        uses: actions/upload-artifact@v5
        with:
          name: openapi-specs
          path: docs/api/generated/*.json
          retention-days: 30
        continue-on-error: true
