// API Response Types
export interface User {
  id: string;
  username: string;
  email: string;
  full_name?: string;
  role: 'admin' | 'user';
  active: boolean;
  created_at: string;
  updated_at: string;
}

export interface Hook {
  id: string;
  user_id: string;
  name: string;
  url: string;
  method: string;
  headers?: string; // JSON string
  events?: string; // JSON string
  description?: string;
  secret?: string;
  retry_count: number;
  timeout: number;
  active: boolean;
  created_at: string;
  updated_at: string;
}

export interface WhatsAppAccount {
  id: string;
  user_id: string;
  account_id?: string; // User-friendly unique identifier
  phone_number: string;
  display_name?: string;
  account_type: 'whatsmeow' | 'business-api';
  status: 'connected' | 'disconnected' | 'connecting' | 'pairing';
  show_qr?: boolean;
  business_api?: BusinessAPIConfig;
  config?: string; // JSON string
  session_path?: string;
  last_connected_at?: string;
  active: boolean;
  created_at: string;
  updated_at: string;
}

export interface BusinessAPIConfig {
  phone_number_id?: string;
  access_token?: string;
  waba_id?: string;
  business_account_id?: string;
  api_version?: string;
  webhook_path?: string;
  verify_token?: string;
  [key: string]: unknown;
}

export interface ThroughputInfo {
  level?: string;
}

export interface PhoneNumberListItem {
  id: string;
  display_phone_number: string;
  phone_number: string;
  verified_name: string;
  code_verification_status: string;
  quality_rating: string;
  throughput?: ThroughputInfo;
}

export interface PhoneNumberListResponse {
  data: PhoneNumberListItem[];
  paging?: Record<string, unknown>;
}

export interface BusinessProfile {
  about?: string;
  address?: string;
  description?: string;
  email?: string;
  websites?: string[];
  vertical?: string;
  profile_picture_url?: string;
}

export interface BusinessProfileUpdateRequest {
  account_id: string;
  about?: string;
  address?: string;
  description?: string;
  email?: string;
  websites?: string[];
  vertical?: string;
}

export interface TemplateComponentExample {
  header_handle?: string[];
  body_example?: string[][];
}

export interface TemplateButtonDef {
  type: string;
  text: string;
  url?: string;
  phone_number?: string;
  dynamic?: boolean;
}

export interface TemplateComponentDef {
  type: string;
  format?: string;
  text?: string;
  buttons?: TemplateButtonDef[];
  example?: TemplateComponentExample;
}

export interface TemplateInfo {
  id: string;
  name: string;
  status: string;
  language: string;
  category: string;
  created_at: string;
  components: TemplateComponentDef[];
  rejection_reasons?: string[];
  quality_score?: string;
}

export interface TemplateListResponse {
  data: TemplateInfo[];
  paging?: Record<string, unknown>;
}

export interface TemplateUploadButton {
  type: string;
  text: string;
  url?: string;
  phone_number?: string;
  example?: string[];
}

export interface TemplateUploadExample {
  header_handle?: string[];
  body_example?: string[][];
}

export interface TemplateUploadComponent {
  type: string;
  format?: string;
  text?: string;
  buttons?: TemplateUploadButton[];
  example?: TemplateUploadExample;
}

export interface TemplateUploadRequest {
  account_id: string;
  name: string;
  language: string;
  category: string;
  components: TemplateUploadComponent[];
  allow_category_change?: boolean;
}

export interface FlowInfo {
  id: string;
  name: string;
  status: string;
  categories: string[];
  created_at: string;
  updated_at: string;
  endpoint_url?: string;
  preview_url?: string;
  signed_preview_url?: string;
  signed_flow_url?: string;
}

export interface FlowListResponse {
  data: FlowInfo[];
  paging?: Record<string, unknown>;
}

export interface CatalogInfo {
  id: string;
  name: string;
  product_count?: number;
}

export interface CatalogListResponse {
  data: CatalogInfo[];
  paging?: Record<string, unknown>;
}

export interface WhatsAppAccountConfig {
  id: string;
  type: 'whatsmeow' | 'business-api';
  phone_number: string;
  session_path?: string;
  show_qr?: boolean;
  disabled?: boolean;
  status?: 'connected' | 'disconnected' | 'connecting' | 'pairing';
  connected?: boolean;
  qr_available?: boolean;
  business_api?: BusinessAPIConfig;
}

export interface WhatsAppAccountRuntimeStatus {
  account_id: string;
  type: string;
  status: 'connected' | 'disconnected' | 'connecting' | 'pairing';
  connected: boolean;
  qr_available: boolean;
}

export interface EventLog {
  id: string;
  user_id?: string;
  event_type: string;
  action?: string;
  entity_type?: string;
  entity_id?: string;
  data?: string; // JSON string
  success: boolean;
  error?: string;
  ip_address?: string;
  user_agent?: string;
  created_at: string;
}

export interface MessageCacheEventPayload {
  type: string;
  timestamp: string;
  data?: Record<string, unknown>;
}

export interface MessageCacheEvent {
  id: string;
  event: MessageCacheEventPayload;
  timestamp: string;
  reason: string;
  attempts: number;
  last_attempt?: string;
}

export interface MessageCacheListResponse {
  cached_events: MessageCacheEvent[];
  count: number;
  filtered_count: number;
  returned_count: number;
  limit: number;
  offset: number;
}

export interface MessageCacheStats {
  enabled: boolean;
  count?: number;
  total_count?: number;
  by_event_type?: Record<string, number>;
}

export interface SystemStats {
  go_memory_bytes: number;
  go_memory_mb: number;
  go_sys_memory_bytes: number;
  go_sys_memory_mb: number;
  go_goroutines: number;
  go_cpu_percent: number;
  network_rx_bytes: number;
  network_tx_bytes: number;
  network_total_bytes: number;
  network_bytes_per_sec: number;
}

export interface APIKey {
  id: string;
  user_id: string;
  name: string;
  key: string;
  key_prefix?: string;
  permissions?: string; // JSON string
  expires_at?: string;
  last_used_at?: string;
  active: boolean;
  created_at: string;
  updated_at: string;
}

export interface Session {
  id: string;
  user_id: string;
  token: string;
  ip_address?: string;
  user_agent?: string;
  expires_at: string;
  created_at: string;
  updated_at: string;
}

// Auth Types
export interface LoginRequest {
  username: string;
  password: string;
}

export interface LoginResponse {
  token: string;
  user: User;
  expires_in: number;
}

// API Response wrappers
export interface ApiResponse<T> {
  data?: T;
  error?: string;
  message?: string;
}

export interface PaginatedResponse<T> {
  data: T[];
  total: number;
  page: number;
  per_page: number;
}
