services:
  postgres:
    image: postgres:17-alpine
    environment:
      POSTGRES_USER: ${POSTGRES_USER:-train_watcher}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-train_watcher}
      POSTGRES_DB: ${POSTGRES_DB:-train_watcher}
    ports:
      - "5432:5432"
    volumes:
      - postgres-data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER:-train_watcher} -d ${POSTGRES_DB:-train_watcher}"]
      interval: 5s
      timeout: 5s
      retries: 20

  minio:
    image: minio/minio:RELEASE.2025-04-22T22-12-26Z
    command: server /data --console-address ":9001"
    environment:
      MINIO_ROOT_USER: ${S3_ACCESS_KEY_ID:-minioadmin}
      MINIO_ROOT_PASSWORD: ${S3_SECRET_ACCESS_KEY:-minioadmin}
    ports:
      - "9000:9000"
      - "9001:9001"
    volumes:
      - minio-object-data:/data
    healthcheck:
      test: ["CMD", "mc", "ready", "local"]
      interval: 5s
      timeout: 5s
      retries: 20

  minio-init:
    image: minio/mc:RELEASE.2025-04-16T18-13-26Z
    depends_on:
      minio:
        condition: service_healthy
    environment:
      S3_ACCESS_KEY_ID: ${S3_ACCESS_KEY_ID:-minioadmin}
      S3_SECRET_ACCESS_KEY: ${S3_SECRET_ACCESS_KEY:-minioadmin}
      S3_BUCKET: ${S3_BUCKET:-train-watcher-media}
    entrypoint: >
      /bin/sh -c "
      mc alias set local http://minio:9000 $$S3_ACCESS_KEY_ID $$S3_SECRET_ACCESS_KEY &&
      mc mb --ignore-existing local/$$S3_BUCKET &&
      mc anonymous set download local/$$S3_BUCKET
      "

  logic:
    build:
      context: ../services
      dockerfile: logic/Dockerfile
    environment:
      DATABASE_URL: ${LOGIC_DATABASE_URL:-postgresql+psycopg://train_watcher:train_watcher@host.docker.internal:5432/train_watcher}
      SERVICE_TOKEN: ${SERVICE_TOKEN:-dev-service-token-change-me}
      S3_ENDPOINT_URL: ${S3_ENDPOINT_URL:-http://host.docker.internal:9000}
      S3_PUBLIC_BASE_URL: ${S3_PUBLIC_BASE_URL:-http://localhost:9000}
      S3_ACCESS_KEY_ID: ${S3_ACCESS_KEY_ID:-minioadmin}
      S3_SECRET_ACCESS_KEY: ${S3_SECRET_ACCESS_KEY:-minioadmin}
      S3_BUCKET: ${S3_BUCKET:-train-watcher-media}
      S3_REGION: ${S3_REGION:-us-east-1}
      BUILTIN_ASSETS_DIR: /app/workout_assets
    extra_hosts:
      - "host.docker.internal:host-gateway"
    ports:
      - "8002:8000"
    volumes:
      - ../workout_assets:/app/workout_assets:ro
    depends_on:
      postgres:
        condition: service_healthy
      minio-init:
        condition: service_completed_successfully
    healthcheck:
      test: ["CMD", "/app/.venv/bin/python", "-c", "import urllib.request; urllib.request.urlopen('http://127.0.0.1:8000/health', timeout=2)"]
      interval: 5s
      timeout: 5s
      start_period: 120s
      retries: 20

  bff:
    build:
      context: ../services
      dockerfile: bff/Dockerfile
    environment:
      DATABASE_URL: ${BFF_DATABASE_URL:-postgresql+psycopg://train_watcher:train_watcher@host.docker.internal:5432/train_watcher}
      LOGIC_BASE_URL: ${LOGIC_BASE_URL:-http://host.docker.internal:8002}
      SERVICE_TOKEN: ${SERVICE_TOKEN:-dev-service-token-change-me}
      JWT_SECRET: ${JWT_SECRET:-dev-jwt-secret-change-me}
      S3_ENDPOINT_URL: ${S3_ENDPOINT_URL:-http://host.docker.internal:9000}
      S3_PUBLIC_BASE_URL: ${S3_PUBLIC_BASE_URL:-http://localhost:9000}
      S3_ACCESS_KEY_ID: ${S3_ACCESS_KEY_ID:-minioadmin}
      S3_SECRET_ACCESS_KEY: ${S3_SECRET_ACCESS_KEY:-minioadmin}
      S3_BUCKET: ${S3_BUCKET:-train-watcher-media}
      S3_REGION: ${S3_REGION:-us-east-1}
    extra_hosts:
      - "host.docker.internal:host-gateway"
    ports:
      - "8001:8000"
    depends_on:
      postgres:
        condition: service_healthy
      logic:
        condition: service_healthy
      minio-init:
        condition: service_completed_successfully
    healthcheck:
      test: ["CMD", "/app/.venv/bin/python", "-c", "import urllib.request; urllib.request.urlopen('http://127.0.0.1:8000/health', timeout=2)"]
      interval: 5s
      timeout: 5s
      start_period: 120s
      retries: 20

  frontend:
    build:
      context: ../frontend
      args:
        VITE_API_BASE_URL: ${VITE_API_BASE_URL:-/api}
    ports:
      - "5173:80"
    extra_hosts:
      - "host.docker.internal:host-gateway"
    depends_on:
      bff:
        condition: service_healthy

volumes:
  postgres-data:
  minio-object-data:
