version: '3.8'
services:
  app:
    build: .
    container_name: {{ project_name | replace('_', '-') }}
    ports:
      - "8000:8000"
    env_file:
      - .env
    environment:
      NODE_ENV: development
    command: {{ ('pnpm' if package_manager == 'pnpm' else (package_manager if package_manager in ['npm', 'yarn'] else 'npm')) }} run start:dev
    volumes:
      - .:/usr/src/app
      - /usr/src/app/node_modules
    {% if database_type in ['postgresql', 'mysql', 'mongodb'] or include_caching %}
    depends_on:
      {% if database_type == 'postgresql' %}
      postgres:
        condition: service_healthy
      {% elif database_type == 'mysql' %}
      mysql:
        condition: service_healthy
      {% elif database_type == 'mongodb' %}
      mongodb:
        condition: service_started
      {% endif %}
      {% if include_caching %}
      redis:
        condition: service_started
      {% endif %}
    {% endif %}
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8000/health"]
      interval: 10s
      timeout: 3s
      retries: 5

{% if database_type == 'postgresql' %}
  postgres:
    image: postgres:15-alpine
    environment:
      POSTGRES_USER: user
      POSTGRES_PASSWORD: password
      POSTGRES_DB: {{ project_name | replace('-', '_') }}
    ports:
      - "5432:5432"
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U user"]
      interval: 10s
      timeout: 3s
      retries: 5

{% elif database_type == 'mysql' %}
  mysql:
    image: mysql:8.1
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_DATABASE: {{ project_name | replace('-', '_') }}
      MYSQL_USER: user
      MYSQL_PASSWORD: password
    ports:
      - "3306:3306"
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
      interval: 10s
      timeout: 5s
      retries: 5

{% elif database_type == 'mongodb' %}
  mongodb:
    image: mongo:7
    ports:
      - "27017:27017"
    volumes:
      - mongodb_data:/data/db

{% endif %}
{% if include_caching %}
  redis:
    image: redis:7-alpine
    ports:
      - "6379:6379"
    command: ["redis-server", "--appendonly", "yes"]
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 3s
      retries: 5
{% endif %}

volumes:
  mongodb_data:
    driver: local
