# TK-MP-Project 통합 Docker Compose 설정
# 환경 변수 DEPLOY_ENV로 환경 구분: development(기본), production, synology

services:
  # PostgreSQL 데이터베이스
  postgres:
    image: postgres:15-alpine
    container_name: ${COMPOSE_PROJECT_NAME:-tk-mp}-postgres${CONTAINER_SUFFIX:-}
    restart: unless-stopped
    environment:
      POSTGRES_DB: ${POSTGRES_DB:-tk_mp_bom}
      POSTGRES_USER: ${POSTGRES_USER:-tkmp_user}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-tkmp_password_2025}
      POSTGRES_INITDB_ARGS: "--encoding=UTF-8 --locale=C"
    ports:
      # 개발: 5432, 프로덕션: 없음, 시놀로지: 15432
      - "${POSTGRES_EXTERNAL_PORT:-5432}:5432"
    volumes:
      - ${POSTGRES_DATA_VOLUME:-postgres_data}:/var/lib/postgresql/data
      - ./database/init:/docker-entrypoint-initdb.d
    networks:
      - tk-mp-network
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER:-tkmp_user} -d ${POSTGRES_DB:-tk_mp_bom}"]
      interval: 30s
      timeout: 10s
      retries: 3
    profiles:
      - ${POSTGRES_PROFILE:-default}

  # Redis (캐시 및 세션 관리용)
  redis:
    image: redis:7-alpine
    container_name: ${COMPOSE_PROJECT_NAME:-tk-mp}-redis${CONTAINER_SUFFIX:-}
    restart: unless-stopped
    ports:
      # 개발: 6379, 프로덕션: 없음, 시놀로지: 16379
      - "${REDIS_EXTERNAL_PORT:-6379}:6379"
    volumes:
      - ${REDIS_DATA_VOLUME:-redis_data}:/data
    networks:
      - tk-mp-network
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 30s
      timeout: 10s
      retries: 3
    profiles:
      - ${REDIS_PROFILE:-default}

  # 백엔드 FastAPI 서비스
  backend:
    build:
      context: ./backend
      dockerfile: Dockerfile
    container_name: ${COMPOSE_PROJECT_NAME:-tk-mp}-backend${CONTAINER_SUFFIX:-}
    restart: unless-stopped
    ports:
      # 개발: 18000, 프로덕션: 없음, 시놀로지: 10080
      - "${BACKEND_EXTERNAL_PORT:-18000}:8000"
    environment:
      - DATABASE_URL=postgresql://${POSTGRES_USER:-tkmp_user}:${POSTGRES_PASSWORD:-tkmp_password_2025}@postgres:5432/${POSTGRES_DB:-tk_mp_bom}
      - REDIS_URL=redis://redis:6379
      - ENVIRONMENT=${DEPLOY_ENV:-development}
      - DEBUG=${DEBUG:-true}
      - RELOAD=${RELOAD:-true}
      - LOG_LEVEL=${LOG_LEVEL:-DEBUG}
      - PYTHONPATH=/app
    depends_on:
      - postgres
      - redis
    networks:
      - tk-mp-network
    volumes:
      # 개발: 코드 마운트, 프로덕션/시놀로지: 업로드만
      - ${BACKEND_CODE_VOLUME:-./backend}:/app
      - ${UPLOADS_VOLUME:-./backend/uploads}:/app/uploads
    profiles:
      - ${BACKEND_PROFILE:-default}

  # 프론트엔드 React + Nginx 서비스
  frontend:
    build:
      context: ./frontend
      dockerfile: Dockerfile
      args:
        - VITE_API_URL=${VITE_API_URL:-http://localhost:18000}
    container_name: ${COMPOSE_PROJECT_NAME:-tk-mp}-frontend${CONTAINER_SUFFIX:-}
    restart: unless-stopped
    ports:
      # 개발: 13000, 프로덕션: 없음, 시놀로지: 10173
      - "${FRONTEND_EXTERNAL_PORT:-13000}:3000"
    environment:
      - VITE_API_URL=${VITE_API_URL:-http://localhost:18000}
    depends_on:
      - backend
    networks:
      - tk-mp-network
    profiles:
      - ${FRONTEND_PROFILE:-default}

  # Nginx 리버스 프록시 (프로덕션 전용)
  nginx:
    image: nginx:alpine
    container_name: ${COMPOSE_PROJECT_NAME:-tk-mp}-nginx${CONTAINER_SUFFIX:-}
    restart: unless-stopped
    ports:
      - "${NGINX_HTTP_PORT:-80}:80"
      - "${NGINX_HTTPS_PORT:-443}:443"
    volumes:
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
      - ./nginx/ssl:/etc/nginx/ssl:ro
    depends_on:
      - frontend
      - backend
    networks:
      - tk-mp-network
    profiles:
      - production

  # pgAdmin 웹 관리도구
  pgadmin:
    image: dpage/pgadmin4:latest
    container_name: ${COMPOSE_PROJECT_NAME:-tk-mp}-pgadmin${CONTAINER_SUFFIX:-}
    restart: unless-stopped
    environment:
      PGADMIN_DEFAULT_EMAIL: ${PGADMIN_EMAIL:-admin@example.com}
      PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_PASSWORD:-admin2025}
      PGADMIN_CONFIG_SERVER_MODE: 'False'
    ports:
      # 개발: 5050, 시놀로지: 15050, 프로덕션: 비활성화
      - "${PGADMIN_EXTERNAL_PORT:-5050}:80"
    volumes:
      - ${PGADMIN_DATA_VOLUME:-pgadmin_data}:/var/lib/pgadmin
    depends_on:
      - postgres
    networks:
      - tk-mp-network
    profiles:
      - ${PGADMIN_PROFILE:-dev}

volumes:
  postgres_data:
    driver: local
  pgadmin_data:
    driver: local
  redis_data:
    driver: local
  # 시놀로지용 명명된 볼륨
  tk_mp_postgres_data:
    external: false
  tk_mp_redis_data:
    external: false
  tk_mp_uploads:
    external: false

networks:
  tk-mp-network:
    driver: bridge

