services:
  db:
    image: postgres:15-alpine
    environment:
      POSTGRES_DB: ${POSTGRES_DB:-dental_clinic}
      POSTGRES_USER: ${POSTGRES_USER:-dental}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?Set POSTGRES_PASSWORD in .env}
    ports:
      - "5432:5432"
    volumes:
      - pgdata:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER:-dental} -d ${POSTGRES_DB:-dental_clinic}"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 30s
    restart: unless-stopped

  backend:
    build: ./backend
    environment:
      DATABASE_URL: postgresql+asyncpg://${POSTGRES_USER:-dental}:${POSTGRES_PASSWORD}@db:5432/${POSTGRES_DB:-dental_clinic}
      SECRET_KEY: ${SECRET_KEY:?Set SECRET_KEY in .env}
      ENVIRONMENT: ${ENVIRONMENT:-development}
      ALLOWED_ORIGINS: ${ALLOWED_ORIGINS:-}
      STORAGE_BACKEND: ${STORAGE_BACKEND:-local}
      STORAGE_LOCAL_PATH: ${STORAGE_LOCAL_PATH:-/app/storage}
      # Copilot agentic layer (app/core/llm/). Set OPENAI_API_KEY in .env
      # (gitignored). Empty default keeps the stack bootable without it.
      OPENAI_API_KEY: ${OPENAI_API_KEY:-}
    ports:
      - "8000:8000"
    depends_on:
      db:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "python", "-c", "import urllib.request,sys; sys.exit(0 if urllib.request.urlopen('http://localhost:8000/health', timeout=2).status == 200 else 1)"]
      interval: 30s
      timeout: 5s
      retries: 3
      start_period: 60s
    restart: unless-stopped
    volumes:
      - ./backend:/app
      - storage_data:/app/storage
      # Mount the frontend root read-write so the backend can regenerate
      # modules.json when a module with a Nuxt layer is installed.
      - ./frontend:/host_frontend
    command: uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload --proxy-headers --forwarded-allow-ips=*

  frontend:
    build:
      context: ./frontend
      target: builder
      args:
        # The browser resolves this URL, so it must be reachable from the
        # device running it. Set API_BASE_URL=http://<host-lan-ip>:8000 in
        # .env (plus the same origin in ALLOWED_ORIGINS) to reach the app
        # from a phone or another machine.
        API_BASE_URL: ${API_BASE_URL:-http://localhost:8000}
    environment:
      NITRO_HOST: 0.0.0.0
      NITRO_PORT: 3000
      NUXT_PUBLIC_API_BASE_URL: ${API_BASE_URL:-http://localhost:8000}
      NODE_OPTIONS: --max-old-space-size=4096
    ports:
      - "3000:3000"
    depends_on:
      backend:
        condition: service_started
    volumes:
      - ./frontend:/app
      # Mount backend module packages read-only so Nuxt can `extends`
      # each module's layer (declared via `manifest.frontend.layer_path`).
      # The backend writes absolute paths under this mount into
      # `frontend/modules.json`.
      - ./backend/app/modules:/module_layers:ro
      - /app/node_modules
    healthcheck:
      test: ["CMD-SHELL", "wget -q -O /dev/null http://127.0.0.1:3000/ || exit 1"]
      interval: 30s
      timeout: 5s
      retries: 3
      start_period: 60s
    restart: unless-stopped
    command: npm run dev

volumes:
  pgdata:
  storage_data:
