import { Types } from 'mongoose';

// MyOperator API Response Types
export interface MyOperatorApiResponse<T = any> {
  status: 'success' | 'error';
  url?: string;
  code: number;
  data?: T;
  message?: string;
  uuid?: string; // e.g. delete user response
}

// MyOperator User Types
export interface MyOperatorUser {
  uuid: string;
  name: string;
  contact_number: string;
  country_code: string;
  extension: number;
  email?: string;
  alternate_contact_number?: string;
  alternate_cc?: string;
  panel_access?: number; // 1 = can access, 2 = cannot access
  receive_calls?: number; // 1 = receives calls, 2 = does not receive
  contact_type?: 'mobile' | 'sip' | 'landline';
  role?: number; // 1=SuperAdmin, 2=Admin, 4=Manager, 8=Basic
}

export interface CreateMyOperatorUserPayload {
  name: string;
  contact_number: string;
  country_code: string;
  extension: number;
  email?: string;
  alternate_contact_number?: string;
  alternate_cc?: string;
  panel_access?: number;
  receive_calls?: number;
  contact_type?: 'mobile' | 'sip' | 'landline';
}

export interface UpdateMyOperatorUserPayload {
  uuid: string;
  name?: string;
  contact_number?: string;
  country_code?: string;
  extension?: number;
  email?: string;
  alternate_contact_number?: string;
  alternate_cc?: string;
  panel_access?: number;
  receive_calls?: number;
  contact_type?: 'mobile' | 'sip' | 'landline';
}

export interface ListUsersOptions {
  keyword?: string;
  page?: number;
  pageSize?: number;
  all?: boolean;
}

// MyOperator Call Log Types
export interface MyOperatorCallLog {
  unique_id: string;
  customer_number: string;
  customer_name?: string;
  agent_name?: string;
  agent_extension?: number;
  call_type: string; // 'incoming' | 'outgoing'
  call_status: string; // 'answered' | 'missed' | 'failed'
  duration?: number; // in seconds
  recording_file?: string;
  timestamp: string;
  comments?: string;
}

export interface SearchCallLogsOptions {
  from?: number; // UTC timestamp
  to?: number; // UTC timestamp
  logFrom?: number; // pagination offset
  pageSize?: number; // max 100
  searchKey?: string;
  filters?: string; // e.g., "5 AND 13 OR 8"
}

// MyOperator Filter Types
export interface MyOperatorFilter {
  id: number;
  name: string;
  parent_id: number;
}

// Webhook Types
export interface MyOperatorWebhookPayload {
  unique_id?: string;
  reference_id?: string; // _cri field - used for click-to-call tracking
  customer_number?: string;
  customer_name?: string;
  agent_extension?: number;
  agent_name?: string;
  call_type?: string;
  call_status?: string;
  duration?: number;
  recording_file?: string;
  timestamp?: string;
  [key: string]: any; // Allow additional fields
}

// MyOperator New Webhook Format (underscore-prefixed fields)
export interface MyOperatorWebhookCallLeg {
  _an: boolean; // answered
  _wr: boolean; // was ringing
  _rst: string; // ring start time
  _ds: string; // disposition (ANSWER, BUSY, etc.)
  _did: string; // dialed number
  _rr: Array<{
    _id: string; // agent ID
    _na: string; // agent name
    _em: string | null; // agent email
    _ex: string; // extension
    _ct: string; // contact number (without country code)
    _nr: string; // full number with country code
  }>; // received routes/agents
  _tt: any[]; // transfer targets
  _su: string; // status update
  _st: number; // start timestamp
  _et: number; // end timestamp
  _dr: string; // duration (HH:MM:SS)
  _ac: string; // action (received, missed, etc.)
}

export interface MyOperatorWebhookPayloadNew {
  _ai: string; // activity ID
  _so: string; // source
  _ci: string; // company ID
  _cr: string; // caller number (without country code)
  _cm: string; // caller message
  _cl: string; // caller number with country code
  _cy: string; // country code
  _se: string; // state/region
  _ts: number; // timestamp
  _st: number; // start time
  _ms: number; // millisecond timestamp
  _ss: number; // session start
  _et: number; // end time
  _dr: string; // duration (HH:MM:SS)
  _drm: number; // duration in minutes
  _ty: number; // type
  _ev: number; // event type
  _fn: string; // recording filename
  _fu: string; // recording file URL
  _ns: string; // namespace
  _su: number; // status update
  _dn: string; // display name
  _di: string; // display info
  _pm: Array<{ ky: string; vl: string }>; // parameters
  _cn: any[]; // custom notes
  _ld: MyOperatorWebhookCallLeg[]; // call leg details
  _an: number; // answered count
  _us: Array<{ ky: string; vl: string }>; // user status
  _tc: Array<{ ye: string; yf: number }>; // talk count
  _ri: string; // request ID
  _ji: string; // job ID
  _ivid: string; // IVR ID
  _cri: string; // call reference ID
  _rv: boolean; // reviewed
}

// Service Method Types
export interface SyncUserOptions {
  userId: Types.ObjectId | string;
  companyId: Types.ObjectId | string;
  extension?: number;
}

export interface SyncUserResult {
  success: boolean;
  uuid?: string;
  extension?: number;
  message?: string;
  error?: string;
}

// Credentials Types
export interface MyOperatorCredentials {
  apiToken: string;
  secretKey?: string;
  xApiKey?: string;
  companyId?: string;
  isActive: boolean;
  connectionStatus?: 'connected' | 'disconnected' | 'error' | 'pending';
}

export interface SaveCredentialsPayload {
  apiToken: string;
  secretKey?: string;
  xApiKey?: string;
  companyId?: string;
  webhookSecret?: string;
  publicIvrId?: string;
  isActive?: boolean;
}

export interface TestConnectionResult {
  success: boolean;
  status: 'connected' | 'disconnected' | 'error';
  message?: string;
  error?: string;
}

// Recording Link Types
export interface RecordingLinkResponse {
  link: string;
  expiresAt: Date;
}

// Lead Integration Types
export interface LeadMyOperatorStatus {
  isConfigured: boolean;
  isActive: boolean;
  userSynced: boolean;
  userExtension?: number;
  leadPhone?: string;
  lastCallActivity?: {
    id: string;
    title: string;
    status: string;
    createdAt: Date;
  };
}

export interface LeadCallHistory {
  calls: MyOperatorCallLog[];
  totalCalls: number;
  page: number;
  pageSize: number;
}

export interface InitiateClickToCallRequest {
  userId: Types.ObjectId | string;
  leadId: Types.ObjectId | string;
  contactId: Types.ObjectId | string;
}

export interface InitiateClickToCallResponse {
  success: boolean;
  message: string;
  callId?: string;
  referenceId?: string;
  uniqueId?: string;
}

export interface GetCallStatusOptions {
  userId: Types.ObjectId | string;
}

export interface GetCallStatusResponse {
  ongoing: boolean;
  activeCall?: {
    userId: string;
    contactId?: string;
    leadId?: string;
    referenceId: string;
    startedAt: string;
  };
}

export interface MyOperatorOBDApiResponse {
  details: string;
  status: 'success' | 'error';
  code: string;
  unique_id: string;
  reference_id?: string;
  message?: string;
  type?: string;
  stage?: string;
  resourcePath?: string;
}

export interface MyOperatorUserCreateResponse {
  status: string;
  code: number;
  message: string;
  uuid: string;
}
