version: '3.8'

services:
  source-mysql:
    image: mariadb:10.6
    container_name: medikal-source-mysql
    restart: always
    ports:
      - "3307:3306"
    environment:
      - MARIADB_ALLOW_EMPTY_ROOT_PASSWORD=yes
      - MARIADB_DATABASE=medikal_saasdb
    volumes:
      - mysql-data:/var/lib/mysql
      - ./dumps/medikal_saasdb.sql:/docker-entrypoint-initdb.d/medikal_saasdb.sql:ro
    # Performance tweaks to speed up bulk SQL import 10x
    command: 
      - --innodb_doublewrite=0 
      - --innodb_flush_log_at_trx_commit=2 
      - --sync_binlog=0

  source-postgres:
    image: postgres:17-alpine
    container_name: medikal-source-postgres
    restart: always
    ports:
      - "5433:5432"
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
      - POSTGRES_DB=postgres
    volumes:
      - postgres-source-data:/var/lib/postgresql/data
      - ./dumps/supabase_schema.sql:/dumps/supabase_schema.sql:ro
      - ./dumps/hms_subset:/dumps/hms_subset:ro
      - ./scripts/init_source_postgres.sh:/docker-entrypoint-initdb.d/init_source_postgres.sh:ro

  target-omop:
    image: postgres:17-alpine
    container_name: medikal-target-omop
    restart: always
    ports:
      - "5434:5432"
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
      - POSTGRES_DB=postgres
    volumes:
      - postgres-omop-data:/var/lib/postgresql/data
      - ./scripts/omop_ddl.sql:/scripts/omop_ddl.sql:ro
      - ./scripts/omop_pk.sql:/scripts/omop_pk.sql:ro
      - ./scripts/omop_constraints.sql:/scripts/omop_constraints.sql:ro
      - ./scripts/omop_indexes.sql:/scripts/omop_indexes.sql:ro
      - ./scripts/init_target_omop.sh:/docker-entrypoint-initdb.d/init_target_omop.sh:ro

  achilles-runner:
    image: ohdsi/broadsea-methodslibrary:latest
    container_name: medikal-achilles
    profiles: ["profiling"]
    depends_on:
      - target-omop
    volumes:
      - ./scripts/run_achilles.R:/scripts/run_achilles.R:ro
    command: ["Rscript", "/scripts/run_achilles.R"]

  dqd-runner:
    build:
      context: ./scripts
      dockerfile: Dockerfile.dqd
    container_name: medikal-dqd
    profiles: ["profiling"]
    depends_on:
      - target-omop
    volumes:
      - ./scripts/run_dqd.R:/scripts/run_dqd.R:ro
      - ./reports:/reports
    command: ["Rscript", "/scripts/run_dqd.R"]

  medikal-api:
    build: ./api
    container_name: medikal-api
    restart: always
    ports:
      - "3034:8000"
    depends_on:
      - target-omop
    environment:
      - DATABASE_URL=postgresql://postgres:postgres@target-omop:5432/postgres

  medikal-web:
    build: ./web
    container_name: medikal-web
    ports:
      - "3033:3000"
    volumes:
      - ./web:/app
      - /app/node_modules
    depends_on:
      - medikal-api

volumes:
  mysql-data:
  postgres-source-data:
  postgres-omop-data:
