services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: {{ project_slug }}_app
    ports:
      - "8000:8000"
    environment:
      - APP_ENV=production
      - DEBUG=false
    {% if has_database %}
      {% if database.value == "PostgreSQL" %}
      - DATABASE_URL=postgresql+psycopg://myuser:mypassword@postgres:5432/mydb
      {% elif database.value == "MySQL" %}
      - DATABASE_URL=mysql+asyncmy://myuser:mypassword@mysql:3306/mydb
      {% elif database.value == "SQLite" %}
      - DATABASE_URL=sqlite+aiosqlite:///./app.db
      {% endif %}
    {% endif %}
    {% if has_store %}
      - REDIS_URL=redis://{{ project_slug }}_{{ memory_store.value|lower }}:6379/0
    {% endif %}
    env_file:
      - .env
    {% if (database.value == "PostgreSQL" or database.value == "MySQL") or has_store %}
    depends_on:
      {% if database.value == "PostgreSQL" %}
      postgres:
        condition: service_healthy
      {% elif database.value == "MySQL" %}
      mysql:
        condition: service_healthy
      {% endif %}
      {% if memory_store.value == "Redis" %}
      redis:
        condition: service_healthy
      {% elif memory_store.value == "Valkey" %}
      valkey:
        condition: service_healthy
      {% endif %}
    {% endif %}
    restart: unless-stopped

  {% if database.value == "PostgreSQL" %}
  postgres:
    image: {{ db_config.docker_image }}
    container_name: {{ project_slug }}_postgres_db
    environment:
      POSTGRES_DB: mydb
      POSTGRES_USER: myuser
      POSTGRES_PASSWORD: mypassword
    volumes:
      - {{ project_slug }}_postgres_db:/var/lib/postgresql/data
    ports:
      - "5432:5432"
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U myuser -d mydb"]
      interval: 10s
      timeout: 5s
      retries: 5
    restart: unless-stopped
  {% elif database.value == "MySQL" %}
  mysql:
    image: {{ db_config.docker_image }}
    container_name: {{ project_slug }}_mysql_db
    environment:
      MYSQL_DATABASE: mydb
      MYSQL_USER: myuser
      MYSQL_PASSWORD: mypassword
      MYSQL_ROOT_PASSWORD: rootpassword
    volumes:
      - {{ project_slug }}_mysql_db:/var/lib/mysql
    ports:
      - "3306:3306"
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
      interval: 10s
      timeout: 5s
      retries: 5
    restart: unless-stopped
  {% endif %}

  {% if memory_store.value == "Redis" %}
  redis:
    image: {{ store_config.docker_image }}
    container_name: {{ project_slug }}_redis
    ports:
      - "{{ store_config.port }}:6379"
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5
    restart: unless-stopped
  {% elif memory_store.value == "Valkey" %}
  valkey:
    image: {{ store_config.docker_image }}
    container_name: {{ project_slug }}_valkey
    ports:
      - "{{ store_config.port }}:6379"
    healthcheck:
      test: ["CMD", "valkey-cli", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5
    restart: unless-stopped
  {% endif %}

{% if database.value == "PostgreSQL" %}
volumes:
  {{ project_slug }}_postgres_db: {}
{% elif database.value == "MySQL" %}
volumes:
  {{ project_slug }}_mysql_db: {}
{% endif %}
