// Velocity Comms Types
// Native types for the Conversations module

export type CommsChannel = 'whatsapp' | 'sms' | 'call';
export type CommsThreadStatus = 'open' | 'resolved' | 'spam' | 'archived';
export type CommsMessageDirection = 'inbound' | 'outbound' | 'system';
export type CommsMessageType = 'text' | 'image' | 'video' | 'audio' | 'document' | 'location' | 'template';
export type CommsProvider = 'mock' | 'waha' | 'evolution' | 'meta_cloud';
export type CommsDeliveryStatus = 'pending' | 'sent' | 'delivered' | 'read' | 'failed';

export interface CommsThread {
  threadId: string;
  provider: CommsProvider;
  externalThreadId?: string;
  personId?: string;
  phoneE164: string;
  displayName?: string;
  channel: CommsChannel;
  status: CommsThreadStatus;
  assignedUserId?: string;
  lastMessageAt?: string;
  unreadCount: number;
  metadataJson?: Record<string, unknown>;
  createdAt: string;
  updatedAt: string;
  crmPerson?: {
    id: string;
    fullName?: string;
    primaryPhone?: string;
    primaryEmail?: string;
    buyerType?: string;
    leadStatus?: string;
    projectName?: string;
  };
  lastMessagePreview?: string;
}

export interface CommsMessage {
  messageId: string;
  threadId: string;
  provider: CommsProvider;
  externalMessageId?: string;
  direction: CommsMessageDirection;
  messageType: CommsMessageType;
  body: string;
  mediaUrl?: string;
  mediaMimeType?: string;
  deliveryStatus: CommsDeliveryStatus;
  sentAt?: string;
  deliveredAt?: string;
  readAt?: string;
  rawPayload?: Record<string, unknown>;
  createdAt: string;
  senderName?: string;
  senderAvatar?: string;
}

export interface CommsCallLog {
  callId: string;
  threadId?: string;
  personId?: string;
  provider: CommsProvider;
  externalCallId?: string;
  phoneE164: string;
  direction: 'inbound' | 'outbound';
  status: 'ringing' | 'answered' | 'missed' | 'voicemail' | 'completed';
  startedAt: string;
  endedAt?: string;
  durationSeconds?: number;
  recordingUrl?: string;
  transcriptId?: string;
  transcriptText?: string;
  rawPayload?: Record<string, unknown>;
  createdAt: string;
}

export interface CommsSettings {
  provider: CommsProvider;
  providerBaseUrl?: string;
  providerApiKey?: string;
  instanceId?: string;
  phoneNumberId?: string;
  webhookCallbackUrl?: string;
  webhookSecretSet: boolean;
  defaultAssignmentUserId?: string;
  autoLinkByPhone: boolean;
  createCrmInteractionOnInbound: boolean;
  defaultCountryCode: string;
  mediaStorageDir?: string;
  transcriptionProvider?: 'none' | 'openai' | 'local';
  updatedAt?: string;
}

export interface ThreadLinkPayload {
  personId: string;
}

export interface SendMessagePayload {
  messageType: CommsMessageType;
  body: string;
  mediaUrl?: string;
  templateName?: string;
  templateLanguage?: string;
}

export interface CommsProviderTestResult {
  success: boolean;
  message: string;
  accountInfo?: Record<string, unknown>;
}

export interface CommsThreadListResponse {
  threads: CommsThread[];
  total: number;
  unreadTotal: number;
}

export interface CommsMessageListResponse {
  messages: CommsMessage[];
  thread: CommsThread;
}
