/**
 * BoostBot Frontend Types
 */
import { t } from '@/lib/i18n';

// ===========================
// Window Global Types
// ===========================

declare global {
  interface Window {
    boostbot_config?: BoostBotConfig;
  }
}

export interface BoostBotConfig {
  chat_available?: boolean;
  bot_id?: number;
  api_url?: string;
  rest_url?: string;
  nonce?: string;
  is_rtl?: boolean;
  locale?: string;
  primary_language?: string;
  current_page?: {
    id: number;
    url: string;
    title: string;
  };
  woocommerce?: {
    active?: boolean;
    show_cart_button?: boolean;
    currency_symbol?: string;
    currency_code?: string;
    is_product?: boolean;
    is_shop?: boolean;
    is_cart?: boolean;
    is_checkout?: boolean;
    cart_url?: string;
    checkout_url?: string;
    product?: {
      id: number;
      name: string;
      price?: string;
      image?: string;
      currency_code?: string;
    } | null;
  };
}

export interface CartContextItem {
  product_id: number;
  name: string;
  quantity: number;
  price_display: string;
}

export interface CommerceContextLite {
  item_count?: number;
  subtotal_display?: string;
  currency?: string;
  currency_symbol?: string;
  is_cart_page?: boolean;
  is_checkout_page?: boolean;
  items?: CartContextItem[];
}

export interface WooCartItem {
  key: string;
  id: number;
  variation_id?: number;
  name: string;
  quantity: number;
  price: string;
  price_display?: string;
  subtotal: string;
  subtotal_display?: string;
  image?: string | null;
  product_url?: string;
}

export interface WooAppliedCoupon {
  code: string;
  discount_total?: string;
  discount_total_display?: string;
  free_shipping?: boolean;
}

export interface WooCart {
  count: number;
  subtotal: string;
  subtotal_amount?: string;
  total: string;
  total_amount?: string;
  discount_total?: string;
  discount_total_display?: string;
  currency_code?: string;
  currency_symbol?: string;
  applied_coupons?: WooAppliedCoupon[];
  items: WooCartItem[];
  checkout_url: string;
  cart_url: string;
  is_empty: boolean;
}

export interface CommerceActionHint {
  type: string;
  widget_type?: string;
  widget_id?: number;
  product_id?: number;
  reason?: string;
}

export type CommerceEventType =
  | 'product_card_viewed'
  | 'product_viewed'
  | 'product_added_to_cart'
  | 'product_add_to_cart_failed'
  | 'cart_opened'
  | 'cart_item_quantity_updated'
  | 'cart_item_removed'
  | 'checkout_clicked'
  | 'coupon_viewed'
  | 'coupon_copied'
  | 'coupon_applied'
  | 'coupon_apply_failed'
  | 'coupon_removed'
  | 'bundle_built'
  | 'bundle_item_swapped'
  | 'bundle_added_to_cart'
  | 'bundle_add_to_cart_failed';

export interface CommerceEvent {
  event_type: CommerceEventType;
  timestamp?: string;
  conversation_id?: string | null;
  visitor_id?: string | null;
  bot_id?: number | null;
  language?: string;
  currency_code?: string;
  widget_type?: string;
  widget_id?: number | null;
  source_page?: string;
  product_id?: number | null;
  product_name?: string | null;
  variation_id?: number | null;
  quantity?: number | null;
  cart_item_key?: string | null;
  cart_item_count?: number | null;
  cart_subtotal?: number | null;
  cart_total?: number | null;
  discount_total?: number | null;
  coupon_code?: string | null;
  bundle_id?: number | null;
  bundle_size?: number | null;
  bundle_value?: number | null;
  order_value?: number | null;
  metadata?: Record<string, unknown>;
}

export interface RespondContextHints {
  has_cart_context?: boolean;
  cart_item_count?: number;
}

export interface RespondMetaV2Optional {
  protocol_version?: string;
  commerce_actions?: CommerceActionHint[];
  context_hints?: RespondContextHints;
}

// ===========================
// API Types
// ===========================

export interface SendMessageRequest {
  message: string;
  visitor_id: string;
  conversation_id?: string | null;
  protocol_version?: string;
  context?: {
    id?: number;
    post_id?: number;
    page_id?: number;
    page_url?: string;
    page_title?: string;
    url?: string;
    referrer?: string;
  };
  cart_context?: CommerceContextLite;
  history?: Array<{ role: string; content: string }>;
  bot_id?: number | null;  // Bot ID for widget detection
  /** Per-question AI instructions from opening screen (questions_ai mode).
   *  Injected into system prompt with high priority on the backend. */
  ai_instructions?: string;
}

export interface SendMessageResponse {
  success: boolean;
  data: {
    reply: string;
    conversation_id: string;
    widgets?: Widget[];
    suggestions?: string[];
    /**
     * When true, backend indicates chat should be blocked for this visitor/session
     * (e.g. no credits, site disabled). Frontend uses this to hide the widget.
     */
    blocked?: boolean;
    /**
     * Bot config including behavior settings (proactive engagement, etc.)
     * Returned from orchestrated retrieval endpoint
     */
    bot_config?: {
      name: string;
      personality?: string;
      output_limit?: number;
      enabled_widgets?: string[];
      language?: string;
      behavior?: {
        proactive_engagement?: boolean;
        proactive_delay_seconds?: number;
      };
      widget_assignments?: Array<{
        type: string;
        name?: string;
        id?: number;
        description?: string;
        keywords?: string[];
        instruction?: string;
        has_phone_field?: boolean;
      }>;
    };
    /** Per-visitor message rate limiting (Week 33) */
    rate_limited?: boolean;
    limit_type?: 'hourly' | 'daily' | 'cooldown';
    retry_after?: number;
    remaining_hour?: number;
    remaining_day?: number;
    cooldown_seconds?: number;
  };
}

export interface ConfigResponse {
  success: boolean;
  data: ChatConfig & {
    bot_id?: number;
    page_context?: {
      title?: string;
      content?: string;
    };
  };
}

// ===========================
// Chat Types
// ===========================

export interface ChatMessage {
  id: string;
  role: 'user' | 'bot';
  content: string;
  timestamp: Date;
  widgets?: Widget[];
  isStreaming?: boolean;
}

// Re-export from utils for convenience
export type { FieldValue, GlobalValues, TypographyScale } from '@/utils/resolveAppearance';

// ===========================
// Element Settings Types
// ===========================

export interface TypographySettings {
  fontFamily?: import('@/utils/resolveAppearance').FieldValue;
  fontSize?: import('@/utils/resolveAppearance').FieldValue;
  fontWeight?: import('@/utils/resolveAppearance').FieldValue;
  lineHeight?: import('@/utils/resolveAppearance').FieldValue;
  letterSpacing?: import('@/utils/resolveAppearance').FieldValue;
  textTransform?: import('@/utils/resolveAppearance').FieldValue;
  fontStyle?: import('@/utils/resolveAppearance').FieldValue;
  textDecoration?: import('@/utils/resolveAppearance').FieldValue;
  wordSpacing?: import('@/utils/resolveAppearance').FieldValue;
  textShadow?: import('@/utils/resolveAppearance').FieldValue;
}

export interface HeaderSettings {
  background?: import('@/utils/resolveAppearance').FieldValue;
  text?: import('@/utils/resolveAppearance').FieldValue;
  height?: import('@/utils/resolveAppearance').FieldValue;
  padding?: import('@/utils/resolveAppearance').FieldValue;
  showCloseButton?: boolean;
  showMinimizeButton?: boolean;
  /** Header container border radius (top corners) */
  borderRadius?: import('@/utils/resolveAppearance').FieldValue;
  /** Header container border style */
  borderStyle?: 'none' | 'solid' | 'dashed' | 'dotted';
  /** Header container border width */
  borderWidth?: import('@/utils/resolveAppearance').FieldValue | { top: string; right: string; bottom: string; left: string };
  /** Header container border color */
  borderColor?: import('@/utils/resolveAppearance').FieldValue;
  typography?: TypographySettings;

  // Subtitle typography (styles only; content is not controlled here)
  subtitleTypography?: TypographySettings;

  // Avatar/Icon wrapper styling (styles only)
  avatarSize?: import('@/utils/resolveAppearance').FieldValue;
  avatarBackground?: import('@/utils/resolveAppearance').FieldValue;
  avatarBorderRadius?: import('@/utils/resolveAppearance').FieldValue;
  avatarBorderWidth?: import('@/utils/resolveAppearance').FieldValue | { top: string; right: string; bottom: string; left: string };
  avatarBorderColor?: import('@/utils/resolveAppearance').FieldValue;
  /** Border style/type for the avatar wrapper */
  avatarBorderStyle?: 'none' | 'solid' | 'dashed' | 'dotted';
  /** Icon color inside the avatar wrapper (separate from header text color) */
  avatarIconColor?: import('@/utils/resolveAppearance').FieldValue;
  /** Which fallback icon to show in the avatar wrapper */
  avatarIcon?: string;
  /** Icon size inside the avatar wrapper */
  avatarIconSize?: import('@/utils/resolveAppearance').FieldValue;
  /** Online status indicator (dot) */
  statusIndicatorSize?: import('@/utils/resolveAppearance').FieldValue;
  statusIndicatorBackground?: import('@/utils/resolveAppearance').FieldValue;
  statusIndicatorBorderWidth?: import('@/utils/resolveAppearance').FieldValue;
  statusIndicatorBorderColor?: import('@/utils/resolveAppearance').FieldValue;

  // Header action buttons styling (Close / Reset Chat)
  actionButtonSize?: import('@/utils/resolveAppearance').FieldValue;
  actionIconSize?: import('@/utils/resolveAppearance').FieldValue;
  minimizeButtonStyle?: ButtonStyleSettings;
  closeButtonStyle?: ButtonStyleSettings;
  resetButtonStyle?: ButtonStyleSettings;

  // Content fields (kept for compatibility; not controlled by style editors)
  title?: string;
  subtitle?: string;
  logoUrl?: string;
}

export interface ChatWindowSettings {
  width?: import('@/utils/resolveAppearance').FieldValue;
  height?: import('@/utils/resolveAppearance').FieldValue;
  background?: import('@/utils/resolveAppearance').FieldValue;
  borderRadius?: import('@/utils/resolveAppearance').FieldValue;
  /** Border style/type for the chat window container */
  borderType?: 'none' | 'solid' | 'dashed' | 'dotted';
  /** Border width for the chat window container */
  borderWidth?: import('@/utils/resolveAppearance').FieldValue;
  /** Border color for the chat window container */
  borderColor?: import('@/utils/resolveAppearance').FieldValue;
  shadow?: import('@/utils/resolveAppearance').FieldValue;
  padding?: import('@/utils/resolveAppearance').FieldValue;
  /** Messages list area padding (CSS shorthand string) */
  messagesPadding?: import('@/utils/resolveAppearance').FieldValue;
  /** Vertical spacing between messages (CSS size string) */
  messagesGap?: import('@/utils/resolveAppearance').FieldValue;
}

export interface ChatBubbleSettings {
  size?: import('@/utils/resolveAppearance').FieldValue;
  background?: import('@/utils/resolveAppearance').FieldValue;
  icon?: string;
  customIconUrl?: string;
  animation?: 'none' | 'pulse' | 'bounce' | 'shake' | 'glow' | 'wiggle' | 'float';
  position?: 'bottom-right' | 'bottom-left' | 'top-right' | 'top-left';
  margin?: import('@/utils/resolveAppearance').FieldValue | PerSideValue;
  /** Icon styling (styles only) */
  iconColor?: import('@/utils/resolveAppearance').FieldValue;
  iconSize?: import('@/utils/resolveAppearance').FieldValue;
  /** Effects */
  shadow?: import('@/utils/resolveAppearance').FieldValue;
  /** Border (compound controls) */
  borderType?: 'none' | 'solid' | 'dashed' | 'dotted';
  /** Border width can be legacy single FieldValue or a per-side object */
  borderWidth?: PerSideValue | import('@/utils/resolveAppearance').FieldValue;
  borderColor?: import('@/utils/resolveAppearance').FieldValue;
  borderRadius?: import('@/utils/resolveAppearance').FieldValue;
  /** Unread badge settings */
  unreadBadge?: ChatBubbleUnreadBadgeSettings;
  /** Pulse ring (unread) */
  pulseRing?: ChatBubblePulseRingSettings;
  /** Animation fine-tuning */
  animationSettings?: ChatBubbleAnimationSettings;
}

