# Woodpecker Pipeline - Base Image Builder
# 
# این pipeline فقط برای ساخت base image است
# Trigger: Manual یا زمانی که Dockerfile.base تغییر کند
#
# Usage:
#   Manual: در Woodpecker UI -> Manual trigger
#   Auto: هر بار که docker/Dockerfile.base تغییر کند

when:
  - event: [manual, push]
    path:
      - docker/Dockerfile.base
      - .woodpecker-base.yml

pipeline:
  # ============================================
  # Check if base image needs rebuild
  # ============================================
  
  check-base-exists:
    image: alpine:latest
    commands:
      - apk add --no-cache curl jq
      - |
        echo "Checking if base image exists in registry..."
        REGISTRY="hub.peikarband.ir"
        REPO="peikarband/base"
        TAG="python3.11-node20"
        
        # Try to check if image exists (will fail if doesn't exist)
        if curl -f -u "$HARBOR_USERNAME:$HARBOR_PASSWORD" \
          "https://$REGISTRY/v2/$REPO/manifests/$TAG" > /dev/null 2>&1; then
          echo "✓ Base image exists: $REGISTRY/$REPO:$TAG"
          echo "BASE_EXISTS=true" >> /tmp/base_status
        else
          echo "⚠️  Base image not found, will build new one"
          echo "BASE_EXISTS=false" >> /tmp/base_status
        fi
    secrets: [HARBOR_USERNAME, HARBOR_PASSWORD]

  # ============================================
  # Build Base Image
  # ============================================
  
  build-base:
    image: woodpeckerci/plugin-docker-buildx
    settings:
      registry: hub.peikarband.ir
      repo: hub.peikarband.ir/peikarband/base
      username:
        from_secret: HARBOR_USERNAME
      password:
        from_secret: HARBOR_PASSWORD
      
      dockerfile: docker/Dockerfile.base
      context: .
      platforms: linux/amd64
      
      # Multi-tag strategy
      tags:
        - latest
        - python3.11-node20
        - python3.11-node20-${CI_COMMIT_SHA:0:8}
      
      build_args:
        - PYTHON_VERSION=3.11
        - NODE_VERSION=20
      
      labels:
        - org.opencontainers.image.created=${CI_PIPELINE_CREATED}
        - org.opencontainers.image.source=${CI_REPO_LINK}
        - org.opencontainers.image.revision=${CI_COMMIT_SHA}
        - org.opencontainers.image.version=python3.11-node20
        - org.opencontainers.image.title=Peikarband Base Image
        - org.opencontainers.image.description=Base image with Python 3.11, Node.js 20, bun
      
      provenance: false
      push: true
    
    when:
      - event: [manual, push]

  # ============================================
  # Verify Base Image
  # ============================================
  
  verify-base:
    image: alpine:latest
    commands:
      - apk add --no-cache curl
      - |
        echo "Verifying base image was pushed successfully..."
        sleep 5  # Wait for registry to sync
        
        REGISTRY="hub.peikarband.ir"
        REPO="peikarband/base"
        TAG="latest"
        
        if curl -f -u "$HARBOR_USERNAME:$HARBOR_PASSWORD" \
          "https://$REGISTRY/v2/$REPO/manifests/$TAG" > /dev/null 2>&1; then
          echo "✓ Base image verified: $REGISTRY/$REPO:$TAG"
          echo "✓ Base image is ready for use in application builds"
        else
          echo "❌ Failed to verify base image"
          exit 1
        fi
    secrets: [HARBOR_USERNAME, HARBOR_PASSWORD]
    when:
      - event: [manual, push]

  # ============================================
  # Notification
  # ============================================
  
  notify-complete:
    image: alpine:latest
    commands:
      - |
        echo "================================================"
        echo "🎉 Base Image Build Complete!"
        echo "================================================"
        echo ""
        echo "📦 Image Details:"
        echo "   Registry: hub.peikarband.ir/peikarband/base"
        echo "   Tags: latest, python3.11-node20, python3.11-node20-${CI_COMMIT_SHA:0:8}"
        echo ""
        echo "✓ Python: 3.11"
        echo "✓ Node.js: 20"
        echo "✓ Bun: latest"
        echo "✓ Build tools: gcc, g++, make"
        echo ""
        echo "📝 Next Steps:"
        echo "   1. Application builds will now use this base image"
        echo "   2. Build time will be significantly faster"
        echo "   3. Network reliability improved (no repeated npm/bun installs)"
        echo ""
        echo "================================================"
    when:
      - event: [manual, push]
        status: success

