import { AsyncLocalStorage } from 'node:async_hooks';

  export interface TenantContext {
    tenantId: string;
    subdomain: string;
  }

  export const tenantStorage = new AsyncLocalStorage<TenantContext>();

  export function getCurrentTenant(): TenantContext | undefined {
    return tenantStorage.getStore();
  }

  export function requireCurrentTenant(): TenantContext {
    const ctx = tenantStorage.getStore();
    if (!ctx) {
      throw new Error('Tenant context not set. Did the request pass through TenantMiddleware?');
    }
    return ctx;
  }
  