name: Local Docker Build (Dev)

on:
  push:
    branches: [ dev ]
  pull_request:
    branches: [ dev ]

jobs:
  build-dev:
    runs-on: host
    
    steps:
    - name: Checkout code (DEV)
      run: |
        echo "Current workspace: $(pwd)"
        
        # Clean workspace and temp directory first
        rm -rf * .git* 2>/dev/null || true
        rm -rf /tmp/repo 2>/dev/null || true
        
        # Clone the repository since Gitea runner doesn't auto-checkout
        echo "Cloning repository..."
        git clone --branch dev http://${{ secrets.username }}:${{ secrets.token }}@192.168.253.221:3000/sbcrumb/nfoguard.git /tmp/repo
        cp -r /tmp/repo/* .
        cp -r /tmp/repo/.* . 2>/dev/null || true
        rm -rf /tmp/repo
        
        echo "Repository cloned successfully"
        ls -la
        echo "Checking Dockerfile:"
        head -30 Dockerfile

    - name: Check Docker availability (DEV)
      run: |
        echo "Checking Docker installation..."
        which docker || (echo "Docker not found in PATH" && exit 1)
        docker --version || (echo "Docker not available" && exit 1)
        docker info || (echo "Docker daemon not running" && exit 1)

    - name: Configure Docker for local registry (DEV)
      run: |
        echo "Configuring Docker client for insecure local registry..."
        LOCAL_REGISTRY="192.168.253.221:3000"
        
        export DOCKER_CONFIG=/tmp/docker-config-dev
        mkdir -p $DOCKER_CONFIG
        
        # Create Docker client config for insecure registry
        cat > $DOCKER_CONFIG/config.json << EOF
        {
          "auths": {},
          "HttpHeaders": {
            "User-Agent": "Docker-Client/20.10.0 (linux)"
          },
          "credsStore": "",
          "credHelpers": {},
          "experimental": "disabled"
        }
        EOF
        
        echo "Docker client config created for DEV"
        echo "Testing registry connectivity..."
        curl -s "http://$LOCAL_REGISTRY/v2/" && echo "✅ Registry accessible via HTTP" || echo "❌ Registry not accessible"

    - name: Build Docker image with caching (DEV)
      run: |
        echo "Building DEV Docker image with layer caching..."
        LOCAL_REGISTRY="192.168.253.221:3000"
        CACHE_IMAGE="$LOCAL_REGISTRY/sbcrumb/nfoguard:dev-buildcache"
        
        # Clear any existing Docker configs to prevent permission issues
        unset DOCKER_CONFIG
        export HOME=/tmp/docker-home-dev
        mkdir -p $HOME
        
        # Enable Docker BuildKit for better caching
        export DOCKER_BUILDKIT=1
        
        # Check if DEV cache image exists and pull it
        echo "Checking for existing DEV cache image..."
        CACHE_ARGS=""
        if curl -s "http://$LOCAL_REGISTRY/v2/sbcrumb/nfoguard/manifests/dev-buildcache" > /dev/null 2>&1; then
          echo "✅ DEV cache manifest found, pulling image..."
          if docker pull "$CACHE_IMAGE" 2>/dev/null; then
            echo "✅ DEV cache image pulled successfully"
            CACHE_ARGS="--cache-from=$CACHE_IMAGE"
          else
            echo "⚠️ DEV cache manifest exists but pull failed"
          fi
        else
          echo "ℹ️ No DEV cache image found (first build or cache expired)"
        fi
        
        # Build DEV image with cache (if available)
        echo "Building DEV image with layer caching..."
        VERSION=$(cat VERSION 2>/dev/null || echo "unknown")
        echo "Building version: $VERSION"
        
        docker build \
          $CACHE_ARGS \
          --build-arg GIT_BRANCH=dev \
          --build-arg BUILD_SOURCE=gitea \
          --tag nfoguard:dev \
          --tag nfoguard:${VERSION}-dev-gitea \
          --tag nfoguard:dev-${{ github.sha }} \
          --tag "$CACHE_IMAGE" \
          .
        
        echo "DEV Docker image built and tagged successfully"

    - name: Login and Push to Gitea registry (DEV tags)
      continue-on-error: true
      run: |
        echo "Using LOCAL IP ONLY for DEV push!"
        export DOCKER_CONFIG=/tmp/docker-config-dev
        
        LOCAL_REGISTRY="192.168.253.221:3000"
        echo "Registry: $LOCAL_REGISTRY (DEV TAGS)"
        
        # Try login to local registry
        if echo "${{ secrets.token }}" | docker --config $DOCKER_CONFIG login "$LOCAL_REGISTRY" -u "${{ secrets.username }}" --password-stdin 2>/dev/null; then
          echo "✅ DEV Login succeeded"
          
          # Tag for local registry
          VERSION=$(cat VERSION 2>/dev/null || echo "unknown")
          docker tag nfoguard:dev "$LOCAL_REGISTRY/sbcrumb/nfoguard:dev"
          docker tag nfoguard:${VERSION}-dev-gitea "$LOCAL_REGISTRY/sbcrumb/nfoguard:${VERSION}-dev-gitea"
          docker tag nfoguard:dev "$LOCAL_REGISTRY/sbcrumb/nfoguard:dev-${{ github.sha }}"
          
          # Push DEV images
          echo "=== Pushing DEV cache image ==="
          timeout 60 docker --config $DOCKER_CONFIG push "$LOCAL_REGISTRY/sbcrumb/nfoguard:dev-buildcache" && echo "✅ DEV cache pushed" || echo "⚠️ DEV cache push failed"
          
          echo "=== Pushing DEV latest tag ==="
          if timeout 120 docker --config $DOCKER_CONFIG push "$LOCAL_REGISTRY/sbcrumb/nfoguard:dev"; then
            echo "✅ DEV tag pushed successfully"
            
            echo "=== Pushing version-dev-gitea tag ==="
            if timeout 60 docker --config $DOCKER_CONFIG push "$LOCAL_REGISTRY/sbcrumb/nfoguard:${VERSION}-dev-gitea"; then
              echo "✅ Version-dev-gitea tag pushed successfully: ${VERSION}-dev-gitea"
            fi
            
            if timeout 60 docker --config $DOCKER_CONFIG push "$LOCAL_REGISTRY/sbcrumb/nfoguard:dev-${{ github.sha }}"; then
              echo "✅ DEV SHA tag pushed successfully"
            fi
          fi
        else
          echo "❌ DEV local registry login failed - continuing to Docker Hub"
        fi

        echo ""
        echo "🎉 DEV build completed successfully!"
        echo ""
        VERSION=$(cat VERSION 2>/dev/null || echo "unknown")
        echo "📦 Available DEV images:"
        echo "  Local only:     nfoguard:dev"
        echo "  Version tag:    nfoguard:${VERSION}-dev-gitea"
        echo "  Local SHA:      nfoguard:dev-${{ github.sha }}"
        echo ""
        echo "🐳 Pull with: docker pull 192.168.253.221:3000/sbcrumb/nfoguard:${VERSION}-dev-gitea"