// ============================================================================
// CloseBot API Types — Generated from OpenAPI 3.0.1 Swagger Spec
// ============================================================================

// ---------- Common / Shared ----------

export interface ProblemDetails {
  type?: string | null;
  title?: string | null;
  status?: number | null;
  detail?: string | null;
  instance?: string | null;
  [key: string]: unknown;
}

// ---------- Account ----------

export interface CreateApiKeyInput {
  name?: string | null;
}

export interface CreateApiKeyOutput {
  id?: string | null;
  key?: string | null;
  name?: string | null;
  createdAt?: string;
  expiresAt?: string;
}

export interface ApiKeyDTO {
  id?: string | null;
  name?: string | null;
  createdAt?: string;
  expiresAt?: string;
}

// ---------- Agency ----------

export interface AgencyDto {
  id?: string | null;
  name?: string | null;
  members?: AgencyMemberDto[] | null;
}

export interface AgencyMemberDto {
  accountId?: string | null;
  role?: string | null;
  authId?: string | null;
  name?: string | null;
  email?: string | null;
  status?: string | null;
}

export interface InviteUserInput {
  invites?: Invite[] | null;
}

export interface Invite {
  email?: string | null;
  role?: string | null;
  sourceIds?: string[] | null;
}

// ---------- Billing ----------

export interface BalanceDto {
  balance?: number;
  currency?: string | null;
}

export interface BillingOptionsDto {
  overBillingEnabled?: boolean;
  usageBillingEnabled?: boolean;
  autoRefillEnabled?: boolean;
  topUpAmount?: number;
  refillThreshold?: number;
  currency?: string | null;
}

export interface UpdateBillingConfigInput {
  overBillingEnabled?: boolean | null;
  autoRefillEnabled?: boolean | null;
  topUpAmount?: number | null;
  refillThreshold?: number | null;
}

export interface CreateRefillDto {
  amount?: number;
  currency?: string | null;
}

export interface ReBillingDto {
  reBillingConfigured?: boolean;
  responseUnitCost?: string | null;
  storageUnitCost?: string | null;
  userUnitCost?: string | null;
  tokenMultiplier?: string | null;
}

export interface ReBillingUpdateInput {
  enabled?: boolean | null;
  responseCost?: string | null;
  storageCost?: string | null;
  userCost?: string | null;
  tokenMultiplier?: string | null;
}

export interface TransactionDto {
  id?: number;
  status?: string | null;
  description?: string | null;
  amount?: number;
  currency?: string | null;
  createdAt?: string;
  receiptUrl?: string | null;
  usage?: BilledUsageDto;
}

export interface AddSourceTransactionDto {
  amount?: string | null;
  description?: string | null;
}

export interface BilledUsageDto {
  startTime?: string;
  endTime?: string;
  responses?: number;
  libraryBytes?: number;
  users?: number;
  responseCost?: number;
  libraryCost?: number;
  userCost?: number;
}

// ---------- Bot ----------

export interface BotDto {
  id?: string | null;
  name?: string | null;
  modifiedAt?: string | null;
  modifiedBy?: string | null;
  versions?: BotVersionDto[] | null;
  sources?: BotSourceDto[] | null;
  personaIds?: string[] | null;
  favorited?: boolean;
  locked?: boolean;
  reschedulingEnabled?: boolean;
  category?: string | null;
  folderId?: string | null;
  followUpActive?: boolean;
  followUpSequences?: FollowUpSequenceDto[] | null;
  smartFollowUp?: boolean;
  followUpRepeat?: boolean;
  followUpVarianceMinutes?: number;
  followUpExtraPrompt?: string | null;
  tools?: BotToolDto[] | null;
}

export interface BotVersionDto {
  version?: string | null;
  name?: string | null;
  published?: boolean;
  modifiedAt?: string;
  modifiedBy?: string | null;
}

export interface BotSourceDto {
  id?: string | null;
  category?: string | null;
  key?: string | null;
  name?: string | null;
  tags?: ContactTag[] | null;
  channelList?: string[] | null;
  personaNameOverride?: string | null;
  enabled?: boolean;
}

export interface ContactTag {
  name?: string | null;
  approveDeny?: boolean;
  id?: string | null;
}

export interface FollowUpSequenceDto {
  order?: number;
  duration?: number;
  unit?: string | null;
}

export interface BotToolDto {
  id?: string | null;
  type?: string | null;
  enabled?: boolean;
  options?: Record<string, unknown>;
}

