services:
  postgres:
    image: postgres:16.2
    container_name: rb-northwind-postgres
    environment:
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      POSTGRES_DB: ${POSTGRES_DB}
    ports:
      - "${POSTGRES_PORT}:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data
    command:
      - postgres
      - -c
      - max_connections=500
    restart: unless-stopped
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U $POSTGRES_USER -d $POSTGRES_DB"]
      interval: 10s
      timeout: 5s
      retries: 5

  mysql:
    image: mysql:8.3.0
    container_name: rb-northwind-mysql
    environment:
      MYSQL_ROOT_PASSWORD: ${MYSQL_PASSWORD}
      MYSQL_DATABASE: ${MYSQL_DB}
      MYSQL_ROOT_HOST: '%'
    ports:
      - "${MYSQL_PORT}:3306"
    volumes:
      - mysql_data:/var/lib/mysql
    command: --general-log=1 --general-log-file=/var/log/mysql/mysql-general.log --max_connections=500
    restart: unless-stopped
    healthcheck:
      test: ["CMD-SHELL", "mysqladmin ping -h localhost -u root -p$MYSQL_PASSWORD"]
      interval: 10s
      timeout: 5s
      retries: 5
      
  mariadb:
    image: mariadb:11.3.2
    container_name: rb-northwind-mariadb
    environment:
      MARIADB_ROOT_PASSWORD: ${MARIADB_PASSWORD}
      MARIADB_DATABASE: ${MARIADB_DB}
      MARIADB_ROOT_HOST: '%'
    ports:
      - "${MARIADB_PORT}:3306"  # set MARIADB_PORT (e.g. 3307) if you need to avoid conflicts
    volumes:
      - mariadb_data:/var/lib/mysql
    command: --general-log=1 --general-log-file=/var/log/mysql/mariadb-general.log --max_connections=500
    restart: unless-stopped
    #healthcheck:
    #  test: ["CMD-SHELL", "mysqladmin ping -h localhost -u root -p$MARIADB_PASSWORD"]
    #  interval: 10s
    #  timeout: 5s
    #  retries: 5

  sqlserver:
    image: mcr.microsoft.com/mssql/server:2019-latest
    container_name: rb-northwind-sqlserver
    environment:
      ACCEPT_EULA: Y
      MSSQL_SA_PASSWORD: ${SQLSERVER_PASSWORD}
      MSSQL_PID: Developer
    ports:
      - "${SQLSERVER_PORT}:1433"
    volumes:
      - sqlserver_data:/var/opt/mssql
    restart: unless-stopped
    #healthcheck:
    #  test: ["CMD-SHELL", "/opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P $SQLSERVER_PASSWORD -Q \"SELECT 1;\""]
    #  interval: 30s
    #  timeout: 10s
    #  retries: 10

  oracle:
    image: gvenzl/oracle-xe:21-slim-faststart
    container_name: rb-northwind-oracle
    environment:
      ORACLE_PASSWORD: ${ORACLE_PASSWORD:-oracle}
      APP_USER: ${ORACLE_USER:-oracle}
      APP_USER_PASSWORD: ${ORACLE_PASSWORD:-oracle}
      ORACLE_CHARACTERSET: AL32UTF8
    ports:
      - "${ORACLE_PORT:-1521}:1521"
    volumes:
      - oracle_data:/opt/oracle/oradata
    restart: unless-stopped
    command: --databaseConfigurationType=SMALL
    healthcheck:
      test: ["CMD", "healthcheck.sh"]
      interval: 10s
      timeout: 5s
      retries: 10

  db2:
    image: icr.io/db2_community/db2:11.5.9.0
    container_name: rb-northwind-db2
    privileged: true
    environment:
      LICENSE: accept
      DB2INST1_PASSWORD: ${DB2_PASSWORD}
      DBNAME: ${DB2_DB}
      PERSISTENT_HOME: "false"
      ARCHIVE_LOGS: "false"
      BLU: "false"
    ports:
      - "${DB2_PORT}:50000"
    volumes:
      - db2_data:/database
    healthcheck:
      test: ["CMD-SHELL", "su - db2inst1 -c 'db2 connect to $DB2_DB >/dev/null 2>&1 && db2 terminate'"]
      interval: 30s
      timeout: 10s
      retries: 30
    restart: unless-stopped

  clickhouse:
    image: clickhouse/clickhouse-server:24.3-alpine
    container_name: rb-northwind-clickhouse
    environment:
      CLICKHOUSE_USER: ${CLICKHOUSE_USER:-default}
      CLICKHOUSE_PASSWORD: ${CLICKHOUSE_PASSWORD:-clickhouse}
      CLICKHOUSE_DB: ${CLICKHOUSE_DB:-northwind}
    ports:
      - "${CLICKHOUSE_PORT:-8123}:8123"   # HTTP interface (queries via curl, BI tools, etc.)
      - "9000:9000"   # Native TCP interface (clients, drivers)
    volumes:
      - clickhouse_data:/var/lib/clickhouse
      - clickhouse_logs:/var/log/clickhouse-server
    restart: unless-stopped
    healthcheck:
      test: ["CMD-SHELL", "wget -qO- http://127.0.0.1:8123/ping || exit 1"]
      interval: 10s
      timeout: 5s
      retries: 10
      start_period: 10s

  redis:
    image: redis:7-alpine
    container_name: rb-cache-redis
    ports:
      - "${REDIS_PORT:-6379}:6379"
    volumes:
      - redis_data:/data
    command: redis-server --requirepass ${REDIS_PASSWORD:-redis}
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "redis-cli", "-a", "${REDIS_PASSWORD:-redis}", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5

  # ===================================================================
  # CLICKHOUSE CDC SINK CONNECTOR (DISABLED BY DEFAULT)
  # ===================================================================
  # Enables real-time CDC replication from MySQL/PostgreSQL to ClickHouse
  # using Debezium + Altinity Sink Connector in lightweight mode (no Kafka).
  #
  # PREREQUISITES:
  # - ClickHouse database accessible (use the clickhouse service above, or your own)
  # - Source database with binlog (MySQL) or WAL (PostgreSQL) enabled
  # - Replication user created in source database
  #
  # See CONFIGURE_OLTP_2_OLAP_DATA_WAREHOUSE_SYNC.md for detailed setup instructions.
  # ===================================================================

  # Altinity ClickHouse Sink Connector (Lightweight Mode)
  # clickhouse-sink-connector:
  #   image: altinityinfra/clickhouse-sink-connector:latest
  #   container_name: fkraft-ch-sink
  #   environment:
  #     # ─────────────────────────────────────────────────────────────
  #     # SOURCE DATABASE CONFIGURATION
  #     # ─────────────────────────────────────────────────────────────
  #     # MySQL Source (default)
  #     - DATABASE_HOSTNAME=host.docker.internal  # Your MySQL/PostgreSQL host
  #     - DATABASE_PORT=3306                      # 3306 for MySQL, 5432 for PostgreSQL
  #     - DATABASE_USER=replicator
  #     - DATABASE_PASSWORD=repl_password         # CHANGE THIS!
  #     - DATABASE_NAME=your_database
  #     - DATABASE_SERVER_NAME=mysql-source
  #     - DATABASE_ALLOWPUBLICKEYRETRIEVAL=true
  #     
  #     # For PostgreSQL, also set:
  #     # - CONNECTOR_CLASS=io.debezium.connector.postgresql.PostgresConnector
  #     # - SLOT_NAME=ch_sink_slot
  #     # - PUBLICATION_NAME=my_publication
  #     
  #     # ─────────────────────────────────────────────────────────────
  #     # CLICKHOUSE TARGET CONFIGURATION
  #     # ─────────────────────────────────────────────────────────────
  #     - CLICKHOUSE_URL=http://clickhouse:8123
  #     - CLICKHOUSE_USER=default
  #     - CLICKHOUSE_PASSWORD=clickhouse            # MUST MATCH CLICKHOUSE ABOVE!
  #     - CLICKHOUSE_DATABASE=northwind
  #     
  #     # ─────────────────────────────────────────────────────────────
  #     # REPLICATION SETTINGS
  #     # ─────────────────────────────────────────────────────────────
  #     # Tables to replicate (comma-separated database.table)
  #     - TABLE_INCLUDE_LIST=your_database.orders,your_database.customers
  #     
  #     # Lightweight mode (no Kafka required)
  #     - AUTO_CREATE_TABLES=true
  #     - SINK_CONNECTOR_LIGHTWEIGHT_UPDATE_DELETE=true
  #   restart: unless-stopped
  #   healthcheck:
  #     test: ["CMD-SHELL", "curl -f http://localhost:8083/connectors || exit 1"]
  #     interval: 30s
  #     timeout: 10s
  #     retries: 3
  #     start_period: 60s

  # ===================================================================
  # DBT TRANSFORM SERVICE (DISABLED BY DEFAULT)
  # ===================================================================
  # Transforms raw OLTP data in ClickHouse into OLAP star schema
  # (fact + dimension tables) using dbt Core + dbt-clickhouse adapter.
  #
  # This is a CLI tool (runs and exits), NOT a long-running service.
  # Usage:
  #   docker compose run dbt-transform run       # Run all models
  #   docker compose run dbt-transform test      # Run tests
  #   docker compose run dbt-transform debug     # Test connection
  #
  # PREREQUISITES:
  # - ClickHouse running with raw OLTP tables loaded
  #   (via Java initialization OR CDC sink connector replication)
  #
  # See CONFIGURE_ETL.md for detailed setup instructions.
  # ===================================================================

  # dbt-transform:
  #   build:
  #     context: ./dbt
  #     dockerfile: Dockerfile
  #   container_name: rb-northwind-dbt
  #   depends_on:
  #     clickhouse:
  #       condition: service_healthy
  #   profiles:
  #     - transform
  
volumes:
  postgres_data:
  mysql_data:
  mariadb_data:
  sqlserver_data:
  oracle_data:
  db2_data:
  clickhouse_data:
  clickhouse_logs:
  redis_data:
