name: rentaldrivego-dev

services:
  postgres:
    image: postgres:16-alpine
    restart: unless-stopped
    environment:
      POSTGRES_DB: rentaldrivego
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: password
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres -d rentaldrivego"]
      interval: 5s
      timeout: 5s
      retries: 10
    ports:
      - "5432:5432"
    volumes:
      - postgres_dev_data:/var/lib/postgresql/data

  redis:
    image: redis:7-alpine
    restart: unless-stopped
    ports:
      - "6379:6379"
    volumes:
      - redis_dev_data:/data

  pgadmin:
    image: dpage/pgadmin4:9
    restart: unless-stopped
    depends_on:
      postgres:
        condition: service_healthy
    environment:
      PGADMIN_DEFAULT_EMAIL: admin@rentaldrivego.dev
      PGADMIN_DEFAULT_PASSWORD: admin
      PGADMIN_CONFIG_ENHANCED_COOKIE_PROTECTION: "False"
      PGADMIN_CONFIG_SERVER_MODE: "False"
    ports:
      - "5050:80"
    volumes:
      - pgadmin_dev_data:/var/lib/pgadmin
      - ./docker/pgadmin/servers.json:/pgadmin4/servers.json:ro
    profiles: ["tools"]

  pgmanage:
    image: cmdpromptinc/pgmanage-enterprise:latest
    restart: unless-stopped
    user: "0:0"
    depends_on:
      postgres:
        condition: service_healthy
    environment:
      PGMANAGE_LISTEN_PORT: "8000"
      PGMANAGE_DEFAULT_USERNAME: ${PGMANAGE_DEFAULT_USERNAME:-admin}
      PGMANAGE_DEFAULT_PASSWORD: ${PGMANAGE_DEFAULT_PASSWORD:-admin}
      PGMANAGE_LICENSE_KEY: ${PGMANAGE_LICENSE_KEY:-}
      PGMANAGE_SECURE_COOKIES: ${PGMANAGE_SECURE_COOKIES:-False}
    ports:
      - "8000:8000"
    volumes:
      - pgmanage_dev_data:/appdata
    profiles: ["tools"]

  migrate:
    build:
      context: .
      dockerfile: Dockerfile.dev
    depends_on:
      postgres:
        condition: service_healthy
    env_file:
      - .env.docker.dev
    environment:
      FILE_STORAGE_ROOT: /var/lib/rentaldrivego/storage
      LEGACY_STORAGE_ROOT: /app/apps/api/src/lib/storage
    command: ["sh", "/app/docker/scripts/dev-bootstrap.sh"]
    volumes:
      - .:/app
      - app_node_modules:/app/node_modules
      - postgres_bootstrap_state:/state
      - api_uploads_dev:/var/lib/rentaldrivego/storage

  types:
    build:
      context: .
      dockerfile: Dockerfile.dev
    command: ["sh", "-c", "cd /app/packages/types && exec npm run dev"]
    volumes:
      - .:/app
      - app_node_modules:/app/node_modules
    restart: unless-stopped
    profiles: ["api", "homepage", "storefront", "dashboard", "admin", "full"]

  api:
    build:
      context: .
      dockerfile: Dockerfile.dev
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_started
      migrate:
        condition: service_completed_successfully
      types:
        condition: service_started
    env_file:
      - .env.docker.dev
    environment:
      CHOKIDAR_USEPOLLING: "true"
      CHOKIDAR_INTERVAL: "1000"
      FILE_STORAGE_ROOT: /var/lib/rentaldrivego/storage
    command: ["sh", "-c", "npm run build --workspace @rentaldrivego/types && cd /app/apps/api && exec /app/node_modules/.bin/ts-node-dev --respawn --transpile-only src/index.ts"]
    ports:
      - "4000:4000"
    volumes:
      - .:/app
      - app_node_modules:/app/node_modules
      - api_uploads_dev:/var/lib/rentaldrivego/storage
    restart: unless-stopped
    profiles: ["api", "full"]

  homepage:
    build:
      context: .
      dockerfile: Dockerfile.dev
    depends_on:
      - api
      - types
    env_file:
      - .env.docker.dev
    environment:
      CHOKIDAR_USEPOLLING: "true"
      CHOKIDAR_INTERVAL: "1000"
      WATCHPACK_POLLING: "true"
    command:
      [
        "sh",
        "-c",
        "npm run build --workspace @rentaldrivego/types && cd /app/apps/homepage && mkdir -p .next && find .next -mindepth 1 -exec rm -rf {} + && exec env PATH=/app/docker/scripts:$PATH /app/node_modules/.bin/next dev -H 0.0.0.0 -p 3000",
      ]
    ports:
      - "3000:3000"
    volumes:
      - .:/app
      - app_node_modules:/app/node_modules
      - homepage_next:/app/apps/homepage/.next
    restart: unless-stopped
    profiles: ["homepage", "full"]

  storefront:
    build:
      context: .
      dockerfile: Dockerfile.dev
    depends_on:
      - api
      - types
    env_file:
      - .env.docker.dev
    environment:
      CHOKIDAR_USEPOLLING: "true"
      CHOKIDAR_INTERVAL: "1000"
      WATCHPACK_POLLING: "true"
    command:
      [
        "sh",
        "-c",
        "npm run build --workspace @rentaldrivego/types && cd /app/apps/storefront && mkdir -p .next && find .next -mindepth 1 -exec rm -rf {} + && exec env PATH=/app/docker/scripts:$PATH /app/node_modules/.bin/next dev -H 0.0.0.0 -p 3004",
      ]
    ports:
      - "3004:3004"
    volumes:
      - .:/app
      - app_node_modules:/app/node_modules
      - storefront_next:/app/apps/storefront/.next
    restart: unless-stopped
    profiles: ["storefront", "full"]

  dashboard:
    build:
      context: .
      dockerfile: Dockerfile.dev
    depends_on:
      - api
      - types
    env_file:
      - .env.docker.dev
    environment:
      CHOKIDAR_USEPOLLING: "true"
      CHOKIDAR_INTERVAL: "1000"
      WATCHPACK_POLLING: "true"
    command:
      [
        "sh",
        "-c",
        "npm run build --workspace @rentaldrivego/types && cd /app/apps/dashboard && mkdir -p .next && find .next -mindepth 1 -exec rm -rf {} + && exec env PATH=/app/docker/scripts:$PATH /app/node_modules/.bin/next dev -H 0.0.0.0 -p 3001",
      ]
    ports:
      - "3001:3001"
    volumes:
      - .:/app
      - app_node_modules:/app/node_modules
      - dashboard_next:/app/apps/dashboard/.next
    restart: unless-stopped
    profiles: ["dashboard", "full"]

  admin:
    build:
      context: .
      dockerfile: Dockerfile.dev
    depends_on:
      - api
      - types
    env_file:
      - .env.docker.dev
    environment:
      ADMIN_ASSET_PREFIX: "http://localhost:3002"
      CHOKIDAR_USEPOLLING: "true"
      CHOKIDAR_INTERVAL: "1000"
      WATCHPACK_POLLING: "true"
    command:
      [
        "sh",
        "-c",
        "npm run build --workspace @rentaldrivego/types && cd /app/apps/admin && mkdir -p .next && find .next -mindepth 1 -exec rm -rf {} + && exec env PATH=/app/docker/scripts:$PATH /app/node_modules/.bin/next dev -H 0.0.0.0 -p 3002",
      ]
    ports:
      - "3002:3002"
    volumes:
      - .:/app
      - app_node_modules:/app/node_modules
      - admin_next:/app/apps/admin/.next
    restart: unless-stopped
    profiles: ["admin", "full"]

volumes:
  postgres_dev_data:
  redis_dev_data:
  pgadmin_dev_data:
  pgmanage_dev_data:
  app_node_modules:
  postgres_bootstrap_state:
  api_uploads_dev:
  homepage_next:
  storefront_next:
  dashboard_next:
  admin_next:
