import type { DashboardLocationFilters } from './hospitalUtils';

type WoUrlFilters = {
  status?: string;
  work_order_type?: string;
  company?: string;
  site_name?: string;
};

export function buildWoUrl(
  filters: WoUrlFilters,
  fromDate?: string,
  toDate?: string,
  locationFilters?: DashboardLocationFilters
): string {
  const params = new URLSearchParams();

  if (filters.status) params.set('status', filters.status);
  if (filters.work_order_type) params.set('work_order_type', filters.work_order_type);
  if (filters.company || locationFilters?.company) {
    params.set('company', filters.company || locationFilters?.company || '');
  }
  if (filters.site_name || locationFilters?.site_name) {
    params.set('site_name', filters.site_name || locationFilters?.site_name || '');
  }

  if (fromDate || toDate) {
    params.set('date_filter_by', 'creation');
    if (fromDate) params.set('date_start', fromDate);
    if (toDate) params.set('date_end', toDate);
  }

  const qs = params.toString();
  // Router basename is /asm_app — use app-relative path, not /asm_app/work-orders
  return `/work-orders${qs ? `?${qs}` : ''}`;
}