export interface ChatBubbleUnreadBadgeSettings {
  background?: import('@/utils/resolveAppearance').FieldValue;
  textColor?: import('@/utils/resolveAppearance').FieldValue;
  size?: import('@/utils/resolveAppearance').FieldValue;
  minWidth?: import('@/utils/resolveAppearance').FieldValue;
  paddingX?: import('@/utils/resolveAppearance').FieldValue;
  offsetX?: import('@/utils/resolveAppearance').FieldValue;
  offsetY?: import('@/utils/resolveAppearance').FieldValue;
  borderRadius?: import('@/utils/resolveAppearance').FieldValue;
  typography?: TypographySettings;
}

export interface ChatBubblePulseRingSettings {
  color?: import('@/utils/resolveAppearance').FieldValue;
  scale?: import('@/utils/resolveAppearance').FieldValue;
  opacityStart?: import('@/utils/resolveAppearance').FieldValue;
  opacityEnd?: import('@/utils/resolveAppearance').FieldValue;
  duration?: import('@/utils/resolveAppearance').FieldValue;
}

export interface ChatBubbleAnimationSettings {
  pulseScale?: import('@/utils/resolveAppearance').FieldValue;
  pulseDuration?: import('@/utils/resolveAppearance').FieldValue;
  bounceDistance?: import('@/utils/resolveAppearance').FieldValue;
  bounceDuration?: import('@/utils/resolveAppearance').FieldValue;
  shakeRotation?: import('@/utils/resolveAppearance').FieldValue;
  shakeDuration?: import('@/utils/resolveAppearance').FieldValue;
  shakeDelay?: import('@/utils/resolveAppearance').FieldValue;
}

export interface MessageSettings {
  background?: import('@/utils/resolveAppearance').FieldValue;
  text?: import('@/utils/resolveAppearance').FieldValue;
  borderRadius?: import('@/utils/resolveAppearance').FieldValue;
  padding?: import('@/utils/resolveAppearance').FieldValue;
  maxWidth?: import('@/utils/resolveAppearance').FieldValue;
  fontSize?: import('@/utils/resolveAppearance').FieldValue;
  typography?: TypographySettings;
}

export interface UserMessageSettings extends MessageSettings {
  timestampTypography?: TypographySettings;
  timestampOpacity?: import('@/utils/resolveAppearance').FieldValue;
  showAvatar?: boolean;
  avatarSize?: import('@/utils/resolveAppearance').FieldValue;
  avatarBackground?: import('@/utils/resolveAppearance').FieldValue;
  avatarIconColor?: import('@/utils/resolveAppearance').FieldValue;
  avatarIconSize?: import('@/utils/resolveAppearance').FieldValue;
}

export interface BotMessageSettings extends MessageSettings {
  showAvatar?: boolean;
  avatarUrl?: string;
  timestampTypography?: TypographySettings;
  timestampOpacity?: import('@/utils/resolveAppearance').FieldValue;
  avatarSize?: import('@/utils/resolveAppearance').FieldValue;
  avatarBackground?: import('@/utils/resolveAppearance').FieldValue;
  avatarIconColor?: import('@/utils/resolveAppearance').FieldValue;
  avatarIconSize?: import('@/utils/resolveAppearance').FieldValue;
  shadow?: import('@/utils/resolveAppearance').FieldValue;
  borderColor?: import('@/utils/resolveAppearance').FieldValue;
  borderWidth?: import('@/utils/resolveAppearance').FieldValue;
}

// Per-side border/padding value type
export interface PerSideValue {
  top: string;
  right: string;
  bottom: string;
  left: string;
}

