services:
  db:
    image: postgres:16-alpine
    container_name: nyval_db
    restart: unless-stopped
    environment:
      POSTGRES_USER: ${POSTGRES_USER:-nyval}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-nyval_dev_password}
      POSTGRES_DB: ${POSTGRES_DB:-nyval}
    ports:
      - "${POSTGRES_PORT:-5432}:5432"
    volumes:
      - nyval_db_data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER:-nyval} -d ${POSTGRES_DB:-nyval}"]
      interval: 5s
      timeout: 5s
      retries: 10

  backend:
    build: ./backend
    container_name: nyval_backend
    restart: unless-stopped
    depends_on:
      db:
        condition: service_healthy
    environment:
      NODE_ENV: ${NODE_ENV:-development}
      PORT: 8000
      POSTGRES_HOST: db
      POSTGRES_PORT: 5432
      POSTGRES_USER: ${POSTGRES_USER:-nyval}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-nyval_dev_password}
      POSTGRES_DB: ${POSTGRES_DB:-nyval}
      APP_URL: ${APP_URL:-http://localhost:8000}
      FRONTEND_URL: ${FRONTEND_URL:-http://localhost:5173}
      JWT_ACCESS_SECRET: ${JWT_ACCESS_SECRET}
      JWT_REFRESH_SECRET: ${JWT_REFRESH_SECRET}
      API_KEY_ENCRYPTION_KEY: ${API_KEY_ENCRYPTION_KEY}
      SHOPIFY_CLIENT_ID: ${SHOPIFY_CLIENT_ID}
      SHOPIFY_CLIENT_SECRET: ${SHOPIFY_CLIENT_SECRET}
      SHOPIFY_SCOPES: ${SHOPIFY_SCOPES:-read_products,write_products,read_orders}
      SHOPIFY_API_VERSION: ${SHOPIFY_API_VERSION:-2024-10}
      ANTHROPIC_API_KEY: ${ANTHROPIC_API_KEY}
      CLAUDE_MODEL: ${CLAUDE_MODEL:-claude-opus-4-8}
      NANO_BANANA_API_KEY: ${NANO_BANANA_API_KEY}
      NANO_BANANA_MODEL: ${NANO_BANANA_MODEL:-gemini-2.5-flash-image}
    ports:
      - "8000:8000"
    volumes:
      # Sans ce volume, storage/uploads (images IA générées, imports, photos de
      # mannequins) ne survit pas à une reconstruction du conteneur : COPY . .
      # dans le Dockerfile ne fige que l'état au moment du build, et tout
      # fichier écrit ensuite par l'app vit dans la couche éphémère du
      # conteneur, perdue au prochain `docker compose up --build`.
      - nyval_uploads_data:/app/storage/uploads
    command: sh -c "node node_modules/knex/bin/cli.js migrate:latest && node src/server.js"

  frontend:
    build: ./frontend
    container_name: nyval_frontend
    restart: unless-stopped
    depends_on:
      - backend
    environment:
      VITE_API_URL: ${APP_URL:-http://localhost:8000}
      # Cible du proxy interne Vite pour /api et /uploads (voir vite.config.js) —
      # à l'intérieur du conteneur, "localhost" ne route jamais vers le conteneur
      # backend : il faut le nom du service Docker Compose.
      BACKEND_URL: http://backend:8000
    ports:
      - "5173:5173"

volumes:
  nyval_db_data:
  nyval_uploads_data:
