/**
 * Shared Instantly param/result types (browser-safe)
 *
 * These types define the public interface for Instantly operations -- used by both
 * the server-side adapter and the SDK typed wrappers. They contain zero Node.js
 * dependencies and are safe to import in any environment.
 *
 * Preserves snake_case field names to match server-side API expectations.
 *
 * Server-internal types (credentials, error handling) remain in
 * server/adapters/instantly/
 */

// ============================================================================
// Send Reply
// ============================================================================

/**
 * Send reply parameters
 */
export interface SendReplyParams {
  eaccount: string
  reply_to_uuid: string
  subject: string
  body: {
    text?: string
    html?: string
  }
  timestamp?: string
}

/**
 * Send reply result
 */
export interface SendReplyResult {
  success: boolean
  email_id: string
  thread_id: string
  sent_at: string
}

// ============================================================================
// Remove From Subsequence
// ============================================================================

/**
 * Remove from subsequence parameters
 */
export interface RemoveFromSubsequenceParams {
  lead_email: string
  campaign_id: string
}

/**
 * Remove from subsequence result
 */
export interface RemoveFromSubsequenceResult {
  success: boolean
  lead_email: string
  campaign_id: string
}

// ============================================================================
// Get Emails
// ============================================================================

/**
 * Get emails parameters
 */
export interface GetEmailsParams {
  email_id?: string
  search?: string
  campaign_id?: string
  lead?: string
  email_type?: 'received' | 'sent' | 'manual'
  eaccount?: string
  sort_order?: 'asc' | 'desc'
  preview_only?: boolean
  is_unread?: boolean
  limit?: number
  starting_after?: string
}

/**
 * Get emails result
 */
export interface GetEmailsResult {
  emails: unknown[]
  total_count: number
  next_starting_after?: string
}

// ============================================================================
// Update Interest Status
// ============================================================================

/**
 * Update interest status parameters
 */
export interface UpdateInterestStatusParams {
  lead_email: string
  interest_value: number
  campaign_id?: string
  disable_auto_interest?: boolean
}

/**
 * Update interest status result
 */
export interface UpdateInterestStatusResult {
  success: boolean
  lead_email: string
}

// ============================================================================
// Add To Campaign
// ============================================================================

/**
 * Lead entry for add to campaign
 */
export interface AddToCampaignLead {
  email: string
  first_name?: string
  last_name?: string
  company_name?: string
}

/**
 * Add to campaign parameters
 */
export interface AddToCampaignParams {
  campaign_id: string
  leads: AddToCampaignLead[]
}

/**
 * Add to campaign result
 */
export interface AddToCampaignResult {
  success: boolean
  added_count: number
  errors?: string[]
}

// ============================================================================
// List Campaigns
// ============================================================================

/**
 * List campaigns parameters
 */
export interface ListCampaignsParams {
  limit?: number
  starting_after?: string
  search?: string
  status?: number
}

/**
 * List campaigns result
 */
export interface ListCampaignsResult {
  campaigns: Array<{ id: string; name: string; status: string; created_at: string; [key: string]: unknown }>
  next_starting_after?: string
}

// ============================================================================
// Get Campaign
// ============================================================================

/**
 * Get campaign parameters
 */
export interface GetCampaignParams {
  campaign_id: string
}

/**
 * Get campaign result
 */
export interface GetCampaignResult {
  id: string
  name: string
  status: string
  sequences: unknown[]
  created_at: string
  [key: string]: unknown
}

// ============================================================================
// Update Campaign
// ============================================================================

/**
 * Update campaign parameters
 */
export interface UpdateCampaignParams {
  campaign_id: string
  name?: string
  sequences?: unknown
  email_list?: string[]
  daily_limit?: number
  stop_on_reply?: boolean
  track_opens?: boolean
  track_clicks?: boolean
  [key: string]: unknown
}

/**
 * Update campaign result
 */
export interface UpdateCampaignResult {
  success: boolean
  campaign_id: string
}

// ============================================================================
// Pause Campaign
// ============================================================================

/**
 * Pause campaign parameters
 */
export interface PauseCampaignParams {
  campaign_id: string
}

/**
 * Pause campaign result
 */
export interface PauseCampaignResult {
  success: boolean
  campaign_id: string
}

// ============================================================================
// Activate Campaign
// ============================================================================

/**
 * Activate campaign parameters
 */
export interface ActivateCampaignParams {
  campaign_id: string
}

/**
 * Activate campaign result
 */
export interface ActivateCampaignResult {
  success: boolean
  campaign_id: string
}

// ============================================================================
// Get Campaign Analytics
// ============================================================================

/**
 * Get campaign analytics parameters
 */
export interface GetCampaignAnalyticsParams {
  campaign_id: string
  start_date?: string
  end_date?: string
}

/**
 * Get campaign analytics result
 */
export interface GetCampaignAnalyticsResult {
  campaign_id: string
  sent: number
  opened: number
  unique_opened: number
  replied: number
  unique_replied: number
  bounced: number
  unsubscribed: number
  [key: string]: unknown
}

// ============================================================================
// Get Step Analytics
// ============================================================================

/**
 * Get step analytics parameters
 */
export interface GetStepAnalyticsParams {
  campaign_id: string
  start_date?: string
  end_date?: string
}

/**
 * Get step analytics result
 */
export interface GetStepAnalyticsResult {
  steps: Array<{
    step: string | null
    variant: string | null
    sent: number
    opened: number
    unique_opened: number
    replies: number
    unique_replies: number
  }>
}

// ============================================================================
// Bulk Add Leads
// ============================================================================

/**
 * Bulk add leads parameters
 */
export interface BulkAddLeadsParams {
  campaign_id: string
  leads: Array<{
    email: string
    first_name?: string
    last_name?: string
    company_name?: string
    website?: string
    custom_variables?: Record<string, string>
  }>
}

/**
 * Bulk add leads result
 */
export interface BulkAddLeadsResult {
  success: boolean
  added_count: number
  failed_count: number
  errors?: Array<{ email: string; error: string }>
}

// ============================================================================
// Get Account Health
// ============================================================================

/**
 * Get account health parameters
 */
export interface GetAccountHealthParams {
  limit?: number
  starting_after?: string
}

/**
 * Get account health result
 */
export interface GetAccountHealthResult {
  accounts: Array<{
    id: string
    email: string
    status: string
    warmup_enabled: boolean
    health_score?: number
    [key: string]: unknown
  }>
  next_starting_after?: string
}

// ============================================================================
// Create Inbox Test
// ============================================================================

/**
 * Create inbox test parameters
 */
export interface CreateInboxTestParams {
  email_account: string
  subject?: string
  body?: string
}

/**
 * Create inbox test result
 */
export interface CreateInboxTestResult {
  success: boolean
  test_id: string
  status: string
}

// ============================================================================
// Create Campaign
// ============================================================================

/**
 * Create campaign parameters
 */
export interface CreateCampaignParams {
  name: string
  sequences?: Array<{
    steps: Array<{
      type?: string
      delay?: number
      variants: Array<{
        subject: string
        body: string
        v_disabled?: boolean
      }>
    }>
  }>
  email_list?: string[]
  campaign_schedule?: {
    schedules: Array<{
      name: string
      timing: { from: string; to: string }
      days: Record<string, boolean>
      timezone: string
    }>
  }
  daily_limit?: number
  daily_max_leads?: number
  stop_on_reply?: boolean
  stop_on_auto_reply?: boolean
  stop_for_company?: boolean
  track_opens?: boolean
  track_clicks?: boolean
  text_only?: boolean
  first_email_text_only?: boolean
}

/**
 * Create campaign result
 */
export interface CreateCampaignResult {
  success: boolean
  campaign_id: string
  name: string
}

// ============================================================================
// Get Daily Campaign Analytics
// ============================================================================

/**
 * Get daily campaign analytics parameters
 */
export interface GetDailyCampaignAnalyticsParams {
  campaign_id?: string
  start_date?: string
  end_date?: string
}

/**
 * Get daily campaign analytics result
 */
export interface GetDailyCampaignAnalyticsResult {
  data: Array<{
    date: string
    sent: number
    opened: number
    unique_opened: number
    replied: number
    unique_replied: number
    bounced: number
    clicks: number
    unique_clicks: number
  }>
}

// ============================================================================
// List Leads
// ============================================================================

/**
 * List leads parameters
 */
export interface ListLeadsParams {
  campaign_id?: string
  email?: string
  limit?: number
  starting_after?: string
}

/**
 * List leads result
 */
export interface ListLeadsResult {
  items: Array<{
    id: string
    email: string
    first_name?: string
    last_name?: string
    company_name?: string
    status: string
    campaign_id?: string
    timestamp_created?: string
  }>
  next_starting_after?: string
}

// ============================================================================
// Bulk Delete Leads
// ============================================================================

/**
 * Bulk delete leads parameters
 */
export interface BulkDeleteLeadsParams {
  ids?: string[]
  campaign_id?: string
  list_id?: string
  limit?: number
}

/**
 * Bulk delete leads result
 */
export interface BulkDeleteLeadsResult {
  deleted_count: number
}

// ============================================================================
// DELETE CAMPAIGN
// ============================================================================

/**
 * Delete campaign parameters
 */
export interface DeleteCampaignParams {
  campaign_id: string
}

/**
 * Delete campaign result
 */
export interface DeleteCampaignResult {
  success: boolean
  campaign_id: string
}

// ============================================================================
// PATCH LEAD
// ============================================================================

/**
 * Patch lead parameters
 */
export interface PatchLeadParams {
  lead_id: string
  custom_variables: Record<string, string>
}

/**
 * Patch lead result
 */
export interface PatchLeadResult {
  id: string
}