// Per-corner radius value type
export interface PerCornerValue {
  topLeft: string;
  topRight: string;
  bottomRight: string;
  bottomLeft: string;
}

// Full button style settings (Elementor-style)
export interface ButtonStyleSettings {
  typography?: {
    fontFamily?: import('@/utils/resolveAppearance').FieldValue;
    fontSize?: import('@/utils/resolveAppearance').FieldValue;
    fontWeight?: import('@/utils/resolveAppearance').FieldValue;
    textTransform?: import('@/utils/resolveAppearance').FieldValue;
    fontStyle?: import('@/utils/resolveAppearance').FieldValue;
    textDecoration?: import('@/utils/resolveAppearance').FieldValue;
    lineHeight?: import('@/utils/resolveAppearance').FieldValue;
    letterSpacing?: import('@/utils/resolveAppearance').FieldValue;
  };
  textColor?: import('@/utils/resolveAppearance').FieldValue;
  backgroundColor?: import('@/utils/resolveAppearance').FieldValue;
  borderType?: 'none' | 'solid' | 'dashed' | 'dotted';
  borderWidth?: PerSideValue;
  borderColor?: import('@/utils/resolveAppearance').FieldValue;
  borderRadius?: PerCornerValue | import('@/utils/resolveAppearance').FieldValue;
  boxShadow?: import('@/utils/resolveAppearance').FieldValue;
  padding?: PerSideValue;
  icon?: {
    /** Optional icon name (Lucide icon key, e.g. "shopping-cart") */
    icon?: string;
    position?: 'before' | 'after' | 'only';
    /** Size may be stored as a string (e.g. "20px") */
    size?: string | import('@/utils/resolveAppearance').FieldValue;
    /** Color supports global linking */
    color?: import('@/utils/resolveAppearance').FieldValue;
    /** Spacing may be stored as a string (e.g. "8px") */
    spacing?: string | import('@/utils/resolveAppearance').FieldValue;
    /** Hover color supports global linking */
    hoverColor?: import('@/utils/resolveAppearance').FieldValue;
  };
  hover?: {
    textColor?: import('@/utils/resolveAppearance').FieldValue;
    backgroundColor?: import('@/utils/resolveAppearance').FieldValue;
    borderColor?: import('@/utils/resolveAppearance').FieldValue;
    boxShadow?: import('@/utils/resolveAppearance').FieldValue;
    /** Optional hover icon color (supports global linking) */
    iconColor?: import('@/utils/resolveAppearance').FieldValue;
  };
  transitionDuration?: string;
}

export interface InputAreaSettings {
  background?: import('@/utils/resolveAppearance').FieldValue;
  text?: import('@/utils/resolveAppearance').FieldValue;
  placeholder?: string;
  placeholderColor?: import('@/utils/resolveAppearance').FieldValue;
  border?: import('@/utils/resolveAppearance').FieldValue;
  borderWidth?: import('@/utils/resolveAppearance').FieldValue;
  borderRadius?: import('@/utils/resolveAppearance').FieldValue;
  padding?: import('@/utils/resolveAppearance').FieldValue;
  typography?: {
    fontFamily?: import('@/utils/resolveAppearance').FieldValue;
    fontSize?: import('@/utils/resolveAppearance').FieldValue;
    fontWeight?: import('@/utils/resolveAppearance').FieldValue;
    textTransform?: import('@/utils/resolveAppearance').FieldValue;
    textAlign?: import('@/utils/resolveAppearance').FieldValue;
    fontStyle?: import('@/utils/resolveAppearance').FieldValue;
    textDecoration?: import('@/utils/resolveAppearance').FieldValue;
    lineHeight?: import('@/utils/resolveAppearance').FieldValue;
    letterSpacing?: import('@/utils/resolveAppearance').FieldValue;
    textShadow?: import('@/utils/resolveAppearance').FieldValue;
  };
  helperTextColor?: import('@/utils/resolveAppearance').FieldValue;
  helperTextTypography?: {
    fontFamily?: import('@/utils/resolveAppearance').FieldValue;
    fontSize?: import('@/utils/resolveAppearance').FieldValue;
    fontWeight?: import('@/utils/resolveAppearance').FieldValue;
    textTransform?: import('@/utils/resolveAppearance').FieldValue;
    textAlign?: import('@/utils/resolveAppearance').FieldValue;
    fontStyle?: import('@/utils/resolveAppearance').FieldValue;
    textDecoration?: import('@/utils/resolveAppearance').FieldValue;
    lineHeight?: import('@/utils/resolveAppearance').FieldValue;
    letterSpacing?: import('@/utils/resolveAppearance').FieldValue;
    textShadow?: import('@/utils/resolveAppearance').FieldValue;
  };
  counterTextColor?: import('@/utils/resolveAppearance').FieldValue;
  counterTextTypography?: {
    fontFamily?: import('@/utils/resolveAppearance').FieldValue;
    fontSize?: import('@/utils/resolveAppearance').FieldValue;
    fontWeight?: import('@/utils/resolveAppearance').FieldValue;
    textTransform?: import('@/utils/resolveAppearance').FieldValue;
    textAlign?: import('@/utils/resolveAppearance').FieldValue;
    fontStyle?: import('@/utils/resolveAppearance').FieldValue;
    textDecoration?: import('@/utils/resolveAppearance').FieldValue;
    lineHeight?: import('@/utils/resolveAppearance').FieldValue;
    letterSpacing?: import('@/utils/resolveAppearance').FieldValue;
    textShadow?: import('@/utils/resolveAppearance').FieldValue;
  };
  // Legacy send button settings
  sendButtonBackground?: import('@/utils/resolveAppearance').FieldValue;
  sendButtonIcon?: import('@/utils/resolveAppearance').FieldValue;
  sendButtonHoverBackground?: import('@/utils/resolveAppearance').FieldValue;
  sendButtonHoverShadow?: import('@/utils/resolveAppearance').FieldValue;
  // New comprehensive send button style
  sendButton?: ButtonStyleSettings;
}

export interface TypingIndicatorSettings {
  background?: import('@/utils/resolveAppearance').FieldValue;
  dotColor?: import('@/utils/resolveAppearance').FieldValue;
  dotSize?: import('@/utils/resolveAppearance').FieldValue;
  animationSpeed?: import('@/utils/resolveAppearance').FieldValue;
}

export interface QuickButtonsSettings {
  // Legacy settings
  background?: import('@/utils/resolveAppearance').FieldValue;
  text?: import('@/utils/resolveAppearance').FieldValue;
  border?: import('@/utils/resolveAppearance').FieldValue;
  borderRadius?: import('@/utils/resolveAppearance').FieldValue;
  padding?: import('@/utils/resolveAppearance').FieldValue;
  hoverBackground?: import('@/utils/resolveAppearance').FieldValue;
  hoverText?: import('@/utils/resolveAppearance').FieldValue;
  hoverShadow?: import('@/utils/resolveAppearance').FieldValue;
  typography?: {
    fontFamily?: import('@/utils/resolveAppearance').FieldValue;
    fontSize?: import('@/utils/resolveAppearance').FieldValue;
    fontWeight?: import('@/utils/resolveAppearance').FieldValue;
    textTransform?: import('@/utils/resolveAppearance').FieldValue;
    fontStyle?: import('@/utils/resolveAppearance').FieldValue;
    textDecoration?: import('@/utils/resolveAppearance').FieldValue;
    lineHeight?: import('@/utils/resolveAppearance').FieldValue;
    letterSpacing?: import('@/utils/resolveAppearance').FieldValue;
  };
  gap?: import('@/utils/resolveAppearance').FieldValue;
  containerPadding?: import('@/utils/resolveAppearance').FieldValue;
  // New comprehensive button style
  buttonStyle?: ButtonStyleSettings;
}

