---
kind: pipeline
type: docker
name: catlink-ci-simple

# Trigger настройки
trigger:
  branch:
    - master
    - main
    - develop
  event:
    - push
    - pull_request

# Глобальные переменные
environment:
  DOCKER_BUILDKIT: 1

# Этапы пайплайна
steps:
  # 1. Подготовка и проверка окружения
  - name: prepare
    image: alpine:latest
    commands:
      - echo "🚀 Starting CatLink CI Pipeline"
      - echo "Repository: $${DRONE_REPO}"
      - echo "Branch: $${DRONE_BRANCH}"
      - echo "Commit: $${DRONE_COMMIT_SHA:0:8}"
      - echo "Author: $${DRONE_COMMIT_AUTHOR}"
      - echo "Build Number: $${DRONE_BUILD_NUMBER}"
      - echo ""
      - echo "📁 Checking project structure..."
      - ls -la
      - echo ""
      - echo "📋 CI Scripts availability:"
      - ls -la scripts/ci/ 2>/dev/null || echo "❌ CI scripts directory not found"

  # 2. Базовая проверка кода (без специальных инструментов)
  - name: basic-checks
    image: alpine:latest
    commands:
      - echo "🔍 Running basic code checks..."
      - apk add --no-cache git
      - echo "✅ Git repository check"
      - git status --porcelain || echo "Not a git repository"
      - echo ""
      - echo "📊 Project statistics:"
      - echo "Python files:" $(find . -name "*.py" | wc -l)
      - echo "JavaScript/TypeScript files:" $(find . -name "*.js" -o -name "*.ts" -o -name "*.tsx" | wc -l)
      - echo "Total files:" $(find . -type f | wc -l)
      - echo ""
      - echo "📁 Main directories:"
      - ls -ld */ 2>/dev/null || echo "No directories found"
    depends_on:
      - prepare

  # 3. Docker образы проверка
  - name: docker-check
    image: docker:20.10-dind
    volumes:
      - name: docker
        path: /var/run/docker.sock
    commands:
      - echo "🐳 Docker environment check..."
      - docker --version
      - docker info --format '{{.ServerVersion}}' || echo "Docker daemon not available"
      - echo ""
      - echo "📋 Checking Dockerfiles:"
      - find . -name "Dockerfile*" -exec echo "Found: {}" \;
      - echo ""
      - echo "📋 Checking docker-compose files:"
      - find . -name "docker-compose*.yml" -exec echo "Found: {}" \;
    depends_on:
      - basic-checks

  # 4. Простая сборка (если docker-compose.yml существует)
  - name: simple-build
    image: docker:20.10-dind
    volumes:
      - name: docker
        path: /var/run/docker.sock
    commands:
      - echo "🏗️ Attempting simple build..."
      - apk add --no-cache docker-compose
      - if [ -f docker-compose.yml ]; then
          echo "✅ Found docker-compose.yml, attempting build...";
          docker-compose config --quiet && echo "✅ docker-compose.yml is valid" || echo "❌ docker-compose.yml has issues";
          echo "Building images (timeout 10 minutes)...";
          timeout 600 docker-compose build --parallel || echo "⚠️ Build timeout or failed";
        else
          echo "⚠️ docker-compose.yml not found, skipping build";
        fi
    depends_on:
      - docker-check
    failure: ignore

  # 5. Проверка безопасности базовая
  - name: security-basic
    image: alpine:latest
    commands:
      - echo "🔒 Basic security checks..."
      - echo "Checking for common sensitive files:"
      - find . -name ".env" -o -name "*.key" -o -name "*.pem" -o -name "id_rsa" | head -10
      - echo ""
      - echo "Checking for hardcoded secrets patterns:"
      - grep -r -i "password\|secret\|token\|api_key" . --include="*.py" --include="*.js" --include="*.ts" | head -5 || echo "No obvious secrets found"
      - echo ""
      - echo "✅ Basic security check completed"
    depends_on:
      - basic-checks
    failure: ignore

  # 6. Результаты сборки
  - name: build-result
    image: alpine:latest
    commands:
      - echo "📊 Build Summary:"
      - echo "================="
      - echo "✅ Repository: $${DRONE_REPO}"
      - echo "✅ Branch: $${DRONE_BRANCH}"
      - echo "✅ Commit: $${DRONE_COMMIT_SHA:0:8}"
      - echo "✅ Author: $${DRONE_COMMIT_AUTHOR}"
      - echo "✅ Build: #$${DRONE_BUILD_NUMBER}"
      - echo ""
      - echo "🎉 Basic CI pipeline completed successfully!"
      - echo "💡 To enable full CI/CD features:"
      - echo "   1. Set up CI scripts in scripts/ci/"
      - echo "   2. Configure Docker registry secrets"
      - echo "   3. Set up deployment targets"
    depends_on:
      - simple-build
      - security-basic
    when:
      status:
        - success

  # 7. Обработка ошибок
  - name: build-failure
    image: alpine:latest
    commands:
      - echo "❌ BUILD FAILED!"
      - echo "=================="
      - echo "📁 Repository: $${DRONE_REPO}"
      - echo "🌿 Branch: $${DRONE_BRANCH}"
      - echo "👤 Author: $${DRONE_COMMIT_AUTHOR}"
      - echo "📝 Commit: $${DRONE_COMMIT_SHA:0:8}"
      - echo "🔗 Build: #$${DRONE_BUILD_NUMBER}"
      - echo ""
      - echo "🔍 Debugging information:"
      - echo "- Check if CI scripts exist in scripts/ci/"
      - echo "- Verify docker-compose.yml syntax"
      - echo "- Check Drone CI logs for details"
      - echo ""
      - echo "📚 Documentation:"
      - echo "- CI/CD Guide: docs/CICD.md"
      - echo "- Makefile Commands: docs/MAKEFILE.md"
    depends_on:
      - simple-build
      - security-basic
    when:
      status:
        - failure

# Volumes для Docker
volumes:
  - name: docker
    host:
      path: /var/run/docker.sock