version: '3.8'

services:
  # Backend API
  backend:
    build:
      context: ./backend
      dockerfile: Dockerfile.prod
      cache_from:
        - type=local,src=.docker/cache/backend
      cache_to:
        - type=local,dest=.docker/cache/backend
    container_name: cuccu_backend
    restart: unless-stopped
    ports:
      - "5100:5000"
    env_file:
      - ./backend/.env
    environment:
      # MongoDB — pulled from backend/.env (gitignored)
      MONGODB_URI: ${MONGODB_URI:-mongodb://localhost:27017/cucu_note}
      MONGODB_DB_NAME: ${MONGODB_DB_NAME:-cucu_note}
      # Meilisearch
      MEILI_URL: "http://meilisearch:7700"
      MEILI_MASTER_KEY: ${MEILI_MASTER_KEY:-changeme}
    volumes:
      - ./backend:/app
      - backend_data:/app/data
    depends_on:
      meilisearch:
        condition: service_healthy
    networks:
      - cuccu_network
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:5000/docs"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s
    deploy:
      resources:
        limits:
          memory: 512M
          cpus: '1.0'
        reservations:
          memory: 128M
          cpus: '0.5'

  # Meilisearch - Lightning-fast keyword search
  meilisearch:
    image: getmeili/meilisearch:v1.13
    container_name: cuccu_meilisearch
    restart: unless-stopped
    ports:
      - "7700:7700"
    environment:
      MEILI_MASTER_KEY: ${MEILI_MASTER_KEY:-changeme}
      MEILI_ENV: "production"
      MEILI_NO_ANALYTICS: "true"
    volumes:
      - meili_data:/meili_data
    networks:
      - cuccu_network
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:7700/health"]
      interval: 15s
      timeout: 5s
      retries: 3
      start_period: 10s
    deploy:
      resources:
        limits:
          memory: 64M

  # Frontend (Production build with nginx - saves ~190MB RAM)
  frontend:
    build:
      context: ./frontend
      dockerfile: Dockerfile.prod
      args:
        VITE_API_BASE_URL: ${VITE_API_BASE_URL:-http://localhost:5000}
        VITE_CLERK_PUBLISHABLE_KEY: ${VITE_CLERK_PUBLISHABLE_KEY:-}
    container_name: cuccu_frontend
    restart: unless-stopped
    ports:
      - "3010:80"
    depends_on:
      - backend
    networks:
      - cuccu_network
    healthcheck:
      test: ["CMD", "wget", "-q", "--spider", "http://localhost:80"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 30s
    deploy:
      resources:
        limits:
          memory: 32M

volumes:
  backend_data:
    driver: local
  meili_data:
    driver: local

networks:
  cuccu_network:
    driver: bridge


