import type { AdminVerificationQueueFilters } from '@/services/verification/types';

/**
 * The queue's URL-synced filter shape — shared between the list (`page.tsx`, via `useAdminListState`)
 * and the case detail (`[nurseId]/page.tsx`) so a case page can **re-derive the same query key** the
 * list primed (`status`/`search`/`page` → `useVerificationQueue`) and reuse its cached page for
 * next/prev case navigation, with no extra URL param and no duplicate fetch in the common flow.
 */
export const EMPTY_QUEUE_FILTERS: AdminVerificationQueueFilters = {};

/** The URL query-param name for the name/phone search (REQ-062's proposed `q`). */
const SEARCH_PARAM = 'q';

export function parseQueueFilters(params: URLSearchParams): AdminVerificationQueueFilters {
  const status = params.get('status');
  return {
    status: status === 'pending' || status === 'in_review' ? status : undefined,
    search: params.get(SEARCH_PARAM) ?? undefined,
  };
}

export function serializeQueueFilters(filters: AdminVerificationQueueFilters): Record<string, string> {
  const record: Record<string, string> = {};
  if (filters.status) record.status = filters.status;
  if (filters.search) record[SEARCH_PARAM] = filters.search;
  return record;
}

/** A case URL carrying the queue's current filters + page, so next/prev navigation (and a browser
 * back/refresh) keeps resolving against the same cached queue page instead of an unfiltered default. */
export function queueCaseHref(
  locale: string,
  casePath: string,
  filters: AdminVerificationQueueFilters,
  page: number,
): string {
  const params = new URLSearchParams(serializeQueueFilters(filters));
  if (page > 1) params.set('page', String(page));
  const qs = params.toString();
  return `/${locale}${casePath}${qs ? `?${qs}` : ''}`;
}