export interface CreateBotInput {
  name?: string | null;
  templateId?: string | null;
  importKdl?: string | null;
  folderId?: string | null;
  category?: string | null;
}

export interface AiCreateBotInput {
  name?: string | null;
  description?: string | null;
  category?: string | null;
  folderId?: string | null;
}

export interface UpdateBotInput {
  favorite?: boolean | null;
  trash?: boolean | null;
  locked?: boolean | null;
  rescheduling?: boolean | null;
  name?: string | null;
  folderId?: string | null;
  category?: string | null;
  followUpActive?: boolean | null;
  followUpSequences?: FollowUpSequenceDto[] | null;
  smartFollowUp?: boolean | null;
  followUpRepeat?: boolean | null;
  followUpVarianceMinutes?: number | null;
  followUpExtraPrompt?: string | null;
}

export interface UpdateBotErrorResponse {
  message?: string | null;
}

export interface AttachSourceInput {
  tags?: ContactTag[] | null;
  channels?: string[] | null;
  personaNameOverride?: string | null;
  enabled?: boolean | null;
}

export interface ExportBotResponse {
  id?: string | null;
  kdl?: string | null;
  version?: string | null;
}

export interface SaveBotInput {
  botSteps?: unknown;
}

export interface SaveBotResponse {
  version?: string | null;
  invalidPaths?: string[] | null;
  message?: string | null;
}

export interface PublishBotResponse {
  version?: string | null;
  message?: unknown;
}

export interface UpdateVersionInput {
  name?: string | null;
  importKdl?: string | null;
}

export interface ToolInputDto {
  type?: string | null;
  enabled?: boolean;
  options?: unknown;
}

// ---------- Bot Metrics ----------

export interface AgencyDashboardSummaryResponse {
  currentMonthMessageCount?: number;
  lastMonthMessageCount?: number;
  totalStorage?: number;
  currentMonthSuccessfulBookings?: number;
  lastMonthSuccessfulBookings?: number;
  currentMonthActiveSources?: number;
  lastMonthActiveSources?: number;
  currentUsers?: number;
  currentMonthContacts?: number;
  lastMonthContacts?: number;
}

export interface BotMetricAction {
  timestamp?: string;
  actionId?: string | null;
  leadId?: string | null;
  sourceId?: string | null;
  botId?: string | null;
  nodeId?: number;
  frontendNodeId?: string | null;
}

export interface BotMetricMessage {
  messageId?: string | null;
  sourceId?: string | null;
  leadId?: string | null;
  botId?: string | null;
  personaId?: string | null;
  channel?: string | null;
  fromBot?: boolean;
  direction?: string | null;
  message?: string | null;
  attachments?: BotMetricAttachment[] | null;
  timestamp?: string;
  activities?: BotMetricActivity[] | null;
}

export interface BotMetricAttachment {
  url?: string | null;
}

export interface BotMetricActivity {
  activity?: string | null;
  data?: string | null;
  timeData?: string | null;
}

export interface BotMetricLog {
  timestamp?: string;
  botId?: string | null;
  messageId?: string | null;
  sourceId?: string | null;
  leadId?: string | null;
  actionId?: string | null;
  severity?: number | null;
  message?: string | null;
  prompt?: BotMetricPrompt[] | null;
  response?: string | null;
  promptTokens?: number | null;
  completionTokens?: number | null;
  provider?: string | null;
  model?: string | null;
  purpose?: string | null;
  body?: string | null;
}

export interface BotMetricPrompt {
  kind?: string | null;
  body?: string | null;
}

export interface LeaderboardResponse {
  agencyName?: string | null;
  agencyId?: string | null;
  count?: number;
  value?: number;
  rank?: number;
  isCurrentAgency?: boolean;
}

export interface MessageFeedbackRequest {
  messageId?: string | null;
  leadId?: string | null;
  reasons?: string | null;
  liked?: boolean | null;
}

export interface MessageFeedbackResponse {
  feedbackMessageIds?: string[] | null;
}

export interface MessageLikesResponse {
  likedMessageIds?: string[] | null;
}

export interface MessageReason {
  reason?: string | null;
}

// ---------- Bot Source Variables ----------

export interface BotVariableDto {
  id?: string | null;
  name?: string | null;
  value?: string | null;
}

export interface BotVariableUpdateInput {
  id?: string | null;
  value?: string | null;
}

// ---------- Bot Templates ----------

export interface BotTemplateDto {
  id?: string | null;
  name?: string | null;
  description?: string | null;
  level?: string | null;
  industry?: string | null;
  tags?: string[] | null;
  videoUrl?: string | null;
  previewUrl?: string | null;
  tier?: string | null;
}

