// lib/api/types/leads.types.ts
//
// Mirrors the NestJS lead module. Keep in step with lead.entity.ts.

export type LeadStatus = 'New' | 'Interested' | 'Converted' | 'Lost';
export type LeadPriority = 'Low' | 'Medium' | 'High';

/**
 * Matches the ClientGroup union in audit-request.types.ts, plus QRS_NEW.
 * All four are pickable; QRS_B resolves to QRS when grouping or filtering,
 * the same way groupToSource() treats it in BatchAuditRequestForm.
 */
export type ClientGroup = 'QRS' | 'TQS' | 'QRS_B' | 'QRS_NEW';
export type SelectableClientGroup = ClientGroup;

/** The bucket a stored value counts as. */
export type RollupClientGroup = 'QRS' | 'TQS' | 'QRS_NEW';

export interface LeadUserRef {
  id: number;
  firstName?: string | null;
  lastName?: string | null;
  email?: string | null;
}

export interface Lead {
  id: number;
  lead_code: string;
  company: string;
  client_group: ClientGroup | null;
  contact: string | null;
  phone: string | null;
  email: string | null;
  website: string | null;
  standards: string[] | null;
  status: LeadStatus;
  lost_reason: string | null;
  lost_notes: string | null;
  lost_at: string | null;
  source: string | null;
  priority: LeadPriority;
  notes: string | null;
  tags: string[] | null;
  assigned_to: number | null;
  assignedUser: LeadUserRef | null;
  assigned_by: number | null;
  assignedByUser: LeadUserRef | null;
  assigned_at: string | null;
  created_by: number | null;
  creator: LeadUserRef | null;
  converted_at: string | null;
  created_at: string | null;
  updated_at: string | null;
}

export interface LeadListMeta {
  total: number;
  page: number;
  limit: number;
  totalPages: number;
}

export interface LeadListResponse {
  data: Lead[];
  meta: LeadListMeta;
}

export interface LeadAnalytics {
  total: number;
  new_month: number;
  converted: number;
  lost: number;
  by_client_group: Record<string, number>;
}

export interface LeadListParams {
  page?: number;
  limit?: number;
  search?: string;
  status?: LeadStatus;
  client_group?: ClientGroup;
  assigned_to?: number;
  source?: string;
  tag?: string;
  date_from?: string;
  date_to?: string;
}

export interface CreateLeadPayload {
  company: string;
  client_group?: SelectableClientGroup;
  contact?: string;
  phone?: string;
  email?: string;
  website?: string;
  standards?: string[];
  status?: Exclude<LeadStatus, 'Converted'>;
  lost_reason?: string;
  lost_notes?: string;
  source?: string;
  priority?: LeadPriority;
  notes?: string;
  tags?: string[];
  assigned_to?: number;
  override_duplicate?: boolean;
}

export type UpdateLeadPayload = Partial<
  Omit<CreateLeadPayload, 'assigned_to' | 'override_duplicate'>
> & {
  status?: LeadStatus;
};

export interface AssignLeadPayload {
  assigned_to: number;
  note?: string;
  notify?: boolean;
}

export interface DuplicateMatch {
  id: number;
  lead_code: string;
  company: string;
  contact: string | null;
  email: string | null;
  phone: string | null;
  status: LeadStatus;
  reasons: { label: string }[];
  severity: 'high' | 'medium' | 'low';
}

/** Payload of a LEAD_ASSIGNED / LEAD_UNASSIGNED real-time notification. */
export interface LeadNotification {
  user_id: number;
  type:
    | 'LEAD_ASSIGNED'
    | 'LEAD_UNASSIGNED'
    | 'LEAD_BULK_ASSIGNED'
    | 'LEAD_HANDOVER_REQUESTED';
  title: string;
  message: string;
  link?: string | null;
  metadata?: {
    lead_id?: number;
    lead_code?: string;
    company?: string;
    client_group?: ClientGroup | null;
    assigned_by?: number;
    new_owner_id?: number;
    previous_owner_id?: number | null;
    lead_ids?: number[];
  } | null;
}