export interface CouponWidgetSettings {
  container?: {
    background?: import('@/utils/resolveAppearance').FieldValue;
    borderRadius?: import('@/utils/resolveAppearance').FieldValue;
    borderStyle?: string;
    borderColor?: import('@/utils/resolveAppearance').FieldValue;
  };
  header?: {
    background?: import('@/utils/resolveAppearance').FieldValue;
    color?: import('@/utils/resolveAppearance').FieldValue;
    font?: import('@/utils/resolveAppearance').FieldValue;
    fontSize?: import('@/utils/resolveAppearance').FieldValue;
  };
  description?: {
    color?: import('@/utils/resolveAppearance').FieldValue;
    fontSize?: import('@/utils/resolveAppearance').FieldValue;
  };
  code?: {
    background?: import('@/utils/resolveAppearance').FieldValue;
    color?: import('@/utils/resolveAppearance').FieldValue;
    font?: import('@/utils/resolveAppearance').FieldValue;
    fontSize?: import('@/utils/resolveAppearance').FieldValue;
    borderRadius?: import('@/utils/resolveAppearance').FieldValue;
  };
  copyButton?: {
    background?: import('@/utils/resolveAppearance').FieldValue;
    color?: import('@/utils/resolveAppearance').FieldValue;
    hoverBackground?: import('@/utils/resolveAppearance').FieldValue;
    borderRadius?: import('@/utils/resolveAppearance').FieldValue;
  };
  badge?: {
    background?: import('@/utils/resolveAppearance').FieldValue;
    color?: import('@/utils/resolveAppearance').FieldValue;
    borderRadius?: import('@/utils/resolveAppearance').FieldValue;
  };
  expiry?: {
    color?: import('@/utils/resolveAppearance').FieldValue;
    font?: import('@/utils/resolveAppearance').FieldValue;
    fontSize?: import('@/utils/resolveAppearance').FieldValue;
  };
  removeLink?: {
    color?: import('@/utils/resolveAppearance').FieldValue;
    hoverColor?: import('@/utils/resolveAppearance').FieldValue;
  };
}

export interface ElementSettings {
  header?: HeaderSettings;
  chatWindow?: ChatWindowSettings;
  chatBubble?: ChatBubbleSettings;
  userMessage?: UserMessageSettings;
  botMessage?: BotMessageSettings;
  inputArea?: InputAreaSettings;
  typingIndicator?: TypingIndicatorSettings;
  quickButtons?: QuickButtonsSettings;
}

// ===========================
// Chat Config Type
// ===========================

// Opening Screen Types
export interface OpeningScreenData {
  has_opening_screen: boolean;
  preset_id?: number;
  mode?: 'questions_ai' | 'questions_answers' | 'departments';
  welcome_text?: string;
  show_welcome?: boolean;
  questions?: Array<{ text: string; icon?: string; instructions?: string }>;
  ai_instructions?: string;
  qa_pairs?: Array<{ question: string; answer: string; icon?: string }>;
}

/** Options for sendMessage to support different opening screen modes */
export interface SendMessageOptions {
  /** AI instructions to include as context (for questions_ai mode) */
  aiInstructions?: string;
}

export interface ChatConfig {
  bot_name: string;
  welcome_message: string;
  show_welcome_message?: boolean;
  primary_color: string;
  position: 'bottom-right' | 'bottom-left';
  avatar_url: string | null;
  show_on_mobile: boolean;
  auto_open_delay: number;
  widgets_enabled: string[];
  triggers?: TriggerConfig;
  
  // Mobile Layout Settings (vw/vh based for responsive chat window)
  // Individual properties (legacy/flat format)
  mobile_layout_width_vw?: number;      // Default: 94
  mobile_layout_height_vh?: number;     // Default: 75
  mobile_layout_margin_top_vh?: number; // Default: 20
  mobile_layout_margin_bottom_vh?: number; // Default: 5
  
  // Mobile layout object (from config endpoint)
  // Note: side margin is auto-calculated from width: (100 - width) / 2
  mobile_layout?: {
    width_vw: number;          // Default: 94 (94% of viewport width)
    height_vh: number;         // Default: 75 (75% of viewport height)
    margin_top_vh: number;     // Default: 20 (leave space for site header)
    margin_bottom_vh: number;  // Default: 5
  };
  /** If false, the widget should not render on this page (e.g. default bot excluded) */
  is_active?: boolean;
  /** True when the resolved bot is in admin-only test mode. */
  is_test_mode?: boolean;
  /**
   * If false, chat is not available (no credits / site blocked / backend not configured).
   * Frontend should hide the entire widget (no bubble).
   */
  chat_available?: boolean;
  