// ---------- Bot Testing ----------

export interface TestSession {
  leadId?: string | null;
  sourceId?: string | null;
}

export interface TestSessionMessageInput {
  leadId?: string | null;
  message?: string | null;
}

export interface UpdateSessionInput {
  mimicSourceId?: string | null;
}

export interface UpdateSessionDto {
  sessionId?: string | null;
}

export interface BotTestingRollbackInput {
  messageId?: string | null;
}

export interface BotTestingRollbackOutput {
  deletedMessageIds?: string[] | null;
  deletedActionIds?: string[] | null;
}

export interface ListLeadDto {
  leads?: LeadDto[] | null;
  total?: number;
}

// ---------- Hierarchy (Folders) ----------

export interface AddHierarchyInput {
  name?: string | null;
  parentId?: string | null;
}

export interface AddHierarchyOutput {
  id?: string | null;
}

export interface ListHierarchyResult {
  id?: string | null;
  name?: string | null;
  bots?: string[] | null;
  personas?: string[] | null;
  children?: string[] | null;
}

export interface HierarchyInput {
  name?: string | null;
}

// ---------- Lead ----------

export interface LeadDto {
  id?: string | null;
  name?: string | null;
  contactId?: string | null;
  lastMessageTime?: string | null;
  lastMessage?: string | null;
  lastMessageDirection?: string | null;
  lastMessageBotId?: string | null;
  mostRecentFailureReason?: string | null;
  mimicSourceId?: string | null;
  source?: LeadSourceDto;
  tags?: string[] | null;
  fields?: LeadFieldDto[] | null;
  instances?: BotInstanceDto[] | null;
}

export interface LeadDtoPaginated {
  total?: number;
  results?: LeadDto[] | null;
  page?: number;
  pageSize?: number;
}

export interface LeadSourceDto {
  id?: string | null;
  name?: string | null;
}

export interface LeadFieldDto {
  field?: string | null;
  value?: string | null;
}

export interface LeadFieldUpdateDto {
  field?: string | null;
  value?: string | null;
}

export interface LeadUpdateDto {
  fields?: LeadFieldUpdateDto[] | null;
}

export interface SearchQueryInput {
  offset?: number | null;
  count?: number | null;
  search?: string | null;
  sourceIds?: string[] | null;
  channels?: string[] | null;
  botIds?: string[] | null;
  personaIds?: string[] | null;
  minimumResponses?: number | null;
  lastMessageDirection?: string | null;
  followUpScheduled?: boolean | null;
}

export interface InstanceUpdateDto {
  followUpTime?: string | null;
}

export interface BotInstanceDto {
  botId?: string | null;
  botVersion?: string | null;
  followUpTimezoneKind?: string | null;
  followUpTimezone?: string | null;
  followUpTime?: string | null;
  isSmartFollowUpTime?: boolean;
}

// ---------- Library ----------

export interface FileDto {
  fileId?: string | null;
  fileName?: string | null;
  lastModified?: string;
  fileType?: string | null;
  fileStatus?: string | null;
  fileSize?: number;
  estimatedFileSize?: number;
  sources?: FileSourceDto[] | null;
  accountId?: string | null;
  uri?: string | null;
}

export interface FileSourceDto {
  id?: string | null;
  name?: string | null;
  category?: string | null;
}

export interface UploadWebscrapeToSourceInput {
  scrapeUrl?: string | null;
  maxBreadth?: number;
  maxDepth?: number;
}

export interface WebscrapePageDto {
  url?: string | null;
  enabled?: boolean;
}

export interface WebscrapePageUpdateDto {
  pages?: WebscrapePageDto[] | null;
}

// ---------- Live Demo ----------

export interface LiveDemoCreateDto {
  name?: string | null;
  mimicSourceId?: string | null;
  active?: boolean;
}

export interface LiveDemoDto {
  name?: string | null;
  key?: string | null;
  organizationId?: string | null;
  active?: boolean;
  mimicSourceId?: string | null;
  sourceVariables?: BotVariableDto[] | null;
}

export interface LiveDemoSessionDto {
  key?: string | null;
  sessionLeadId?: string | null;
}

export interface LiveDemoMessageInput {
  message?: string | null;
}

// ---------- Notification ----------

export interface NotificationDto {
  id?: string | null;
  kind?: string | null;
  title?: string | null;
  body?: string | null;
  viewed?: boolean;
  timestamp?: string;
  metadata?: NotificationMetadata;
}

export interface NotificationMetadata {
  aiProviderId?: string | null;
  sourceId?: string | null;
  botId?: string | null;
  rawAiError?: string | null;
}

export interface NotificationUpdateDto {
  viewed?: boolean;
}

export interface NotificationForwardingDto {
  enabled?: boolean;
  channelsEnabled?: string[] | null;
  webhookEndpoint?: string | null;
}

// ---------- Persona ----------

export interface PersonaDto {
  id?: string | null;
  agencyId?: string | null;
  personaName?: string | null;
  description?: string | null;
  color?: string | null;
  imageUri?: string | null;
  voiceStyles?: string | null;
  howToRespond?: string | null;
  typoPercent?: number;
  breakupLargeMessagePercent?: number;
  responseTime?: string | null;
  responseDelay?: number;
  modifiedAt?: string | null;
  modifiedBy?: string | null;
  aiProviderPreferences?: string[] | null;
  folderId?: string | null;
  botIds?: string[] | null;
  bots?: BotPersonaDto[] | null;
  favorited?: boolean;
  default?: boolean;
}

export interface BotPersonaDto {
  id?: string | null;
  name?: string | null;
}

export interface CreatePersonaInput {
  personaName?: string | null;
  description?: string | null;
  voiceStyles?: string | null;
  howToRespond?: string | null;
  typoPercent?: number;
  breakupLargeMessagePercent?: number;
  responseTime?: string | null;
  responseDelay?: number;
  aiProviderPreferences?: string[] | null;
  color?: string | null;
  imageData?: string | null;
}

export interface UpdatePersonaInput {
  personaName?: string | null;
  description?: string | null;
  voiceStyles?: string | null;
  howToRespond?: string | null;
  typoPercent?: number | null;
  breakupLargeMessagePercent?: number | null;
  responseTime?: string | null;
  responseDelay?: number | null;
  aiProviderPreferences?: string[] | null;
  folderId?: string | null;
  favorited?: boolean | null;
  trash?: boolean | null;
  default?: boolean | null;
  color?: string | null;
  imageData?: string | null;
}

// ---------- Smart FAQ ----------

export interface SmartFAQDto {
  id?: string | null;
  agencyId?: string | null;
  sourceId?: string | null;
  question?: string | null;
  answer?: string | null;
  leadIds?: string[] | null;
  state?: number;
}

export interface CreateSmartFAQRequest {
  sourceId?: string | null;
  question?: string | null;
  answer?: string | null;
}

export interface AnswerMultipleFAQsRequest {
  faQs?: AnswerFAQRequest[] | null;
}

export interface AnswerFAQRequest {
  id?: string | null;
  answer?: string | null;
}

export interface AnsweredFAQFollowUpRequest {
  faqId?: string | null;
  leadIds?: string[] | null;
}

// ---------- Source ----------

export interface SourceDto {
  agencyId?: string | null;
  sourceId?: string | null;
  name?: string | null;
  category?: string | null;
  key?: string | null;
  accessToken?: string | null;
  address?: string | null;
  connected?: boolean;
  autoShutoff?: boolean;
  gracefulGoodbye?: boolean;
  bots?: SourceBotDto[] | null;
  accountsWithAccess?: string[] | null;
  isAvailabilityContactTimezone?: boolean;
  respondWindows?: SourceAvailabilityDto[] | null;
  doNotRespondWindows?: SourceDoNotRespondWindowDto[] | null;
  summarizeAttachments?: boolean;
  respondToReactions?: boolean;
  markConversationAsUnread?: boolean;
  webhookCallback?: string | null;
  wallet?: SourceWalletDto;
}

export interface SourceDtoPaginated {
  total?: number;
  results?: SourceDto[] | null;
  page?: number;
  pageSize?: number;
}

export interface SourceBotDto {
  id?: string | null;
  botName?: string | null;
  tags?: ContactTag[] | null;
  channels?: string[] | null;
  personaNameOverride?: string | null;
  enabled?: boolean;
}

export interface SourceAvailabilityDto {
  dayOfWeekUtc?: string | null;
  startTimeUtc?: string | null;
  duration?: string | null;
}

export interface SourceDoNotRespondWindowDto {
  start?: string;
  end?: string;
}

export interface SourceWalletDto {
  reBilling?: boolean;
  autoRefill?: boolean;
  topUpAmount?: number;
  refillThreshold?: number;
  stripeCustomerId?: string | null;
  currency?: string | null;
  responseUnitCostOverride?: string | null;
  storageUnitCostOverride?: string | null;
  userUnitCostOverride?: string | null;
}

