/** Shared domain types. Mirrored by web/src/lib/types.ts and widget/src/lib/types.ts. */

export type ConversationStatus = 'queued' | 'active' | 'closed';
export type SenderType = 'customer' | 'agent' | 'system';
export type MessageType = 'text' | 'file' | 'system';
export type Presence = 'online' | 'away' | 'busy' | 'offline';

export interface AgentDTO {
  id: string;
  name: string;
  email: string;
  avatarUrl: string | null;
  role: 'agent' | 'admin';
  presence: Presence;
  maxConcurrentChats: number;
  activeChats?: number;
}

export interface CustomerDTO {
  id: string;
  name: string;
  email: string | null;
  phone: string | null;
  createdAt: string;
}

export interface SessionInfoDTO {
  ip: string | null;
  userAgent: string | null;
  device: Record<string, unknown>;
  geo: Record<string, unknown>;
  locale: string | null;
  timezone: string | null;
  screen: { w: number; h: number; dpr: number } | null;
  pageUrl: string | null;
  referrer: string | null;
  /** Pre-formatted for direct display in the agent panel. */
  deviceLabel: string;
  geoLabel: string;
}

export interface AttachmentDTO {
  id: string;
  name: string;
  mimeType: string;
  size: number;
  url: string | null;         // signed, short-lived
  isImage: boolean;
}

export interface MessageDTO {
  id: string;
  seq: number;
  conversationId: string;
  senderType: SenderType;
  senderId: string | null;
  senderName: string | null;
  type: MessageType;
  body: string;
  attachment: AttachmentDTO | null;
  clientMsgId: string | null;
  createdAt: string;
}

export interface ConversationDTO {
  id: string;
  status: ConversationStatus;
  priority: number;
  subject: string | null;
  customer: CustomerDTO;
  agent: Pick<AgentDTO, 'id' | 'name' | 'avatarUrl'> | null;
  session: SessionInfoDTO | null;
  lastMessage: { body: string; createdAt: string; senderType: SenderType } | null;
  unreadForAgent: number;
  customerLastReadSeq: number;
  agentLastReadSeq: number;
  queuedAt: string;
  acceptedAt: string | null;
  closedAt: string | null;
  lastMessageAt: string | null;
  waitingSeconds: number;
  meta: Record<string, unknown>;
}

export interface NoteDTO {
  id: string;
  conversationId: string;
  agentId: string | null;
  agentName: string | null;
  body: string;
  createdAt: string;
}

export interface CannedResponseDTO {
  id: string;
  title: string;
  shortcut: string | null;
  body: string;
  scope: 'team' | 'personal';
}

export interface CsatDTO {
  conversationId: string;
  rating: number;
  comment: string | null;
  createdAt: string;
}

export interface HistoryEntryDTO {
  id: string;
  subject: string | null;
  status: ConversationStatus;
  createdAt: string;
  closedAt: string | null;
  agentName: string | null;
  messageCount: number;
  csatRating: number | null;
}