  // Opening screen preset data
  opening_screen?: OpeningScreenData;

  // WooCommerce storefront context from /config
  woocommerce?: BoostBotConfig['woocommerce'];
  
  // Full appearance preset from backend
  appearance_preset?: {
    id: number;
    name: string;
    global_values: import('@/utils/resolveAppearance').GlobalValues;
    element_settings: ElementSettings;
    widget_settings: Record<string, unknown>;
  };
  
  // Flattened appearance settings (legacy support)
  appearance?: {
    primary_color?: string;
    secondary_color?: string;
    accent_color?: string;
    background_color?: string;
    text_color?: string;
    font_family?: string;
  };
  
  // Direct element settings (can come from either appearance_preset or directly)
  element_settings?: ElementSettings;
  
  // Custom font CSS for Elementor custom fonts
  custom_font_css?: string;

  // Week 26 credits watermark (safe to expose)
  credit_type?: 'free' | 'purchased' | 'none' | null;
  site_status?: 'active' | 'blocked' | 'disabled' | null;
  watermark?: {
    enabled: boolean;
    text: string;
  };

  // Week 28: character caps (provided by /config)
  visitor_input_char_limit_enabled?: boolean;
  visitor_input_char_limit?: number;
  bot_output_char_limit_global?: number;
  bot_output_char_limit?: number;

  // Per-visitor message rate limiting (Week 33)
  visitor_message_limit_enabled?: boolean;
  visitor_messages_per_hour?: number;
  visitor_messages_per_day?: number;
  visitor_cooldown_enabled?: boolean;
  
  // Direct streaming configuration (browser → backend)
  direct_streaming_enabled?: boolean;
  backend_stream_url?: string;
  // Backward compatibility with older backend/frontend field naming.
  vercel_stream_url?: string;
  // Legacy streaming (WordPress proxy)
  streaming_enabled?: boolean;
  
  // Orchestrated flow (Gemini + GPT)
  orchestrated_flow_enabled?: boolean;
  
  // Page transition triggers (when user navigates to page with different bot)
  page_transition_triggers_enabled?: boolean;

  // Multilingual: the resolved bot's language code (e.g. 'he', 'en')
  language?: string;
  is_rtl?: boolean;
}

// ===========================
// Widget Types
// ===========================

export interface Widget {
  id?: string | number;
  type: string;
  config: Record<string, unknown>;
  data?: Record<string, unknown>;
}

// ===========================
// Trigger Types
// ===========================

export type TriggerType = 
  | 'exit_intent'
  | 'scroll_depth'
  | 'time_on_page'
  | 'returning_visitor'
  | 'cart_abandoned';

export interface TriggerSettings {
  enabled: boolean;
  message: string;
  threshold?: number;
  seconds?: number;
}

export interface TriggerConfig {
  exit_intent: TriggerSettings;
  scroll_depth: TriggerSettings & { threshold: number };
  time_on_page: TriggerSettings & { seconds: number; threshold?: number };
  returning_visitor: TriggerSettings;
  cart_abandoned: TriggerSettings;
}

export function getDefaultTriggers(): TriggerConfig {
  return {
    exit_intent: {
      enabled: false,
      message: t('Wait! Before you leave... I have a special offer for you') + ' 🎁',
    },
    scroll_depth: {
      enabled: false,
      threshold: 50,
      message: t('I see you\'re interested! I have something that can help you') + ' 💡',
    },
    time_on_page: {
      enabled: false,
      seconds: 30,
      message: t('Hi! I\'d love to help you find what you\'re looking for') + ' 🔍',
    },
    returning_visitor: {
      enabled: false,
      message: t('Great to see you again!') + ' 👋 ' + t('How can I help today?'),
    },
    cart_abandoned: {
      enabled: false,
      message: t('I noticed you have items in your cart') + ' 🛒 ' + t('Can I help complete your purchase?'),
    },
  };
}
