import { createMiddleware } from "hono/factory";

export type TenantVariables = {
  householdId: string | null;
};

export const tenantMiddleware = createMiddleware<{
  Variables: TenantVariables;
}>(async (c, next) => {
  const householdId = c.req.header("x-household-id") ?? null;
  c.set("householdId", householdId);
  await next();
});

export const requireHousehold = createMiddleware<{
  Variables: TenantVariables;
}>(async (c, next) => {
  const householdId = c.get("householdId");
  if (!householdId) {
    return c.json({ error: "No household selected" }, 400);
  }
  await next();
});
