// --- Contact Lists ---

export interface SmsContactList {
  id: string;
  name: string;
  originalFilename: string | null;
  totalContacts: number;
  status: 'ACTIVE' | 'ARCHIVED';
  createdByUserId: string | null;
  createdByUser?: { id: string; name: string | null; email: string };
  createdAt: string;
  updatedAt: string;
  _count?: { entries: number; campaigns: number };
}

export interface SmsContactListEntry {
  id: string;
  listId: string;
  phone: string;
  name: string | null;
  email: string | null;
  customFields: Record<string, string> | null;
  createdAt: string;
  list?: { id: string; name: string };
}

// --- Campaigns ---

export type SmsCampaignStatus = 'DRAFT' | 'RUNNING' | 'PAUSED' | 'COMPLETED' | 'FAILED';
export type SmsMessageStatus = 'PENDING' | 'SENT' | 'FAILED' | 'DELIVERED';
export type SmsMessageDirection = 'OUTBOUND' | 'INBOUND';
export type SmsResponseType = 'POSITIVE' | 'NEGATIVE' | 'QUESTION' | 'OPT_OUT' | 'NEUTRAL';
export type SmsConversationStatus = 'ACTIVE' | 'OPTED_OUT' | 'CLOSED';

export interface SmsCampaign {
  id: string;
  name: string;
  messageTemplate: string;
  status: SmsCampaignStatus;
  totalRecipients: number;
  totalSent: number;
  totalFailed: number;
  totalResponded: number;
  delayBetweenMs: number;
  startedAt: string | null;
  completedAt: string | null;
  contactListId: string;
  contactList?: { id: string; name: string; totalContacts?: number };
  advocacyCampaignId: string | null;
  advocacyCampaign?: { id: string; title: string; slug: string } | null;
  createdByUserId: string | null;
  createdByUser?: { id: string; name: string | null; email: string };
  createdAt: string;
  updatedAt: string;
  _count?: { recipients: number; messages: number; conversations: number };
}

export interface SmsCampaignRecipient {
  id: string;
  campaignId: string;
  phone: string;
  name: string | null;
  status: SmsMessageStatus;
  sentAt: string | null;
  errorMessage: string | null;
  createdAt: string;
}

// --- Messages ---

export interface SmsMessage {
  id: string;
  phone: string;
  message: string;
  direction: SmsMessageDirection;
  status: SmsMessageStatus;
  connectionType: string | null;
  campaignId: string | null;
  campaign?: { id: string; name: string } | null;
  conversationId: string | null;
  conversation?: { id: string; contactName: string | null } | null;
  responseType: SmsResponseType | null;
  isRead: boolean;
  sentAt: string;
}

// --- Conversations ---

export interface SmsConversation {
  id: string;
  phone: string;
  contactName: string | null;
  campaignId: string | null;
  campaign?: { id: string; name: string } | null;
  contactId: string | null;
  contact?: { id: string; displayName: string } | null;
  status: SmsConversationStatus;
  totalMessages: number;
  totalResponses: number;
  unreadCount: number;
  lastMessageAt: string | null;
  lastResponseAt: string | null;
  notes: string | null;
  tags: string[] | null;
  messages?: SmsMessage[];
  createdAt: string;
  updatedAt: string;
}

// --- Contact Search ---

export interface SmsContactSearchResult {
  phone: string;
  name: string | null;
  source: 'sms_contact' | 'crm_contact' | 'conversation';
  sourceId: string;
  contactId?: string;
}

// --- Templates ---

export interface SmsMessageTemplate {
  id: string;
  name: string;
  template: string;
  description: string | null;
  category: string | null;
  isFavorite: boolean;
  usageCount: number;
  createdByUserId: string | null;
  createdByUser?: { id: string; name: string | null; email: string };
  createdAt: string;
  updatedAt: string;
  variables?: string[];
  isSystem?: boolean;
}

// --- Setup ---

export interface SmsSetupStatus {
  configured: boolean;
  connected: boolean;
  tailscaleConfigured: boolean;
  source: 'database' | 'env';
}

export interface SmsSetupConfig {
  enableSms: boolean;
  smsTermuxApiUrl: string;
  smsTermuxApiKey: string;
  smsTermuxApiKeySet: boolean;
  smsTailscaleApiKey: string;
  smsTailscaleApiKeySet: boolean;
  smsTailscaleTailnet: string;
  smsTailscaleDeviceId: string;
  smsTailscaleDeviceName: string;
}

export interface TailscaleDevice {
  id: string;
  name: string;
  addresses: string[];
  os: string;
  hostname: string;
  online: boolean;
  lastSeen: string;
}

export interface SmsTestConnectionResult {
  success: boolean;
  health?: { status: string; uptime: number; messages_sent: number };
  error?: string;
}

// --- Device ---

export interface SmsDeviceStatus {
  isConnected: boolean;
  connectionType: string | null;
  batteryLevel: number | null;
  batteryStatus: string | null;
  totalSent: number;
  lastCheckedAt: string | null;
}

// --- Paginated Response ---

export interface SmsPaginatedResponse<T> {
  items: T[];
  total: number;
  page: number;
  limit: number;
}
