/**
 * Global search types — search-completeness spec.
 *
 * Shared by the API routes, DB service, and client-side hooks.
 */

export type EntityType =
  | 'task'
  | 'project'
  | 'customer'
  | 'invoice'
  | 'receipt'
  | 'expense'
  | 'vendor'
  | 'lead'
  | 'proposal'
  | 'contract'
  | 'contractor'
  | 'kb_article'
  | 'support_ticket'
  | 'team_member'

export interface SearchResultItem {
  id: string
  type: EntityType
  /** Formatted: "Task: {title}", "Invoice #INV-001", etc. */
  label: string
  /** Entity title, customer name, etc. */
  primary: string
  /** Project name, email, vendor, etc. Null when not applicable. */
  secondary: string | null
  /** Status string when applicable (tasks, invoices, tickets). Null otherwise. */
  badge: string | null
  /** In-app navigation path e.g. "/tasks/{uuid}" */
  url: string
  /** Snippet with matched term wrapped in <mark>…</mark>. Null when absent. */
  highlight: string | null
}

export interface SearchResultGroup {
  type: EntityType
  /** Human display name: "Tasks", "Customers", etc. */
  label: string
  items: SearchResultItem[]
  /** Total matches — may exceed items.length */
  total: number
}

export interface SearchResponse {
  query: string
  /** Ordered by result count desc, ties broken by ENTITY_PRIORITY */
  groups: SearchResultGroup[]
  /** Total groups with results — may exceed groups.length */
  total_groups: number
}

export interface FullSearchResponse {
  query: string
  /** Echoes the requested type filter. Null when querying all entities. */
  type: EntityType | null
  groups: SearchResultGroup[]
  /** Null when no more results */
  next_cursor: string | null
  has_more: boolean
}

/**
 * Priority order for tie-breaking when multiple groups have the same total count.
 * Also used for tab ordering on the full search page (§2.4).
 */
export const ENTITY_PRIORITY: EntityType[] = [
  'task',
  'project',
  'customer',
  'invoice',
  'receipt',
  'expense',
  'vendor',
  'lead',
  'proposal',
  'contract',
  'contractor',
  'kb_article',
  'support_ticket',
  'team_member',
]

/** Human-readable display labels for each entity type. */
export const ENTITY_LABELS: Record<EntityType, string> = {
  task: 'Tasks',
  project: 'Projects',
  customer: 'Customers',
  invoice: 'Invoices',
  receipt: 'Receipts',
  expense: 'Expenses',
  vendor: 'Vendors',
  lead: 'Leads',
  proposal: 'Proposals',
  contract: 'Contracts',
  contractor: 'Contractors',
  kb_article: 'KB Articles',
  support_ticket: 'Support Tickets',
  team_member: 'Team Members',
}
