import { HttpInterceptorFn } from '@angular/common/http';
import { inject } from '@angular/core';
import { AdminAuthService } from './admin-auth.service';

/**
 * Attaches admin session/token headers only to admin API requests. Mirrors
 * apiHeadersInterceptor's self-guarding pattern but scoped to `/admin` so it
 * never touches customer requests and never reads AuthService's session.
 */
export const adminAuthHeadersInterceptor: HttpInterceptorFn = (req, next) => {
  const isAdminRequest = req.url.includes('/admin/');
  if (!isAdminRequest) {
    return next(req);
  }

  const adminAuth = inject(AdminAuthService);
  const session = adminAuth.session();
  const token = adminAuth.getAdminToken();

  let headers = req.headers;
  if (session?.sessionId) {
    headers = headers.set('AdminWebSessionID', session.sessionId);
  }
  if (token) {
    headers = headers.set('Authorization', `Bearer ${token}`);
  }

  return next(req.clone({ headers }));
};
