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

export interface ToolDefinition {
  name: string;
  description: string;
  inputSchema: {
    type: "object";
    properties?: Record<string, unknown>;
    required?: string[];
  };
}

export interface ToolResult {
  content: Array<{ type: "text"; text: string } | { type: "image"; data: string; mimeType: string }>;
  isError?: boolean;
  structuredContent?: {
    type: "html" | "markdown";
    html?: string;
    markdown?: string;
  };
}

// ============================================================================
// Pipedrive Entity Types
// ============================================================================

export interface Deal {
  id: number;
  title: string;
  value: number;
  currency: string;
  status: string;
  stage_id: number;
  pipeline_id: number;
  person_id?: number;
  org_id?: number;
  user_id?: number;
  won_time?: string;
  lost_time?: string;
  close_time?: string;
  lost_reason?: string;
  expected_close_date?: string;
  visible_to?: string;
  add_time?: string;
  update_time?: string;
  stage_change_time?: string;
  active?: boolean;
  deleted?: boolean;
  probability?: number;
  next_activity_date?: string;
  next_activity_time?: string;
  next_activity_id?: number;
  last_activity_id?: number;
  last_activity_date?: string;
  label?: string;
  org_name?: string;
  person_name?: string;
  cc_email?: string;
  [key: string]: unknown;
}

export interface Person {
  id: number;
  name: string;
  email?: Array<{ value: string; primary: boolean; label?: string }>;
  phone?: Array<{ value: string; primary: boolean; label?: string }>;
  org_id?: number;
  owner_id?: number;
  add_time?: string;
  update_time?: string;
  visible_to?: string;
  active_flag?: boolean;
  label?: number;
  first_name?: string;
  last_name?: string;
  org_name?: string;
  [key: string]: unknown;
}

export interface Organization {
  id: number;
  name: string;
  owner_id?: number;
  address?: string;
  cc_email?: string;
  add_time?: string;
  update_time?: string;
  visible_to?: string;
  active_flag?: boolean;
  label?: number;
  [key: string]: unknown;
}

export interface Activity {
  id: number;
  subject: string;
  type: string;
  done: boolean;
  due_date?: string;
  due_time?: string;
  duration?: string;
  deal_id?: number;
  person_id?: number;
  org_id?: number;
  user_id?: number;
  note?: string;
  add_time?: string;
  update_time?: string;
  marked_as_done_time?: string;
  active_flag?: boolean;
  [key: string]: unknown;
}

export interface Pipeline {
  id: number;
  name: string;
  order_nr: number;
  active: boolean;
  deal_probability: boolean;
  add_time?: string;
  update_time?: string;
  selected?: boolean;
  [key: string]: unknown;
}

export interface Stage {
  id: number;
  name: string;
  pipeline_id: number;
  order_nr: number;
  deal_probability: number;
  rotten_flag?: boolean;
  rotten_days?: number;
  add_time?: string;
  update_time?: string;
  active_flag?: boolean;
  [key: string]: unknown;
}

export interface Product {
  id: number;
  name: string;
  code?: string;
  unit?: string;
  tax?: number;
  active_flag?: boolean;
  visible_to?: string;
  owner_id?: number;
  add_time?: string;
  update_time?: string;
  prices?: Array<{
    id: number;
    price: number;
    currency: string;
    cost?: number;
    overhead_cost?: number;
  }>;
  [key: string]: unknown;
}

export interface Lead {
  id: string;
  title: string;
  owner_id?: number;
  person_id?: number;
  organization_id?: number;
  value?: {
    amount: number;
    currency: string;
  };
  expected_close_date?: string;
  visible_to?: string;
  was_seen?: boolean;
  add_time?: string;
  update_time?: string;
  label_ids?: string[];
  [key: string]: unknown;
}

export interface Note {
  id: number;
  content: string;
  deal_id?: number;
  person_id?: number;
  org_id?: number;
  lead_id?: string;
  user_id?: number;
  add_time?: string;
  update_time?: string;
  active_flag?: boolean;
  pinned_to_deal_flag?: boolean;
  pinned_to_person_flag?: boolean;
  pinned_to_organization_flag?: boolean;
  pinned_to_lead_flag?: boolean;
  [key: string]: unknown;
}

