import { HttpInterceptorFn } from '@angular/common/http';
import { currentRole } from './role';

/**
 * Dev-only: stamps role-aware requests with the current `?role=` as an `X-Role`
 * header so the backend can enforce the drafter/approver/admin rules. Only the
 * brief, org-template and /me endpoints carry it (WP-23 widened the set — /me must
 * see the role or `AccessStore` could never learn a capability); everything else
 * is untouched.
 */
const ROLE_AWARE = ['/api/v1/brief', '/api/v1/admin/org-template', '/api/v1/me'];

export const roleInterceptor: HttpInterceptorFn = (req, next) => {
  if (!ROLE_AWARE.some((prefix) => req.url.includes(prefix))) return next(req);
  return next(req.clone({ setHeaders: { 'X-Role': currentRole() } }));
};
