# MetaBuilder Development Deployment
# Fire-and-forget development stack with hot-reload and debugging tools
# Usage: docker-compose -f deployment/docker-compose.development.yml up

version: '3.8'

services:
  # PostgreSQL Database (Development)
  postgres:
    image: postgres:16-alpine
    container_name: metabuilder-postgres-dev
    environment:
      POSTGRES_DB: metabuilder_dev
      POSTGRES_USER: metabuilder
      POSTGRES_PASSWORD: dev_password
      POSTGRES_INITDB_ARGS: "-E UTF8 --locale=C"
    volumes:
      - postgres_data_dev:/var/lib/postgresql/data
      - ./init-db.sh:/docker-entrypoint-initdb.d/init-db.sh:ro
    ports:
      - "5433:5432"  # Different port to avoid conflicts with local PostgreSQL
    networks:
      - metabuilder-dev-network
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U metabuilder"]
      interval: 10s
      timeout: 5s
      retries: 5

  # C++ DBAL Daemon (Development with interactive mode)
  dbal-daemon:
    build:
      context: ../dbal/cpp
      dockerfile: Dockerfile
      args:
        BUILD_TYPE: Debug
    container_name: metabuilder-dbal-dev
    environment:
      DBAL_BIND_ADDRESS: 0.0.0.0
      DBAL_PORT: 8080
      DBAL_LOG_LEVEL: debug
      DBAL_MODE: development
      DBAL_DAEMON: "false"  # Interactive mode for development
      DBAL_CONFIG: /app/config/config.yaml
    volumes:
      - ../dbal/cpp:/app/source:ro  # Mount source for live changes
      - ./config/dbal:/app/config:ro
      - dbal_data_dev:/app/data
    ports:
      - "8081:8080"  # Different port to avoid conflicts
    stdin_open: true  # Enable interactive mode
    tty: true
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8080/health"]
      interval: 30s
      timeout: 5s
      retries: 3
      start_period: 10s
    depends_on:
      postgres:
        condition: service_healthy
    networks:
      - metabuilder-dev-network

  # MetaBuilder Frontend & API (Development with hot-reload)
  metabuilder-app:
    build:
      context: ..
      dockerfile: deployment/Dockerfile.app.dev
      args:
        NODE_ENV: development
    container_name: metabuilder-app-dev
    environment:
      NODE_ENV: development
      DATABASE_URL: postgresql://metabuilder:dev_password@postgres:5432/metabuilder_dev
      PORT: 3000
      DBAL_API_URL: http://dbal-daemon:8080
      VITE_HMR_HOST: localhost  # For Vite hot module replacement
    volumes:
      - ..:/app  # Mount entire project for hot-reload
      - /app/node_modules  # Prevent overwriting node_modules
      - app_cache_dev:/app/.cache
    ports:
      - "5173:3000"  # Vite dev server
    command: npm run dev
    restart: unless-stopped
    depends_on:
      postgres:
        condition: service_healthy
      dbal-daemon:
        condition: service_healthy
    networks:
      - metabuilder-dev-network

  # Redis Cache (Development)
  redis:
    image: redis:7-alpine
    container_name: metabuilder-redis-dev
    command: redis-server --appendonly yes
    volumes:
      - redis_data_dev:/data
    ports:
      - "6380:6379"  # Different port to avoid conflicts
    networks:
      - metabuilder-dev-network
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 3s
      retries: 5

  # Mailhog (Email testing in development)
  mailhog:
    image: mailhog/mailhog:latest
    container_name: metabuilder-mailhog
    ports:
      - "1025:1025"  # SMTP
      - "8025:8025"  # Web UI
    networks:
      - metabuilder-dev-network

  # Adminer (Database management UI)
  adminer:
    image: adminer:latest
    container_name: metabuilder-adminer
    environment:
      ADMINER_DEFAULT_SERVER: postgres
    ports:
      - "8082:8080"
    depends_on:
      - postgres
    networks:
      - metabuilder-dev-network

  # Redis Commander (Redis management UI)
  redis-commander:
    image: rediscommander/redis-commander:latest
    container_name: metabuilder-redis-commander
    environment:
      REDIS_HOSTS: local:redis:6379
    ports:
      - "8083:8081"
    depends_on:
      - redis
    networks:
      - metabuilder-dev-network

volumes:
  postgres_data_dev:
    driver: local
  dbal_data_dev:
    driver: local
  app_cache_dev:
    driver: local
  redis_data_dev:
    driver: local

networks:
  metabuilder-dev-network:
    driver: bridge
    ipam:
      config:
        - subnet: 172.21.0.0/16