export interface File {
  id: number;
  name: string;
  file_type: string;
  file_size: number;
  url?: string;
  deal_id?: number;
  person_id?: number;
  org_id?: number;
  product_id?: number;
  activity_id?: number;
  add_time?: string;
  update_time?: string;
  active_flag?: boolean;
  [key: string]: unknown;
}

export interface Filter {
  id: number;
  name: string;
  type: string;
  active_flag?: boolean;
  temporary_flag?: boolean;
  user_id?: number;
  add_time?: string;
  update_time?: string;
  visible_to?: string;
  [key: string]: unknown;
}

export interface Goal {
  id: string;
  owner_id?: number;
  title: string;
  type: {
    name: string;
    params?: Record<string, unknown>;
  };
  assignee?: {
    id: number;
    type: string;
  };
  interval: string;
  duration?: {
    start: string;
    end: string;
  };
  expected_outcome?: {
    target: number;
    tracking_metric: string;
  };
  is_active?: boolean;
  [key: string]: unknown;
}

export interface Webhook {
  id: number;
  company_id: number;
  owner_id: number;
  user_id: number;
  event_action: string;
  event_object: string;
  subscription_url: string;
  is_active: number;
  add_time?: string;
  remove_time?: string;
  type?: string;
  http_auth_user?: string;
  http_auth_password?: string;
  [key: string]: unknown;
}

export interface User {
  id: number;
  name: string;
  email: string;
  active_flag: boolean;
  is_admin?: number;
  created?: string;
  modified?: string;
  role_id?: number;
  has_created_company?: boolean;
  access?: Array<{
    app?: string;
    admin?: boolean;
    permission_set_id?: string;
  }>;
  [key: string]: unknown;
}

export interface MailThread {
  id: number;
  account_id: string;
  user_id: number;
  subject: string;
  snippet?: string;
  read_flag?: number;
  mail_tracking_status?: string;
  has_attachments_flag?: number;
  has_inline_attachments_flag?: number;
  has_real_attachments_flag?: number;
  deleted_flag?: number;
  synced_flag?: number;
  smart_bcc_flag?: number;
  mail_link_tracking_enabled_flag?: number;
  write_flag?: number;
  draft?: string;
  draft_id?: string;
  parties?: unknown;
  last_message_timestamp?: string;
  first_message_to_now_in_minutes?: number;
  last_message_sent_or_received_timestamp?: string;
  add_time?: string;
  update_time?: string;
  deal_id?: number;
  deal_status?: string;
  lead_id?: string;
  all_messages_sent_flag?: number;
  [key: string]: unknown;
}

export interface MailMessage {
  id: number;
  from: Array<{ email_address: string; name?: string; linked_person_id?: number; linked_person_name?: string }>;
  to: Array<{ email_address: string; name?: string; linked_person_id?: number; linked_person_name?: string }>;
  cc?: Array<{ email_address: string; name?: string }>;
  bcc?: Array<{ email_address: string; name?: string }>;
  body_url: string;
  subject: string;
  snippet?: string;
  mail_thread_id: number;
  account_id: string;
  user_id: number;
  timestamp?: string;
  has_attachments_flag?: number;
  sent_flag?: number;
  sent_from_pipedrive_flag?: number;
  smart_bcc_flag?: number;
  message_time?: string;
  add_time?: string;
  update_time?: string;
  [key: string]: unknown;
}

export interface Subscription {
  id: number;
  user_id: number;
  deal_id: number;
  is_active: boolean;
  cycles_count?: number;
  cycle_amount?: number;
  infinite?: boolean;
  currency: string;
  cadence_type: string;
  start_date: string;
  end_date?: string;
  lifetime_value?: number;
  final_status?: string;
  add_time?: string;
  update_time?: string;
  [key: string]: unknown;
}

export interface ActivityType {
  id: number;
  name: string;
  icon_key: string;
  active_flag: boolean;
  color?: string;
  order_nr?: number;
  key_string?: string;
  add_time?: string;
  update_time?: string;
  [key: string]: unknown;
}
