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"
    ports:
      - "5050:80"
    volumes:
      - pgadmin_dev_data:/var/lib/pgadmin
      - ./docker/pgadmin/servers.json:/pgadmin4/servers.json:ro
    profiles: ["tools"]

  migrate:
    build:
      context: .
      dockerfile: Dockerfile.dev
    depends_on:
      postgres:
        condition: service_healthy
    env_file:
      - .env.docker.dev
    command: ["sh", "/app/docker/scripts/dev-bootstrap.sh"]
    volumes:
      - .:/app
      - app_node_modules:/app/node_modules
      - postgres_bootstrap_state:/state

  api:
    build:
      context: .
      dockerfile: Dockerfile.dev
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_started
      migrate:
        condition: service_completed_successfully
    env_file:
      - .env.docker.dev
    command: ["npm", "run", "dev", "--workspace", "@rentaldrivego/api"]
    ports:
      - "4000:4000"
    volumes:
      - .:/app
      - app_node_modules:/app/node_modules
    restart: unless-stopped
    profiles: ["api", "full"]

  marketplace:
    build:
      context: .
      dockerfile: Dockerfile.dev
    depends_on:
      - api
    env_file:
      - .env.docker.dev
    command: ["npm", "run", "dev", "--workspace", "@rentaldrivego/marketplace"]
    ports:
      - "3000:3000"
    volumes:
      - .:/app
      - app_node_modules:/app/node_modules
    restart: unless-stopped
    profiles: ["marketplace", "full"]

  dashboard:
    build:
      context: .
      dockerfile: Dockerfile.dev
    depends_on:
      - api
    env_file:
      - .env.docker.dev
    command: ["npm", "run", "dev", "--workspace", "@rentaldrivego/dashboard"]
    ports:
      - "3001:3001"
    volumes:
      - .:/app
      - app_node_modules:/app/node_modules
    restart: unless-stopped
    profiles: ["dashboard", "full"]

  admin:
    build:
      context: .
      dockerfile: Dockerfile.dev
    depends_on:
      - api
    env_file:
      - .env.docker.dev
    command: ["npm", "run", "dev", "--workspace", "@rentaldrivego/admin"]
    ports:
      - "3002:3002"
    volumes:
      - .:/app
      - app_node_modules:/app/node_modules
    restart: unless-stopped
    profiles: ["admin", "full"]

  public-site:
    build:
      context: .
      dockerfile: Dockerfile.dev
    depends_on:
      - api
    env_file:
      - .env.docker.dev
    command: ["npm", "run", "dev", "--workspace", "@rentaldrivego/public-site"]
    ports:
      - "3003:3003"
    volumes:
      - .:/app
      - app_node_modules:/app/node_modules
    restart: unless-stopped
    profiles: ["public-site", "full"]

volumes:
  postgres_dev_data:
  redis_dev_data:
  pgadmin_dev_data:
  app_node_modules:
  postgres_bootstrap_state:
