name: Multi-Level CI/CD Pipeline
on: [push]

jobs:
  # Level 0 - Инициализация
  init:
    name: "Initialize Pipeline"
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        run: echo "Checking out..."
      - name: Setup environment
        run: echo "Setting up..."
      - name: Validate config
        run: echo "Validating..."

  # Level 1 - Параллельные проверки
  lint-frontend:
    name: "Lint Frontend"
    needs: init
    runs-on: ubuntu-latest
    steps:
      - run: echo "Linting frontend..."
      - run: sleep 3

  lint-backend:
    name: "Lint Backend"
    needs: init
    runs-on: ubuntu-latest
    steps:
      - run: echo "Linting backend..."
      - run: sleep 3

  # Level 2 - Сборка
  build-frontend:
    name: "Build Frontend"
    needs: lint-frontend
    runs-on: ubuntu-latest
    steps:
      - run: echo "Building frontend..."
      - run: sleep 4

  build-backend:
    name: "Build Backend"
    needs: lint-backend
    runs-on: ubuntu-latest
    steps:
      - run: echo "Building backend..."
      - run: sleep 5

  # Level 3 - Матрица тестов
  test-unit-matrix:
    name: "Unit Tests"
    needs: [build-frontend, build-backend]
    strategy:
      matrix:
        component: [api, service, util]
        coverage: [true, false]
    runs-on: ubuntu-latest
    steps:
      - name: Run tests
        run: |
          echo "Testing ${{ matrix.component }} (coverage: ${{ matrix.coverage }})"
      - name: Sleep
        run: sleep 3

  test-integration:
    name: "Integration Tests"
    needs: build-backend
    runs-on: ubuntu-latest
    steps:
      - run: echo "Running integration tests..."
      - run: sleep 6

  # Level 4 - E2E тесты в матрице
  test-e2e-matrix:
    name: "E2E Tests"
    needs: [build-frontend, test-unit-matrix]
    strategy:
      matrix:
        browser: [chrome, firefox, safari]
        viewport: [desktop, mobile, tablet]
    runs-on: ubuntu-latest
    steps:
      - run: echo "E2E on ${{ matrix.browser }} (${{ matrix.viewport }})"
      - run: sleep 4

  # Level 5 - Сборка артефактов
  bundle-app:
    name: "Bundle Application"
    needs: [test-unit-matrix, test-integration, test-e2e-matrix]
    runs-on: ubuntu-latest
    steps:
      - run: echo "Bundling app..."
      - run: sleep 3

  # Level 6 - Деплой в разные среды
  deploy-dev:
    name: "Deploy to Dev"
    needs: bundle-app
    runs-on: ubuntu-latest
    steps:
      - run: echo "Deploying to dev..."
      - run: sleep 3

  deploy-qa:
    name: "Deploy to QA"
    needs: bundle-app
    runs-on: ubuntu-latest
    steps:
      - run: echo "Deploying to QA..."
      - run: sleep 3

  # Level 7 - Финальные проверки
  verify-dev:
    name: "Verify Dev"
    needs: deploy-dev
    runs-on: ubuntu-latest
    steps:
      - run: echo "Verifying dev..."
      - run: sleep 2

  verify-qa:
    name: "Verify QA"
    needs: deploy-qa
    runs-on: ubuntu-latest
    steps:
      - run: echo "Verifying QA..."
      - run: sleep 2

  # Level 8 - Продакшн
  deploy-prod:
    name: "Deploy to Production"
    needs: [verify-dev, verify-qa]
    runs-on: ubuntu-latest
    steps:
      - run: echo "Deploying to production..."
      - run: sleep 5

  # Level 9 - Пост-деплой
  post-deploy-checks:
    name: "Post-Deploy Checks"
    needs: deploy-prod
    runs-on: ubuntu-latest
    steps:
      - run: echo "Running health checks..."
        continue-on-error: true
      - run: echo "Checking metrics..."
      - run: echo "Validating deployment..."