services:
  postgres:
    image: postgres:16-alpine
    container_name: pycharter-postgres
    environment:
      POSTGRES_DB: pycharter
      POSTGRES_USER: pycharter
      POSTGRES_PASSWORD: pycharter
    ports:
      - "5432:5432"
    volumes:
      - pycharter_pgdata:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U pycharter"]
      interval: 5s
      timeout: 3s
      retries: 5
    restart: unless-stopped

  api:
    build:
      context: .
      dockerfile: Dockerfile
      target: production
      args:
        SERVICE: api
    container_name: pycharter-api
    environment:
      PYCHARTER_DATABASE_URL: postgresql://pycharter:pycharter@postgres:5432/pycharter
      RUN_MIGRATIONS: "true"
      API_HOST: "0.0.0.0"
      API_PORT: "8002"
    ports:
      - "8002:8002"
    depends_on:
      postgres:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "python", "-c", "import urllib.request; urllib.request.urlopen('http://localhost:8002/health')"]
      interval: 30s
      timeout: 10s
      start_period: 15s
      retries: 3
    restart: unless-stopped

  worker:
    build:
      context: .
      dockerfile: Dockerfile
      target: production
      args:
        SERVICE: worker
    container_name: pycharter-worker
    environment:
      PYCHARTER_DATABASE_URL: postgresql://pycharter:pycharter@postgres:5432/pycharter
      WORKER_CONCURRENCY: "5"
      WORKER_POLL_INTERVAL: "500"
    depends_on:
      api:
        condition: service_healthy
    restart: unless-stopped

  ui:
    build:
      context: .
      dockerfile: Dockerfile
      target: production-ui
      args:
        SERVICE: ui
    container_name: pycharter-ui
    environment:
      PYCHARTER_API_URL: http://api:8002
      UI_HOST: "0.0.0.0"
      UI_PORT: "3002"
    ports:
      - "3002:3002"
    depends_on:
      api:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "python", "-c", "import urllib.request; urllib.request.urlopen('http://localhost:3002/health')"]
      interval: 30s
      timeout: 10s
      start_period: 15s
      retries: 3
    restart: unless-stopped

volumes:
  pycharter_pgdata:
    driver: local
