# TK-MP-Project Docker Compose 설정
# 기본 설정 (개발 환경 기준)
# version: '3.8'  # Docker Compose v2에서는 version 필드가 선택사항

services:
  # PostgreSQL 데이터베이스
  postgres:
    image: postgres:15-alpine
    container_name: tk-mp-postgres
    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:
      - "${POSTGRES_PORT:-5432}:5432"
    volumes:
      - 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

  # Redis (캐시 및 세션 관리용)
  redis:
    image: redis:7-alpine
    container_name: tk-mp-redis
    restart: unless-stopped
    ports:
      - "${REDIS_PORT:-6379}:6379"
    volumes:
      - redis_data:/data
    networks:
      - tk-mp-network
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 30s
      timeout: 10s
      retries: 3

  # 백엔드 FastAPI 서비스
  backend:
    build:
      context: ./backend
      dockerfile: Dockerfile
    container_name: tk-mp-backend
    restart: unless-stopped
    ports:
      - "${BACKEND_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=${ENVIRONMENT:-development}
      - DEBUG=${DEBUG:-true}
      - PYTHONPATH=/app
      # DB 마이그레이션용 개별 환경변수
      - DB_HOST=postgres
      - DB_PORT=5432
      - DB_NAME=${POSTGRES_DB:-tk_mp_bom}
      - DB_USER=${POSTGRES_USER:-tkmp_user}
      - DB_PASSWORD=${POSTGRES_PASSWORD:-tkmp_password_2025}
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    networks:
      - tk-mp-network
    volumes:
      - ./backend/uploads:/app/uploads
      # 개발 환경에서는 코드 변경 실시간 반영 (오버라이드에서 설정)
    healthcheck:
      test: ["CMD-SHELL", "python -c 'import requests; requests.get(\"http://localhost:8000/health\", timeout=5)'"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 60s

  # 프론트엔드 React + Nginx 서비스
  frontend:
    build:
      context: ./frontend
      dockerfile: Dockerfile
      args:
        - VITE_API_URL=${VITE_API_URL:-/api}
    container_name: tk-mp-frontend
    restart: unless-stopped
    ports:
      - "${FRONTEND_PORT:-13000}:3000"
    environment:
      - VITE_API_URL=${VITE_API_URL:-/api}
    depends_on:
      - backend
    networks:
      - tk-mp-network

  # pgAdmin 웹 관리도구 (개발/테스트 환경용)
  pgadmin:
    image: dpage/pgadmin4:latest
    container_name: tk-mp-pgadmin
    restart: unless-stopped
    environment:
      PGADMIN_DEFAULT_EMAIL: ${PGADMIN_EMAIL:-admin@example.com}
      PGLADMIN_DEFAULT_PASSWORD: ${PGADMIN_PASSWORD:-admin2025}
      PGADMIN_CONFIG_SERVER_MODE: 'False'
    ports:
      - "${PGADMIN_PORT:-5050}:80"
    volumes:
      - pgadmin_data:/var/lib/pgadmin
    depends_on:
      - postgres
    networks:
      - tk-mp-network
    profiles:
      - dev
      - test

volumes:
  postgres_data:
    driver: local
  pgadmin_data:
    driver: local
  redis_data:
    driver: local

networks:
  tk-mp-network:
    driver: bridge