export interface AddSourceInput {
  name?: string | null;
  category?: string | null;
  key?: string | null;
  accessToken?: string | null;
  refreshToken?: string | null;
  expiresIn?: number | null;
  autoShutoff?: boolean | null;
  gracefulGoodbye?: boolean | null;
  summarizeAttachments?: boolean | null;
  webhookCallback?: string | null;
}

export interface UpdateSourceInput {
  name?: string | null;
  autoShutoff?: boolean | null;
  gracefulGoodbye?: boolean | null;
  isAvailabilityContactTimezone?: boolean | null;
  respondWindows?: SourceAvailabilityDto[] | null;
  doNotRespondWindows?: SourceDoNotRespondWindowDto[] | null;
  summarizeAttachments?: boolean | null;
  respondToReactions?: boolean | null;
  markConversationsAsUnread?: boolean | null;
  wallet?: UpdateSourceWalletInput;
  accountsWithAccess?: string[] | null;
  webhookCallback?: string | null;
}

export interface UpdateSourceWalletInput {
  reBilling?: boolean | null;
  autoRefill?: boolean | null;
  topUpAmount?: number | null;
  refillThreshold?: number | null;
  responseUnitCostOverride?: string | null;
  storageUnitCostOverride?: string | null;
  userUnitCostOverride?: string | null;
  stripeCustomerId?: string | null;
}

export interface SourceCalendarDto {
  name?: string | null;
  id?: string | null;
}

export interface SourceChannelDto {
  name?: string | null;
  id?: string | null;
}

export interface SourceFieldCollectionDto {
  contact?: SourceFieldDto[] | null;
  location?: SourceFieldDto[] | null;
  customValue?: SourceFieldDto[] | null;
}

export interface SourceFieldDto {
  name?: string | null;
  fieldKey?: string | null;
  dataType?: string | null;
}

export interface SourceTagDto {
  name?: string | null;
  id?: string | null;
}

// ---------- Node Descriptors ----------

export interface NodeInformation {
  dataTypes?: DataTypeInfo[] | null;
  atomicNodes?: NodeInfo[] | null;
  groups?: BotNodeGroup[] | null;
  tools?: ToolInfo[] | null;
}

export interface DataTypeInfo {
  dataTypeName?: string | null;
  properties?: PropertyInfo[] | null;
  displayName?: string | null;
}

export interface NodeInfo {
  className?: string | null;
  displayName?: string | null;
  description?: string | null;
  group?: string | null;
  properties?: PropertyInfo[] | null;
  outputs?: OutputInfo[] | null;
  outputHandles?: OutputHandleInfo[] | null;
  dynamicHandles?: DynamicOutputHandleInfo[] | null;
  hasInputHandle?: boolean;
  hasDynamicVariables?: boolean;
  helpUrl?: string | null;
  requiresPaid?: boolean;
  order?: number;
  hidden?: boolean;
}

export interface PropertyInfo {
  name?: string | null;
  type?: string | null;
  enumValues?: string[] | null;
  displayName?: string | null;
  defaultValue?: string | null;
  group?: string | null;
  conditions?: BotNodePropertyCondition[] | null;
}

export interface BotNodePropertyCondition {
  key?: string | null;
  value?: string | null;
}

export interface OutputInfo {
  name?: string | null;
  displayName?: string | null;
}

export interface OutputHandleInfo {
  name?: string | null;
  label?: string | null;
  color?: string | null;
}

export interface DynamicOutputHandleInfo {
  linkedProperty?: string | null;
  labelPropertyName?: string | null;
}

export interface BotNodeGroup {
  name?: string | null;
  order?: number;
}

export interface ToolInfo {
  className?: string | null;
  displayName?: string | null;
  description?: string | null;
  properties?: PropertyInfo[] | null;
  helpUrl?: string | null;
  hidden?: boolean;
}

// ---------- MCP Tool Registration Types ----------

export interface ToolDefinition {
  name: string;
  description: string;
  inputSchema: {
    type: "object";
    properties: Record<string, unknown>;
    required?: string[];
  };
}

export interface ToolGroup {
  tools: ToolDefinition[];
  handler: (name: string, args: Record<string, unknown>) => Promise<ToolResult>;
}

export interface ToolResult {
  content: Array<{ type: "text"; text: string } | { type: "text"; text: string; [key: string]: unknown }>;
  isError?: boolean;
  structuredContent?: unknown;
}
