import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';
import { inject, Injectable } from '@angular/core';
import { CookieService } from 'ngx-cookie-service';
import { Observable } from 'rxjs';
import { environment } from 'src/environments/environment';

@Injectable()
export class ApiBaseUrlInterceptor implements HttpInterceptor {
  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    const cookieService = inject(CookieService);
    // Prepare default headers but don't overwrite existing ones
    const defaultHeaders: Record<string, string> = {
      Accept: 'application/json',
      'Content-Type': 'application/json',
      'X-Requested-With': 'XMLHttpRequest',
    };

    const headersToSet: Record<string, string> = {};

    if (req.url === '/api/v1/pos' || req.url.startsWith('/api/v1/pos/')) {
      headersToSet['pos_Id'] = cookieService.get('posId');

      // req.headers.set('posId', cookieService.get('posId'));
    }

    const isFormData = req.body instanceof FormData;
    Object.keys(defaultHeaders).forEach((k) => {
      if (k === 'Content-Type' && isFormData) return;
      if (!req.headers.has(k)) {
        headersToSet[k] = defaultHeaders[k];
      }
    });

    // Only prepend base URL if the request URL is relative (does not start with http or https)
    if (!/^https?:\/\//i.test(req.url)) {
      const apiReq = req.clone({
        url: environment.apiBaseUrl + req.url,
        setHeaders: headersToSet,
        credentials: 'include',
      });
      return next.handle(apiReq);
    }

    // If absolute URL, still ensure default headers are present
    if (Object.keys(headersToSet).length > 0) {
      const reqWithHeaders = req.clone({ setHeaders: headersToSet });
      return next.handle(reqWithHeaders);
    }

    return next.handle(req);
  }
}
