services:
  deps:
    image: node:20-alpine
    working_dir: /app
    env_file:
      - .env.development
    environment:
      - NODE_ENV=development
      - NPM_CONFIG_FUND=false
      - NPM_CONFIG_AUDIT=false
      - NPM_CONFIG_UPDATE_NOTIFIER=false
    volumes:
      - ./:/app
      - dev_node_modules:/app/node_modules
      - dev_npm_cache:/root/.npm
    command:
      [
        "sh",
        "-lc",
        "set -e; echo '[deps] Preparing dependency installation...'; MARKER=\"node_modules/.gridpilot_lock_hash_dev\"; LOCK_HASH=\"$$(sha1sum package-lock.json | awk '{print $$1}')\"; if [ -f \"$$MARKER\" ] && [ \"$$(cat \"$$MARKER\")\" = \"$$LOCK_HASH\" ]; then echo \"[deps] node_modules up-to-date\"; exit 0; fi; echo '[deps] Cleaning up any existing node_modules to avoid conflicts...'; find /app -name 'node_modules' -type d -path '*/apps/*' -exec rm -rf {} + 2>/dev/null || true; find /app -name '.react-*' -type d -path '*/apps/*/node_modules/*' -exec rm -rf {} + 2>/dev/null || true; echo '[deps] installing workspace deps (slow first time)'; npm install --no-package-lock --include-workspace-root --no-audit --fund=false --prefer-offline; node -e \"require.resolve('ts-node-dev')\"; node -e \"require.resolve('next')\"; echo \"$$LOCK_HASH\" > \"$$MARKER\"; echo '[deps] Dependencies installed successfully'",
      ]
    networks:
      - gridpilot-network
    restart: "no"

  api:
    image: gridpilot-api-dev
    build:
      context: .
      dockerfile: apps/api/Dockerfile.dev
      args:
        - NODE_ENV=development
    working_dir: /app/apps/api
    env_file:
      - .env.development
    environment:
      - NODE_ENV=development
      # IMPORTANT: do not override values provided by env_file (.env.development).
      # Using ${VAR:-} here expands to an empty string when VAR is not set in the
      # host shell, which *overrides* env_file and disables force reseed.
      - MEDIA_STORAGE_DIR=/data/media
      - GRIDPILOT_API_FORCE_RESEED=${GRIDPILOT_API_FORCE_RESEED:-}
    ports:
      - "3001:3000"
      - "9229:9229"
    volumes:
      - ./:/app
      - dev_node_modules:/app/node_modules
      - dev_npm_cache:/root/.npm
      - dev_media_data:/data/media
    command: ["sh", "-lc", "echo '[api] Waiting for dependencies...'; npm run start:dev"]
    depends_on:
      deps:
        condition: service_completed_successfully
      db:
        condition: service_healthy
    networks:
      - gridpilot-network
    restart: "no"
    healthcheck:
      test: ["CMD", "node", "-e", "fetch('http://localhost:3000/health').then(r=>process.exit(r.ok?0:1)).catch(()=>process.exit(1))"]
      interval: 5s
      timeout: 10s
      retries: 15
      start_period: 30s

  website:
    image: gridpilot-website-dev
    build:
      context: .
      dockerfile: apps/website/Dockerfile.dev
      args:
        - NODE_ENV=development
    env_file:
      - .env.development
    environment:
      - NEXT_TELEMETRY_DISABLED=1
      - NODE_ENV=development
      - API_BASE_URL=http://api:3000
      - DOCKER=true
    ports:
      - "3000:3000"
    volumes:
      - ./:/app
      - dev_node_modules:/app/node_modules
      - dev_npm_cache:/root/.npm
    command: ["sh", "-lc", "echo '[website] Waiting for API...'; npm run dev --workspace=@gridpilot/website"]
    depends_on:
      deps:
        condition: service_completed_successfully
      api:
        condition: service_healthy
    networks:
      - gridpilot-network
    restart: "no"
    healthcheck:
      test: ["CMD", "node", "-e", "fetch('http://localhost:3000/health').then(r=>process.exit(r.ok?0:1)).catch(()=>process.exit(1))"]
      interval: 10s
      timeout: 10s
      retries: 15
      start_period: 30s

  db:
    image: postgres:15-alpine
    restart: "no"
    env_file:
      - .env.development
    ports:
      - "5432:5432"
    volumes:
      - dev_db_data:/var/lib/postgresql/data
    networks:
      - gridpilot-network
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U $$POSTGRES_USER -d $$POSTGRES_DB"]
      interval: 5s
      timeout: 5s
      retries: 5

networks:
  gridpilot-network:
    driver: bridge

volumes:
  dev_db_data:
  dev_node_modules:
  dev_npm_cache:
  dev_media_data:
