services:
  # ==========================================================
  # PostgreSQL Database
  # ==========================================================
  postgres:
    image: postgres:17
    container_name: stickyboard_db
    restart: unless-stopped
    env_file:
      - .env
    environment:
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      POSTGRES_DB: ${POSTGRES_DB}
    volumes:
      - sticky_pgdata:/var/lib/postgresql/data
      - ./db/init:/docker-entrypoint-initdb.d   # auto-seed SQL
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER}"]
      interval: 10s
      timeout: 5s
      retries: 5
    networks:
      - sticky_net
    ports:
      - "5432:5432"  # kept open for dev; disable in prod if needed

  # ==========================================================
  # StickyBoard API
  # ==========================================================
  api:
    build: 
      context: .
      dockerfile: api/Dockerfile
    container_name: stickyboard_api
    depends_on:
      - postgres
    env_file:
      - .env
    environment:
      DATABASE_URL: postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${DB_HOST:-postgres}:5432/${POSTGRES_DB}
      WORKER_API_KEY: ${WORKER_API_KEY}
      ASPNETCORE_ENVIRONMENT: ${ASPNETCORE_ENVIRONMENT}
      ASPNETCORE_URLS: ${ASPNETCORE_URLS}
    volumes:
      - ./logs/api:/app/logs
    ports:
      - "8080:8080"
    restart: unless-stopped
    networks:
      - sticky_net

  # ==========================================================
  # StickyBoard Background Worker
  # ==========================================================
  worker:
    build:
      context: .
      dockerfile: worker/Dockerfile
    container_name: stickyboard_worker
    depends_on:
      - postgres
      - api
    env_file:
      - .env
    environment:
      DATABASE_URL: postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${DB_HOST:-postgres}:5432/${POSTGRES_DB}
      ASPNETCORE_ENVIRONMENT: ${ASPNETCORE_ENVIRONMENT}
      WORKER_API_KEY: ${WORKER_API_KEY}
    volumes:
      - ./logs/worker:/app/logs
    restart: unless-stopped
    networks:
      - sticky_net

# ==========================================================
# Networks & Volumes
# ==========================================================
networks:
  sticky_net:
    driver: bridge

volumes:
  sticky_pgdata:
