version: "3.9"

services:
  # ── API FastAPI ──────────────────────────────────────────────
  app:
    build:
      context: .
      target: runtime   # Utilise le stage runtime du Dockerfile multi-stage
    container_name: taskapi_app
    ports:
      - "8000:8000"
    environment:
      - DATABASE_URL=postgresql://taskuser:taskpass@db:5432/taskapi
      - DEBUG=false
    env_file:
      - .env
    depends_on:
      db:
        condition: service_healthy   # Attend que Postgres soit prêt
    restart: unless-stopped
    healthcheck:
      test:
        - "CMD"
        - "python"
        - "-c"
        - "import urllib.request; urllib.request.urlopen('http://localhost:8000/health')"
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 10s
    networks:
      - taskapi_net

  # ── PostgreSQL ───────────────────────────────────────────────
  db:
    image: postgres:16-alpine
    container_name: taskapi_db
    environment:
      POSTGRES_USER: taskuser
      POSTGRES_PASSWORD: taskpass
      POSTGRES_DB: taskapi
    volumes:
      - postgres_data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U taskuser -d taskapi"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 10s
    restart: unless-stopped
    networks:
      - taskapi_net

# ── Volumes persistants ────────────────────────────────────────
volumes:
  postgres_data:
    driver: local

# ── Réseau isolé ─────────────────────────────────────────────
networks:
  taskapi_net:
    driver: bridge
