services:
  main:
    image: x-financial-dev:latest
    container_name: x-financial-main
    restart: unless-stopped
    depends_on:
      onlyoffice:
        condition: service_started
    environment:
      WEB_HOST: 0.0.0.0
      SERVER_HOST: 0.0.0.0
      SERVER_VENV_DIR: /tmp/x-financial-server-venv
      ONLYOFFICE_ENABLED: "${ONLYOFFICE_ENABLED:-true}"
      ONLYOFFICE_PUBLIC_URL: "${ONLYOFFICE_PUBLIC_URL:-http://127.0.0.1:${ONLYOFFICE_PORT:-8082}}"
      ONLYOFFICE_BACKEND_URL: "${ONLYOFFICE_BACKEND_URL:-http://main:${SERVER_PORT:-8000}}"
      ONLYOFFICE_JWT_SECRET: "${ONLYOFFICE_JWT_SECRET:-x-financial-onlyoffice-dev-secret}"
    ports:
      - "${WEB_PORT:-5173}:${WEB_PORT:-5173}"
      - "${SERVER_PORT:-8000}:${SERVER_PORT:-8000}"
      - "2223:22"
    volumes:
      - .:/app
    working_dir: /app
    command:
      - /bin/sh
      - -lc
      - >
        apt-get update &&
        DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends
        python3 python3-pip python3-venv &&
        mkdir -p /run/sshd && /usr/sbin/sshd &&
        printf '%s\n' 'cd /app >/dev/null 2>&1 || true' > /etc/profile.d/zz-x-financial-app-dir.sh &&
        chmod 644 /etc/profile.d/zz-x-financial-app-dir.sh &&
        touch /root/.bashrc /root/.profile &&
        if ! grep -qxF 'cd /app >/dev/null 2>&1 || true' /root/.bashrc; then printf '\ncd /app >/dev/null 2>&1 || true\n' >> /root/.bashrc; fi &&
        if ! grep -qxF 'cd /app >/dev/null 2>&1 || true' /root/.profile; then printf '\ncd /app >/dev/null 2>&1 || true\n' >> /root/.profile; fi &&
        sed -i 's/\r$//' /app/start.sh /app/web/web_start.sh /app/server/server_start.sh &&
        chmod +x /app/start.sh /app/web/web_start.sh /app/server/server_start.sh &&
        cd /app &&
        ./start.sh all
    healthcheck:
      test: ["CMD-SHELL", "curl -fsS http://127.0.0.1:${WEB_PORT:-5173}/ >/dev/null || exit 1"]
      interval: 15s
      timeout: 5s
      retries: 10
      start_period: 180s

  onlyoffice:
    image: onlyoffice/documentserver:latest
    container_name: x-financial-onlyoffice
    restart: unless-stopped
    environment:
      JWT_ENABLED: "true"
      JWT_SECRET: "${ONLYOFFICE_JWT_SECRET:-x-financial-onlyoffice-dev-secret}"
    ports:
      - "${ONLYOFFICE_PORT:-8082}:80"
    healthcheck:
      test: ["CMD-SHELL", "curl -fsS http://127.0.0.1/healthcheck >/dev/null || exit 1"]
      interval: 15s
      timeout: 5s
      retries: 10
      start_period: 60s
