x-logging: &default-logging
  driver: json-file
  options:
    max-size: "10m"
    max-file: "3"

x-app-common: &app-common
  restart: unless-stopped
  init: true
  security_opt:
    - no-new-privileges:true
  cap_drop:
    - ALL
  stop_grace_period: 30s
  logging: *default-logging

services:
  postgres:
    image: postgres:16-alpine
    restart: unless-stopped
    init: true
    environment:
      POSTGRES_DB: ${POSTGRES_DB:-discord_saas}
      POSTGRES_USER: ${POSTGRES_USER:-discord_saas}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?POSTGRES_PASSWORD is required}
      POSTGRES_INITDB_ARGS: --auth-host=scram-sha-256 --auth-local=scram-sha-256
    volumes:
      - postgres_data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U $$POSTGRES_USER -d $$POSTGRES_DB"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 15s
    security_opt:
      - no-new-privileges:true
    stop_grace_period: 30s
    networks:
      - backend_net
    logging: *default-logging

  redis:
    image: redis:7-alpine
    restart: unless-stopped
    init: true
    environment:
      REDIS_PASSWORD: ${REDIS_PASSWORD:?REDIS_PASSWORD is required}
    command:
      - sh
      - -ec
      - >
        exec redis-server
        --appendonly yes
        --save 60 1000
        --requirepass "$${REDIS_PASSWORD}"
        --maxmemory-policy noeviction
    volumes:
      - redis_data:/data
    healthcheck:
      test:
        ["CMD-SHELL", 'redis-cli -a "$${REDIS_PASSWORD}" ping | grep -q PONG']
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 10s
    security_opt:
      - no-new-privileges:true
    stop_grace_period: 20s
    networks:
      - backend_net
    logging: *default-logging

  api:
    <<: *app-common
    build:
      context: .
      dockerfile: apps/api/Dockerfile
    env_file:
      - .env
    environment:
      NODE_ENV: production
      PORT: 4000
      DATABASE_URL: postgresql://${POSTGRES_USER:-discord_saas}:${POSTGRES_PASSWORD}@postgres:5432/${POSTGRES_DB:-discord_saas}
      DATABASE_SSL: "false"
      REDIS_URL: redis://:${REDIS_PASSWORD}@redis:6379
      WEB_URL: ${WEB_URL}
      API_BASE_URL: ${API_BASE_URL}
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    ports:
      - "${API_PORT}:4000"
    networks:
      - edge_net
      - backend_net

  bot:
    <<: *app-common
    build:
      context: .
      dockerfile: apps/bot/Dockerfile
    env_file:
      - .env
    environment:
      NODE_ENV: production
      PORT: 4100
      DATABASE_URL: postgresql://${POSTGRES_USER:-discord_saas}:${POSTGRES_PASSWORD}@postgres:5432/${POSTGRES_DB:-discord_saas}
      DATABASE_SSL: "false"
      REDIS_URL: redis://:${REDIS_PASSWORD}@redis:6379
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
      api:
        condition: service_started
    networks:
      - edge_net
      - backend_net

  web:
    <<: *app-common
    build:
      context: .
      dockerfile: apps/web/Dockerfile
      args:
        NEXT_PUBLIC_API_BASE_URL: ${NEXT_PUBLIC_API_BASE_URL}
        NEXT_PUBLIC_MATOMO_URL: ${NEXT_PUBLIC_MATOMO_URL}
        NEXT_PUBLIC_MATOMO_SITE_ID: ${NEXT_PUBLIC_MATOMO_SITE_ID}
    env_file:
      - .env
    environment:
      NODE_ENV: production
      PORT: 3000
      NEXT_PUBLIC_API_BASE_URL: ${NEXT_PUBLIC_API_BASE_URL}
      NEXT_PUBLIC_MATOMO_URL: ${NEXT_PUBLIC_MATOMO_URL}
      NEXT_PUBLIC_MATOMO_SITE_ID: ${NEXT_PUBLIC_MATOMO_SITE_ID}
      API_BASE_URL: http://api:4000
    depends_on:
      api:
        condition: service_started
    ports:
      - "${WEB_PORT}:3000"
    networks:
      - edge_net
      - backend_net

volumes:
  postgres_data:
  redis_data:

networks:
  edge_net:
    driver: bridge
  backend_net:
    driver: bridge
    internal: true
