services:
  postgres:
    image: postgres:15-alpine
    env_file:
      - ../../.env.prod
    ports:
      - "5432:5432"
    volumes:
      - backend_postgres_data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U $${POSTGRES_USER} -d $${POSTGRES_DB}"]
      interval: 10s
      timeout: 5s
      retries: 10
    restart: unless-stopped

  redis:
    image: redis:7-alpine
    env_file:
      - ../../.env.prod
    command: ["sh", "-c", "redis-server --requirepass \"$$REDIS_PASSWORD\""]
    ports:
      - "6379:6379"
    volumes:
      - backend_redis_data:/data
    healthcheck:
      test: ["CMD-SHELL", "redis-cli -a \"$${REDIS_PASSWORD}\" ping | grep PONG"]
      interval: 10s
      timeout: 5s
      retries: 10
    restart: unless-stopped

  backend:
    build:
      context: .
      dockerfile: Dockerfile
    env_file:
      - ../../.env.prod
    ports:
      - "8080:8080"
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://127.0.0.1:8080/health"]
      interval: 30s
      timeout: 10s
      retries: 3

volumes:
  backend_postgres_data:
  backend_redis_data:
