# Document Translation API - Development Docker Compose
# Usage: docker compose -f docker-compose.dev.yml up
#        (or: docker-compose -f docker-compose.dev.yml up)
#
# Starts: backend (FastAPI :8000), frontend (Next.js :3000), PostgreSQL, Redis
# Hot reload: backend and frontend code mounted as volumes
# Environment: loaded from .env at project root (env_file and Compose substitution)

version: '3.8'

services:
  # ===========================================
  # PostgreSQL Database
  # ===========================================
  postgres:
    image: postgres:16-alpine
    container_name: translate-postgres-dev
    environment:
      - POSTGRES_USER=${POSTGRES_USER:-translate}
      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD:-translate_secret_123}
      - POSTGRES_DB=${POSTGRES_DB:-translate_db}
      - PGDATA=/var/lib/postgresql/data/pgdata
    volumes:
      - postgres_data_dev:/var/lib/postgresql/data
    networks:
      - translate-network
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER:-translate} -d ${POSTGRES_DB:-translate_db}"]
      interval: 5s
      timeout: 5s
      retries: 5
      start_period: 10s

  # ===========================================
  # Redis
  # ===========================================
  redis:
    image: redis:7-alpine
    container_name: translate-redis-dev
    command: redis-server --appendonly yes --maxmemory 256mb --maxmemory-policy allkeys-lru
    volumes:
      - redis_data_dev:/data
    networks:
      - translate-network
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 5s
      timeout: 5s
      retries: 5
      start_period: 5s

  # ===========================================
  # Backend API (FastAPI) - hot reload
  # ===========================================
  backend:
    build:
      context: .
      dockerfile: docker/backend/Dockerfile
      target: builder
    container_name: translate-backend-dev
    env_file:
      - .env
    environment:
      - DEBUG=true
      - LOG_LEVEL=DEBUG
      - DATABASE_URL=postgresql+asyncpg://${POSTGRES_USER:-translate}:${POSTGRES_PASSWORD:-translate_secret_123}@postgres:5432/${POSTGRES_DB:-translate_db}
      - REDIS_URL=redis://redis:6379/0
    volumes:
      - .:/app
      - backend_venv:/opt/venv
    command: uvicorn main:app --host 0.0.0.0 --port 8000 --reload
    ports:
      - "8000:8000"
    networks:
      - translate-network
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    healthcheck:
      test: ["CMD-SHELL", "python -c \"import urllib.request; urllib.request.urlopen('http://localhost:8000/health')\" || exit 1"]
      interval: 10s
      timeout: 5s
      retries: 3
      start_period: 15s

  # ===========================================
  # Frontend (Next.js) - hot reload
  # ===========================================
  frontend:
    build:
      context: .
      dockerfile: docker/frontend/Dockerfile
      target: builder
      args:
        - NEXT_PUBLIC_API_URL=${NEXT_PUBLIC_API_URL:-http://localhost:8000}
    container_name: translate-frontend-dev
    env_file:
      - .env
    environment:
      - NODE_ENV=development
      # Proxy Next : navigateur → /api/* → backend (évite CORS et l’hostname `backend` côté client)
      - BACKEND_URL=http://backend:8000
      # Vide = URLs relatives + proxy. Sinon URL joignable depuis le navigateur (ex. http://localhost:8000)
      - NEXT_PUBLIC_API_URL=${NEXT_PUBLIC_API_URL:-}
    volumes:
      - ./frontend:/app
      - frontend_node_modules:/app/node_modules
      - frontend_next:/app/.next
    command: npm run dev
    ports:
      - "3000:3000"
    networks:
      - translate-network
    depends_on:
      backend:
        condition: service_healthy

# ===========================================
# Networks
# ===========================================
networks:
  translate-network:
    driver: bridge

# ===========================================
# Volumes (dev-only names to avoid clashing with prod)
# ===========================================
volumes:
  postgres_data_dev:
    driver: local
  redis_data_dev:
    driver: local
  backend_venv:
    driver: local
  frontend_node_modules:
    driver: local
  frontend_next:
    driver: local
