version: '3.8'

services:
  # Backend API
  backend:
    build:
      context: ./backend
      dockerfile: Dockerfile.prod
    container_name: cuccu_backend
    restart: unless-stopped
    ports:
      - "5000:5000"
    env_file:
      - ./backend/.env
    environment:
      # MongoDB connection string from Atlas
      MONGODB_URI: "mongodb+srv://20010841:vuhoanganh1704@cluster0.h6qro.mongodb.net/cucu_note?retryWrites=true&w=majority&appName=Cluster0"
      MONGODB_DB_NAME: "cucu_note"
    volumes:
      - ./backend:/app
      - backend_data:/app/data
    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: 2G
          cpus: '1.0'
        reservations:
          memory: 512M
          cpus: '0.5'

  # Frontend
  frontend:
    build:
      context: ./frontend
      dockerfile: Dockerfile.prod
      args:
        # Build-time envs for Vite
        # Browser (người dùng) gọi trực tiếp vào host, nên dùng localhost:5000
        VITE_API_BASE_URL: "http://localhost:5000"
        VITE_CLERK_PUBLISHABLE_KEY: ${VITE_CLERK_PUBLISHABLE_KEY}
    container_name: cuccu_frontend
    restart: unless-stopped
    ports:
      - "3001:80"
    env_file:
      - ./frontend/.env
    depends_on:
      - backend
    networks:
      - cuccu_network
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:80"]
      interval: 30s
      timeout: 10s
      retries: 3

volumes:
  backend_data:
    driver: local

networks:
  cuccu_network:
    driver: bridge

