/**
 * Shared code for every app in this workspace. Import it anywhere: `@app/shared`.
 * The cross-app event bus uses Redis so messages reach every app process/domain.
 */
import { createPubSub } from "../../../../../packages/pubsub/src/index.ts";
import { redisDriver } from "../../../../../packages/pubsub/src/redis.ts";
import { defineService, procedure } from "../../../../../packages/rpc/src/index.ts";
import { v } from "../../../../../packages/validation/src/index.ts";

// One bus per process, backed by Redis (set REDIS_URL, defaults to localhost:6379).
export const bus = createPubSub(redisDriver(process.env.REDIS_URL));

// Shared domain types can live here and be imported by every app.
export interface Tenant {
  id: string;
  name: string;
}

/** Contract shared by web (caller) and admin (callee). */
export const catalogService = defineService({
  name: "catalog",
  procedures: {
    getProduct: procedure
      .input(v.object({ sku: v.string() }))
      .output<{ sku: string; name: string; priceCents: number }>()
      .permission("catalog:read")
      .idempotent()
      .build(),
  },
});
