version: '3.8'

services:
  db:
    image: postgres:16-alpine
    container_name: tipsharks_db
    environment:
      POSTGRES_USER: tipsharks
      POSTGRES_PASSWORD: tipsharks
      POSTGRES_DB: tipsharks
    ports:
      - "5432:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U tipsharks"]
      interval: 10s
      timeout: 5s
      retries: 5

  api:
    build:
      context: .
      dockerfile: infrastructure/docker/Dockerfile.api
    container_name: tipsharks_api
    ports:
      - "8000:8000"
    env_file:
      - .env
    environment:
      DATABASE_URL: postgresql+psycopg://tipsharks:tipsharks@db:5432/tipsharks
    depends_on:
      db:
        condition: service_healthy
    volumes:
      - .:/app
      - /app/venv
    healthcheck:
      test: ["CMD", "python", "-c", "import urllib.request; urllib.request.urlopen('http://localhost:8000/health', timeout=5)"]
      interval: 30s
      timeout: 5s
      retries: 3
      start_period: 10s
    command: uvicorn apps.backend.api.main:app --host 0.0.0.0 --port 8000 --reload
    # Note: API service includes Web UI (templates in apps/backend/web/templates, static files in apps/backend/web/static)
    # Web UI is accessible at: http://localhost:8000/ (homepage and all web pages)
    # API endpoints are at: http://localhost:8000/ratings/*, /races/*, etc.

  worker:
    build:
      context: .
      dockerfile: infrastructure/docker/Dockerfile.worker
    container_name: tipsharks_worker
    env_file:
      - .env
    environment:
      DATABASE_URL: postgresql+psycopg://tipsharks:tipsharks@db:5432/tipsharks
    depends_on:
      db:
        condition: service_healthy
    volumes:
      - .:/app
      - /app/venv
    # Worker runs on-demand, not as a persistent service
    profiles:
      - tools

  adminer:
    image: adminer:4
    container_name: tipsharks_adminer
    restart: unless-stopped
    ports:
      - "8080:8080"
    environment:
      ADMINER_DEFAULT_SERVER: db
    depends_on:
      db:
        condition: service_healthy

volumes:
  postgres_data:
