version: "3.9"

services:
  # Prefect Server - UI и API
  prefect-server:
    image: prefecthq/prefect:2-python3.11
    command: prefect server start --host 0.0.0.0
    ports:
      - "4200:4200"
    environment:
      PREFECT_SERVER_API_HOST: 0.0.0.0
      PREFECT_SERVER_API_PORT: 4200
      PREFECT_API_DATABASE_CONNECTION_URL: postgresql+asyncpg://prefect:prefect@postgres:5432/prefect
    volumes:
      - prefect-data:/root/.prefect
    depends_on:
      postgres:
        condition: service_healthy
    restart: unless-stopped
    networks:
      - prefect-network

  # Prefect Worker - выполняет flows
  prefect-worker:
    build:
      context: .
      dockerfile: Dockerfile
    environment:
      PREFECT_API_URL: http://prefect-server:4200/api
      # Эти переменные будут загружены из Infisical на Dokploy
      LOGTO_MANAGEMENT_API_ENDPOINT: ${LOGTO_MANAGEMENT_API_ENDPOINT}
      LOGTO_M2M_APP_ID: ${LOGTO_M2M_APP_ID}
      LOGTO_M2M_APP_SECRET: ${LOGTO_M2M_APP_SECRET}
      LOGTO_M2M_RESOURCE: ${LOGTO_M2M_RESOURCE}
      TEAMS_BACKEND_URL: ${TEAMS_BACKEND_URL}
    depends_on:
      prefect-server:
        condition: service_started
    restart: unless-stopped
    networks:
      - prefect-network

  # PostgreSQL для Prefect Server
  postgres:
    image: postgres:15-alpine
    environment:
      POSTGRES_USER: prefect
      POSTGRES_PASSWORD: prefect
      POSTGRES_DB: prefect
    volumes:
      - postgres-data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U prefect"]
      interval: 10s
      timeout: 5s
      retries: 5
    restart: unless-stopped
    networks:
      - prefect-network

volumes:
  prefect-data:
  postgres-data:

networks:
  prefect-network:
    driver: bridge
