version: "3.8"

services:
  backend:
    container_name: imk-backend
    build:
      context: ./backend
      dockerfile: Dockerfile
      args:
        - NODE_ENV=${NODE_ENV}
        # - API_URL=${API_URL}
        # - CORS_ORIGIN=${CORS_ORIGIN}
        # - DATABASE_URL=${DATABASE_URL}
        # - JWT_SECRET=${JWT_SECRET}
        # - SMTP_HOST=${SMTP_HOST}
        # - SMTP_PORT=${SMTP_PORT}
        # - SMTP_USER=${SMTP_USER}
        # - SMTP_PASS=${SMTP_PASS}
        # - EMAIL_FROM=${EMAIL_FROM}
    ports:
      - "3000:3000"
    environment:
      - NODE_ENV=${NODE_ENV}
      - API_URL=${API_URL}
      - CORS_ORIGIN=${CORS_ORIGIN}
      - DATABASE_URL=postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@postgres:5432/${POSTGRES_DB}
      - JWT_SECRET=${JWT_SECRET}
      - SMTP_HOST=${SMTP_HOST}
      - SMTP_PORT=${SMTP_PORT}
      - SMTP_USER=${SMTP_USER}
      - SMTP_PASS=${SMTP_PASS}
      - EMAIL_FROM=${EMAIL_FROM}
      - CORS_ORIGINS=http://localhost:5173,https://www.placebo.mk,https://placebo.mk
      - CORS_CREDENTIALS=true
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    networks:
      - app_network

  postgres:
    container_name: imk-postgres
    image: postgres:14-alpine
    environment:
      - DATABASE_URL=postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@postgres:5432/${POSTGRES_DB}
      - POSTGRES_USER=${POSTGRES_USER}
      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
      - POSTGRES_DB=${POSTGRES_DB}
    volumes:
      - postgres_data:/var/lib/postgresql/data
    networks:
      - app_network
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER}"]
      interval: 10s
      timeout: 5s
      retries: 5

  redis:
    container_name: imk-redis
    image: redis:alpine
    command: redis-server --requirepass ${REDIS_PASSWORD}
    environment:
      - REDIS_PASSWORD=${REDIS_PASSWORD}
    volumes:
      - redis_data:/data
    networks:
      - app_network
    healthcheck:
      test: ["CMD", "redis-cli", "-a", "${REDIS_PASSWORD}", "ping"]
      interval: 10s
      timeout: 5s
      retries: 3

networks:
  app_network:
    driver: bridge

volumes:
  postgres_data:
  redis_data:
