/**
 * Intercom API Types
 * Based on Intercom API v2.11
 */

// Branded types for type safety
export type ContactId = string & { readonly __brand: 'ContactId' };
export type CompanyId = string & { readonly __brand: 'CompanyId' };
export type ConversationId = string & { readonly __brand: 'ConversationId' };
export type AdminId = string & { readonly __brand: 'AdminId' };
export type TeamId = string & { readonly __brand: 'TeamId' };
export type ArticleId = string & { readonly __brand: 'ArticleId' };
export type CollectionId = string & { readonly __brand: 'CollectionId' };
export type SectionId = string & { readonly __brand: 'SectionId' };
export type TicketId = string & { readonly __brand: 'TicketId' };
export type TicketTypeId = string & { readonly __brand: 'TicketTypeId' };
export type TagId = string & { readonly __brand: 'TagId' };
export type SegmentId = string & { readonly __brand: 'SegmentId' };
export type NoteId = string & { readonly __brand: 'NoteId' };

// Common types
export interface Timestamp {
  created_at: number;
  updated_at: number;
}

export interface Pagination {
  type: 'pages';
  page: number;
  per_page: number;
  total_pages: number;
}

export interface CursorPagination {
  type: 'pages';
  next?: {
    page: number;
    starting_after: string;
  };
  per_page: number;
  total_pages?: number;
}

export interface ListResponse<T> {
  type: 'list';
  data: T[];
  pages?: CursorPagination;
  total_count?: number;
}

export interface ScrollResponse<T> {
  type: 'list';
  data: T[];
  scroll_param?: string;
  pages?: {
    type: 'pages';
    next?: string;
  };
}

// Search/Filter types
export type SearchOperator = 
  | '=' | '!=' | 'IN' | 'NIN' 
  | '>' | '<' | '>=' | '<='
  | '~' | '!~' | '^' | '$';

export interface SingleFilter {
  field: string;
  operator: SearchOperator;
  value: string | number | boolean | string[];
}

export interface MultipleFilter {
  operator: 'AND' | 'OR';
  value: Array<SingleFilter | MultipleFilter>;
}

export type SearchFilter = SingleFilter | MultipleFilter;

export interface SearchQuery {
  query?: SearchFilter;
  pagination?: {
    per_page?: number;
    starting_after?: string;
  };
  sort?: {
    field: string;
    order: 'asc' | 'desc';
  };
}

// Contact types
export interface ContactLocation {
  type: 'location';
  country?: string;
  region?: string;
  city?: string;
}

export interface ContactSocialProfile {
  type: 'social_profile';
  name: string;
  url: string;
}

export interface ContactAvatar {
  type: 'avatar';
  image_url?: string;
}

export interface ContactCompany {
  type: 'company';
  id: CompanyId;
  name?: string;
  url?: string;
}

export interface CustomAttributes {
  [key: string]: string | number | boolean | null;
}

export interface Contact extends Timestamp {
  type: 'contact';
  id: ContactId;
  workspace_id: string;
  external_id?: string;
  role: 'user' | 'lead';
  email?: string;
  phone?: string;
  name?: string;
  avatar?: ContactAvatar;
  owner_id?: AdminId;
  social_profiles?: {
    type: 'list';
    data: ContactSocialProfile[];
  };
  has_hard_bounced: boolean;
  marked_email_as_spam: boolean;
  unsubscribed_from_emails: boolean;
  location?: ContactLocation;
  last_seen_at?: number;
  last_replied_at?: number;
  last_contacted_at?: number;
  last_email_opened_at?: number;
  last_email_clicked_at?: number;
  language_override?: string;
  browser?: string;
  browser_version?: string;
  browser_language?: string;
  os?: string;
  android_app_name?: string;
  android_app_version?: string;
  android_device?: string;
  android_os_version?: string;
  android_sdk_version?: string;
  android_last_seen_at?: number;
  ios_app_name?: string;
  ios_app_version?: string;
  ios_device?: string;
  ios_os_version?: string;
  ios_sdk_version?: string;
  ios_last_seen_at?: number;
  custom_attributes?: CustomAttributes;
  tags?: {
    type: 'list';
    data: Tag[];
    url: string;
    total_count: number;
    has_more: boolean;
  };
  notes?: {
    type: 'list';
    data: Note[];
    url: string;
    total_count: number;
    has_more: boolean;
  };
  companies?: {
    type: 'list';
    data: ContactCompany[];
    url: string;
    total_count: number;
    has_more: boolean;
  };
}

// Company types
export interface Company extends Timestamp {
  type: 'company';
  id: CompanyId;
  name: string;
  company_id?: string;
  remote_created_at?: number;
  plan?: string;
  size?: number;
  website?: string;
  industry?: string;
  monthly_spend?: number;
  session_count?: number;
  user_count?: number;
  custom_attributes?: CustomAttributes;
  tags?: {
    type: 'list';
    data: Tag[];
    url: string;
    total_count: number;
    has_more: boolean;
  };
  segments?: {
    type: 'list';
    data: Segment[];
    url: string;
    total_count: number;
    has_more: boolean;
  };
}

// Conversation types
export interface ConversationSource {
  type: 'conversation';
  id: ConversationId;
  delivered_as: 'operator_initiated' | 'automated' | 'admin_initiated';
  subject?: string;
  body?: string;
  author: {
    type: 'admin' | 'user' | 'lead' | 'bot';
    id: string;
    name?: string;
    email?: string;
  };
  attachments?: Array<{
    type: 'upload';
    name: string;
    url: string;
    content_type: string;
    filesize: number;
    width?: number;
    height?: number;
  }>;
  url?: string;
}

export interface ConversationPart extends Timestamp {
  type: 'conversation_part';
  id: string;
  part_type: 'comment' | 'note' | 'assignment' | 'open' | 'close' | 'snooze';
  body?: string;
  author: {
    type: 'admin' | 'user' | 'lead' | 'bot';
    id: string;
    name?: string;
    email?: string;
  };
  attachments?: Array<{
    type: 'upload';
    name: string;
    url: string;
    content_type: string;
    filesize: number;
  }>;
  notified_at?: number;
  assigned_to?: {
    type: 'admin' | 'team';
    id: string;
  };
  external_id?: string;
  redacted: boolean;
}

export interface ConversationStatistics {
  type: 'conversation_statistics';
  time_to_assignment?: number;
  time_to_admin_reply?: number;
  time_to_first_close?: number;
  time_to_last_close?: number;
  median_time_to_reply?: number;
  first_contact_reply_at?: number;
  first_assignment_at?: number;
  first_admin_reply_at?: number;
  first_close_at?: number;
  last_assignment_at?: number;
  last_assignment_admin_reply_at?: number;
  last_contact_reply_at?: number;
  last_admin_reply_at?: number;
  last_close_at?: number;
  last_closed_by_id?: string;
  count_reopens?: number;
  count_assignments?: number;
  count_conversation_parts?: number;
}

export interface Conversation extends Timestamp {
  type: 'conversation';
  id: ConversationId;
  title?: string;
  state: 'open' | 'closed' | 'snoozed';
  read: boolean;
  priority: 'priority' | 'not_priority';
  waiting_since?: number;
  snoozed_until?: number;
  open: boolean;
  source: ConversationSource;
  contacts?: {
    type: 'contact.list';
    contacts: Contact[];
  };
  teammates?: {
    type: 'admin.list';
    admins: Admin[];
  };
  assignee?: {
    type: 'admin' | 'team' | 'nobody';
    id?: string;
  };
  conversation_parts?: {
    type: 'conversation_part.list';
    conversation_parts: ConversationPart[];
    total_count: number;
  };
  conversation_rating?: {
    rating?: number;
    remark?: string;
    created_at?: number;
    contact?: {
      type: 'contact';
      id: ContactId;
    };
    teammate?: {
      type: 'admin';
      id: AdminId;
    };
  };
  statistics?: ConversationStatistics;
  tags?: {
    type: 'tag.list';
    tags: Tag[];
  };
  linked_objects?: {
    type: 'list';
    data: Array<{
      id: string;
      category?: string;
    }>;
    total_count: number;
    has_more: boolean;
  };
}

// Article & Help Center types
export interface Article extends Timestamp {
  type: 'article';
  id: ArticleId;
  workspace_id: string;
  title: string;
  description?: string;
  body?: string;
  author_id: AdminId;
  state: 'published' | 'draft';
  parent_id?: CollectionId | SectionId;
  parent_type?: 'collection' | 'section';
  default_locale: string;
  url?: string;
  statistics?: {
    type: 'article_statistics';
    views: number;
    conversions: number;
    reactions: number;
    happy_reaction_count: number;
    neutral_reaction_count: number;
    sad_reaction_count: number;
  };
}

export interface Collection extends Timestamp {
  type: 'collection';
  id: CollectionId;
  workspace_id: string;
  name: string;
  description?: string;
  url?: string;
  order?: number;
  default_locale: string;
  icon?: string;
  parent_id?: string;
  help_center_id?: number;
}

export interface Section extends Timestamp {
  type: 'section';
  id: SectionId;
  workspace_id: string;
  name: string;
  parent_id: CollectionId;
  url?: string;
  order?: number;
}

export interface HelpCenter extends Timestamp {
  type: 'help_center';
  id: string;
  workspace_id: string;
  identifier: string;
  website_turned_on: boolean;
  display_name?: string;
  website_url?: string;
}

// Ticket types
export interface TicketTypeAttribute {
  type: 'ticket_type_attribute';
  id: string;
  workspace_id: string;
  name: string;
  description?: string;
  data_type: 'string' | 'integer' | 'decimal' | 'boolean' | 'date' | 'datetime' | 'files';
  input_options?: {
    multiline?: boolean;
    list?: string[];
  };
  order: number;
  required_to_create: boolean;
  required_to_create_for_contacts: boolean;
  visible_on_create: boolean;
  visible_to_contacts: boolean;
  default: boolean;
  ticket_type_id: TicketTypeId;
  archived: boolean;
  created_at: number;
  updated_at: number;
}

export interface TicketType extends Timestamp {
  type: 'ticket_type';
  id: TicketTypeId;
  workspace_id: string;
  name: string;
  description?: string;
  icon?: string;
  archived: boolean;
  ticket_type_attributes?: {
    type: 'list';
    ticket_type_attributes: TicketTypeAttribute[];
  };
}

export interface TicketState {
  type: 'ticket_state';
  id: string;
  name: string;
  category: 'submitted' | 'in_progress' | 'waiting_on_customer' | 'resolved';
}

export interface Ticket extends Timestamp {
  type: 'ticket';
  id: TicketId;
  ticket_type_id: TicketTypeId;
  contacts?: {
    type: 'contact.list';
    contacts: Contact[];
  };
  admin_assignee_id?: AdminId;
  team_assignee_id?: TeamId;
  ticket_state: TicketState;
  ticket_attributes?: {
    [key: string]: string | number | boolean | string[] | null;
  };
  linked_objects?: {
    type: 'list';
    data: Array<{
      id: string;
      category?: string;
    }>;
    total_count: number;
  };
  is_shared: boolean;
  category?: 'ticket' | 'back_office_ticket' | 'tracker';
}

// Tag types
export interface Tag {
  type: 'tag';
  id: TagId;
  name: string;
  applied_at?: number;
  applied_by?: {
    type: 'admin';
    id: AdminId;
  };
}

// Segment types
export interface Segment {
  type: 'segment';
  id: SegmentId;
  name: string;
  created_at: number;
  updated_at: number;
  person_type: 'contact' | 'user' | 'lead';
  count?: number;
}

// Event types
export interface DataEvent {
  type: 'event';
  id: string;
  event_name: string;
  created_at: number;
  user_id?: string;
  email?: string;
  metadata?: {
    [key: string]: string | number | boolean;
  };
}

export interface Event {
  event_name: string;
  created_at?: number;
  user_id?: string;
  id?: string;
  email?: string;
  metadata?: {
    [key: string]: string | number | boolean | null;
  };
}

// Message types
export type MessageType = 'inapp' | 'email' | 'push';

export interface MessageContent {
  type: 'text' | 'button';
  text?: string;
  style?: 'primary' | 'secondary' | 'link';
  action?: {
    type: 'url' | 'sheet';
    url?: string;
    payload?: string;
  };
}

export interface Message {
  message_type: MessageType;
  subject?: string;
  body?: string;
  template?: 'plain' | 'personal';
  from?: {
    type: 'admin';
    id: AdminId;
  };
  to?: {
    type: 'contact' | 'user' | 'lead';
    id?: ContactId;
    user_id?: string;
    email?: string;
  };
  create_conversation_without_contact_reply?: boolean;
}

// Admin & Team types
export interface Admin {
  type: 'admin';
  id: AdminId;
  name: string;
  email: string;
  email_verified: boolean;
  app?: {
    type: 'app';
    id_code: string;
  };
  avatar?: {
    type: 'avatar';
    image_url?: string;
  };
  away_mode_enabled: boolean;
  away_mode_reassign: boolean;
  has_inbox_seat: boolean;
  team_ids?: TeamId[];
  team_priority_level?: {
    [key: string]: number;
  };
}

export interface Team {
  type: 'team';
  id: TeamId;
  name: string;
  admin_ids?: AdminId[];
  admin_priority_level?: {
    [key: string]: number;
  };
}

// Note types
export interface Note extends Timestamp {
  type: 'note';
  id: NoteId;
  contact?: {
    type: 'contact';
    id: ContactId;
  };
  author?: {
    type: 'admin';
    id: AdminId;
    name?: string;
    email?: string;
  };
  body?: string;
}

// Data Attribute types
export interface DataAttribute {
  type: 'data_attribute';
  id: string;
  workspace_id: string;
  name: string;
  full_name: string;
  label: string;
  description?: string;
  data_type: 
    | 'string' 
    | 'integer' 
    | 'float' 
    | 'boolean' 
    | 'date' 
    | 'datetime'
    | 'object'
    | 'array';
  options?: string[];
  api_writable: boolean;
  ui_writable: boolean;
  custom: boolean;
  archived: boolean;
  created_at: number;
  updated_at: number;
  model: 'contact' | 'company' | 'conversation' | 'ticket';
  admin_id?: AdminId;
  messenger_writable?: boolean;
}

// Subscription types
export interface Subscription {
  type: 'subscription';
  id: string;
  state: 'active' | 'past_due' | 'canceled' | 'trialing';
  default_translation?: {
    name: string;
    description?: string;
  };
  consent_type: 'opt_in' | 'opt_out';
  content_types?: string[];
  created_at: number;
  updated_at: number;
}

// SLA types
export interface SLA {
  type: 'sla';
  sla_name: string;
  sla_status: 'active' | 'missed' | 'cancelled';
}

// Request/Response helpers
export interface CreateContactRequest {
  role?: 'user' | 'lead';
  external_id?: string;
  email?: string;
  phone?: string;
  name?: string;
  avatar?: string;
  signed_up_at?: number;
  last_seen_at?: number;
  owner_id?: number;
  unsubscribed_from_emails?: boolean;
  custom_attributes?: CustomAttributes;
}

export interface UpdateContactRequest {
  role?: 'user' | 'lead';
  external_id?: string;
  email?: string;
  phone?: string;
  name?: string;
  avatar?: string;
  signed_up_at?: number;
  last_seen_at?: number;
  owner_id?: number;
  unsubscribed_from_emails?: boolean;
  custom_attributes?: CustomAttributes;
}

export interface CreateCompanyRequest {
  company_id?: string;
  name: string;
  website?: string;
  plan?: string;
  size?: number;
  industry?: string;
  remote_created_at?: number;
  monthly_spend?: number;
  custom_attributes?: CustomAttributes;
}

export interface CreateConversationRequest {
  from: {
    type: 'user' | 'lead' | 'contact';
    id?: string;
    user_id?: string;
    email?: string;
  };
  body: string;
}

export interface ReplyConversationRequest {
  message_type: 'comment' | 'note';
  type: 'admin' | 'user';
  admin_id?: AdminId;
  body: string;
  attachment_urls?: string[];
  created_at?: number;
}

export interface CreateTicketRequest {
  ticket_type_id: TicketTypeId;
  contacts?: Array<{
    id?: ContactId;
    external_id?: string;
    email?: string;
  }>;
  ticket_attributes?: {
    [key: string]: string | number | boolean | string[];
  };
}

export interface CreateNoteRequest {
  contact_id?: ContactId;
  admin_id?: AdminId;
  body: string;
}
