# Woodpecker CI/CD Pipeline - Peikarband Landing
# Smart pipeline with base image management

variables:
  - &base_image 'hub.peikarband.ir/peikarband/landing:base'
  - &app_image 'hub.peikarband.ir/peikarband/landing'

when:
  - event: [push, pull_request, tag, manual]

pipeline:
  # ============================================
  # Ensure Base Image Exists
  # ============================================
  
  ensure-base-image:
    image: woodpeckerci/plugin-docker-buildx
    settings:
      registry: hub.peikarband.ir
      repo: *app_image
      username:
        from_secret: HARBOR_USERNAME
      password:
        from_secret: HARBOR_PASSWORD
      
      dockerfile: docker/Dockerfile.base
      context: .
      platforms: linux/amd64
      
      tags:
        - base
        - base-python3.11-node20
      
      build_args:
        - PYTHON_VERSION=3.11
        - NODE_VERSION=20
        - BUILD_DATE=${CI_PIPELINE_CREATED}
        - VERSION=${CI_COMMIT_SHA:0:8}
      
      labels:
        - org.opencontainers.image.created=${CI_PIPELINE_CREATED}
        - org.opencontainers.image.source=${CI_REPO_LINK}
        - org.opencontainers.image.title=Peikarband Base
        - org.opencontainers.image.description=Base image with Python, Node.js, bun, and build tools
      
      pull: true
      provenance: false
      sbom: false
      push: true 

    
    when:
      event: [push, tag, manual]
      branch: [main, develop, feature/restructure-project]
      # Only rebuild base if its definition changed
      path:
        include:
          - docker/Dockerfile.base
          - .woodpecker.yml

  # ============================================
  # Build Application Image
  # ============================================
  
  build-and-push-app:
    image: woodpeckerci/plugin-docker-buildx
    settings:
      registry: hub.peikarband.ir
      repo: *app_image
      username:
        from_secret: HARBOR_USERNAME
      password:
        from_secret: HARBOR_PASSWORD
      
      dockerfile: docker/Dockerfile
      context: .
      platforms: linux/amd64
      
      build_args:
        - BASE_IMAGE=*base_image
        - VERSION=${CI_COMMIT_SHA:0:8}
        - BUILD_DATE=${CI_PIPELINE_CREATED}
      
      tags:
        - latest
        - ${CI_COMMIT_SHA:0:8}
        - ${CI_COMMIT_BRANCH}
      
      labels:
        - org.opencontainers.image.created=${CI_PIPELINE_CREATED}
        - org.opencontainers.image.source=${CI_REPO_LINK}
        - org.opencontainers.image.url=${CI_REPO_LINK}
        - org.opencontainers.image.revision=${CI_COMMIT_SHA}
        - org.opencontainers.image.version=${CI_COMMIT_SHA:0:8}
        - org.opencontainers.image.title=Peikarband Landing
        - org.opencontainers.image.description=Peikarband hosting platform landing page
      
      pull: true
      provenance: false
      sbom: false
      push: true
    
    when:
      event: [push, tag]
      branch: [main, develop, feature/restructure-project]

  # ============================================
  # Verify Images
  # ============================================
  
  verify-images:
    image: alpine:latest
    commands:
      - apk add --no-cache curl
      - |
        echo "════════════════════════════════════════"
        echo "  🔍 Verifying Images in Registry"
        echo "════════════════════════════════════════"
        echo ""
        
        # Check base image
        echo "Checking base image..."
        if curl -f -u "$HARBOR_USERNAME:$HARBOR_PASSWORD" \
          "https://hub.peikarband.ir/v2/peikarband/landing/manifests/base" > /dev/null 2>&1; then
          echo "✅ Base image: hub.peikarband.ir/peikarband/landing:base"
        else
          echo "⚠️  Base image not found (this is OK if first build)"
        fi
        
        echo ""
        
        # Check app image
        echo "Checking app image..."
        TAG="${CI_COMMIT_SHA:0:8}"
        if curl -f -u "$HARBOR_USERNAME:$HARBOR_PASSWORD" \
          "https://hub.peikarband.ir/v2/peikarband/landing/manifests/$TAG" > /dev/null 2>&1; then
          echo "✅ App image: hub.peikarband.ir/peikarband/landing:$TAG"
          echo ""
          echo "Available tags:"
          echo "  • latest"
          echo "  • ${CI_COMMIT_SHA:0:8}"
          echo "  • ${CI_COMMIT_BRANCH}"
          echo ""
          echo "════════════════════════════════════════"
        else
          echo "❌ Failed to verify app image"
          exit 1
        fi
    
    secrets: [HARBOR_USERNAME, HARBOR_PASSWORD]
    when:
      event: [push, tag]
      branch: [main, develop, feature/restructure-project]

  # ============================================
  # Notifications
  # ============================================
  
  notify-success:
    image: alpine:latest
    commands:
      - echo "════════════════════════════════════════"
      - echo "  🎉 Pipeline Completed Successfully!"
      - echo "════════════════════════════════════════"
      - echo ""
      - echo "Branch:" "${CI_COMMIT_BRANCH}"
      - echo "Commit:" "${CI_COMMIT_SHA:0:8}"
      - echo ""
      - echo "Images:"
      - echo "  • Base:" "hub.peikarband.ir/peikarband/landing:base"
      - echo "  • App:" "hub.peikarband.ir/peikarband/landing:${CI_COMMIT_SHA:0:8}"
      - echo ""
      - echo "Deploy with:"
      - echo "  kubectl set image deployment/peikarband-landing \\"
      - echo "    peikarband-landing=hub.peikarband.ir/peikarband/landing:${CI_COMMIT_SHA:0:8}"
      - echo ""
      - echo "════════════════════════════════════════"
    when:
      event: [push, tag]
      status: success

  notify-failure:
    image: alpine:latest
    commands:
      - echo "════════════════════════════════════════"
      - echo "  ❌ Pipeline Failed!"
      - echo "════════════════════════════════════════"
      - echo ""
      - echo "Branch:" "${CI_COMMIT_BRANCH}"
      - echo "Commit:" "${CI_COMMIT_SHA:0:8}"
      - echo ""
      - echo "Please check the logs above"
      - echo "════════════════════════════════════════"
    when:
      event: [push, tag]
      status: failure
