import { createParamDecorator, ExecutionContext } from '@nestjs/common';

export interface TenantScopeContext {
  hostId: string;
  tenantId: string;
  clinicIds: string[];
  /// 集团模型:用户所属源组织命名空间(品牌)。数据范围在患者粒度按它圈本品牌;
  /// 空数组 = 不限品牌(集团级角色 / 单命名空间 host)。
  sourceUnits: string[];
  userId: string;
}

export const TenantScope = createParamDecorator(
  (_data: unknown, ctx: ExecutionContext): TenantScopeContext => {
    const req = ctx.switchToHttp().getRequest<{ tenantScope?: TenantScopeContext }>();
    if (!req.tenantScope) {
      throw new Error('TenantScope used on unauthenticated route');
    }
    return req.tenantScope;
  },
);
