name: Lint, Build, and Docker

on:
  push:
    branches: [main, develop]
  pull_request:
    branches: [main, develop]

jobs:
  # ==============================================================================
  # Job 1: Format Check
  # ==============================================================================
  format:
    runs-on: docker
    container:
      image: rust:1.83-slim
    
    steps:
      - name: Install Node.js for checkout
        run: |
          apt-get update
          apt-get install -y curl gnupg
          curl -fsSL https://deb.nodesource.com/setup_20.x | bash -
          apt-get install -y nodejs
      
      - name: Checkout code
        uses: actions/checkout@v4
      
      - name: Install dependencies
        run: |
          apt-get update
          apt-get install -y pkg-config libssl-dev
          rustup component add rustfmt
      
      - name: Check formatting
        working-directory: ./backend
        run: cargo fmt --all -- --check

  # ==============================================================================
  # Job 2: Lint
  # ==============================================================================
  clippy:
    runs-on: docker
    container:
      image: rust:1.83-slim
    
    steps:
      - name: Install Node.js for checkout
        run: |
          apt-get update
          apt-get install -y curl gnupg
          curl -fsSL https://deb.nodesource.com/setup_20.x | bash -
          apt-get install -y nodejs
      
      - name: Checkout code
        uses: actions/checkout@v4
      
      - name: Install dependencies
        run: |
          apt-get update
          apt-get install -y pkg-config libssl-dev
          rustup component add clippy
      
      - name: Run Clippy
        working-directory: ./backend
        run: cargo clippy --all-targets --all-features

  # ==============================================================================
  # Job 3: Build - Depends on format and clippy
  # ==============================================================================
  build:
    runs-on: docker
    container:
      image: rust:1.83-slim
    needs: [format, clippy]
    
    steps:
      - name: Install Node.js for checkout
        run: |
          apt-get update
          apt-get install -y curl gnupg
          curl -fsSL https://deb.nodesource.com/setup_20.x | bash -
          apt-get install -y nodejs
      
      - name: Checkout code
        uses: actions/checkout@v4
      
      - name: Install dependencies
        run: |
          apt-get update
          apt-get install -y pkg-config libssl-dev
          rustup component add clippy
      
      - name: Build release binary
        working-directory: ./backend
        run: cargo build --release --verbose

  # ==============================================================================
  # Job 4: Docker Build - Uses DinD with Buildx
  # ==============================================================================
  docker-build:
    runs-on: docker
    container:
      image: docker:cli
      volumes:
        - /var/run/docker.sock:/var/run/docker.sock
    needs: [build]
    
    services:
      docker:
        image: docker:dind
        command: ["dockerd", "--host=tcp://0.0.0.0:2375", "--tls=false"]
        options: >-
          --privileged
          -e DOCKER_TLS_CERTDIR=
    
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
      
      - name: Verify Docker is available
        run: |
          docker version
          docker info
      
      - name: Set up Docker Buildx
        run: |
          docker buildx version
          docker buildx create --use --name builder --driver docker --driver-opt network=host
          docker buildx inspect --bootstrap
      
      - name: Build Docker image with Buildx
        working-directory: ./backend
        run: |
          docker buildx build \
            --file docker/Dockerfile \
            --tag normogen-backend:${{ github.sha }} \
            --tag normogen-backend:latest \
            --cache-from type=local,src=/tmp/.buildx-cache \
            --cache-to type=local,dest=/tmp/.buildx-cache-new,mode=max \
            --load \
            .
          
          rm -rf /tmp/.buildx-cache
          mv /tmp/.buildx-cache-new /tmp/.buildx-cache || true
      
      - name: Test Docker image
        run: |
          docker run --rm normogen-backend:${{ github.sha }} \
            ls -la /app/normogen-backend || echo "Binary check"
      
      - name: Show image info
        run: |
          docker images normogen-backend
          docker inspect normogen-backend:${{ github.sha }} || echo "Image inspect"
