// Salesforce CRM Data Types for Integration

export interface SalesforceEvent {
  Id: string;
  Subject: string;
  StartDateTime: string | null;
  EndDateTime: string | null;
  Location: string;
  Description: string | null;
  OwnerId: string;
  WhatId: string | null;
  WhoId: string | null;
  IsAllDayEvent: boolean;
  CreatedDate: string;
  attributes: {
    type: string;
    url: string;
  };
}

export interface SalesforceLead {
  Id: string;
  FirstName: string;
  LastName: string;
  Company: string;
  Email: string;
  Status: LeadStatus;
  Phone?: string;
  Title?: string;
  Street?: string;
  City?: string;
  State?: string;
  Country?: string;
  PostalCode?: string;
  Website?: string;
  Industry?: string;
  NumberOfEmployees?: number;
  AnnualRevenue?: number;
  OwnerId?: string;
  LeadSource?: string;
  Description?: string;
  CreatedDate?: string;
  LastModifiedDate?: string;
  attributes: {
    type: string;
    url: string;
  };
}

export interface SalesforceTask {
  Id: string;
  Subject: string;
  Status: TaskStatus;
  Priority: TaskPriority;
  ActivityDate: string | null;
  WhatId: string | null;
  WhoId: string | null;
  OwnerId: string;
  Description: string | null;
  CreatedDate: string;
  LastModifiedDate: string;
  IsClosed?: boolean;
  IsHighPriority?: boolean;
  attributes: {
    type: string;
    url: string;
  };
}

export interface SalesforceAccount {
  Id: string;
  Name: string;
  Industry: string | null;
  Type: string | null;
  Phone: string | null;
  BillingCity: string | null;
  BillingState: string | null;
  BillingCountry: string | null;
  BillingStreet?: string | null;
  BillingPostalCode?: string | null;
  ShippingCity?: string | null;
  ShippingState?: string | null;
  ShippingCountry?: string | null;
  ShippingStreet?: string | null;
  ShippingPostalCode?: string | null;
  Website: string | null;
  OwnerId: string;
  CreatedDate: string;
  LastModifiedDate?: string;
  Description?: string | null;
  NumberOfEmployees?: number;
  AnnualRevenue?: number;
  attributes: {
    type: string;
    url: string;
  };
}

export interface SalesforceOpportunity {
  Id: string;
  Name: string;
  StageName: OpportunityStage;
  Amount: number;
  CloseDate: string;
  AccountId: string | null;
  Type: string | null;
  Probability: number;
  ForecastCategory: string;
  OwnerId: string;
  CreatedDate: string;
  LastModifiedDate: string;
  Description?: string | null;
  LeadSource?: string | null;
  NextStep?: string | null;
  IsClosed?: boolean;
  IsWon?: boolean;
  ExpectedRevenue?: number;
  attributes: {
    type: string;
    url: string;
  };
}

// Enums and Union Types
export type LeadStatus = 'New' | 'Working' | 'Nurturing' | 'Qualified' | 'Unqualified';
export type TaskStatus = 'Open' | 'In Progress' | 'Completed' | 'Waiting' | 'Deferred';
export type TaskPriority = 'High' | 'Normal' | 'Low';
export type OpportunityStage = 
  | 'Prospecting' 
  | 'Qualification' 
  | 'Needs Analysis' 
  | 'Value Proposition' 
  | 'Identify Decision Makers'
  | 'Perception Analysis'
  | 'Proposal/Price Quote'
  | 'Negotiation/Review'
  | 'Closed Won'
  | 'Closed Lost';

// Combined Salesforce CRM Data Interface
export interface SalesforceCrmData {
  events: SalesforceEvent[];
  leads: SalesforceLead[];
  tasks: SalesforceTask[];
  accounts: SalesforceAccount[];
  opportunities: SalesforceOpportunity[];
}

// API Response Types
export interface SalesforceApiResponse<T> {
  status: 'success' | 'error';
  message: string;
  data?: {
    success: boolean;
    data: T[];
    count: number;
    metadata: {
      totalSize: number;
      done: boolean;
      nextRecordsUrl: string | null;
    };
  };
  error?: string;
  timestamp: string;
}

export interface SalesforcePaginatedResponse<T> {
  data: T[];
  info: {
    count: number;
    moreRecords: boolean;
    page: number;
  };
}

// Filter and Search Types
export interface SalesforceFilters {
  events?: {
    dateRange?: {
      start: string;
      end: string;
    };
  };
  leads?: {
    status?: LeadStatus[];
    source?: string[];
    assignedTo?: string[];
    dateRange?: {
      start: string;
      end: string;
    };
  };
  tasks?: {
    status?: TaskStatus[];
    priority?: TaskPriority[];
    assignedTo?: string[];
    dueDateRange?: {
      start: string;
      end: string;
    };
  };
  accounts?: {
    industry?: string[];
    type?: string[];
    assignedTo?: string[];
  };
  opportunities?: {
    stage?: OpportunityStage[];
    owner?: string[];
    amountRange?: {
      min: number;
      max: number;
    };
    closeDateRange?: {
      start: string;
      end: string;
    };
  };
}

export interface SalesforceSearchParams {
  query?: string;
  type?: 'events' | 'leads' | 'tasks' | 'accounts' | 'opportunities' | 'all';
  filters?: SalesforceFilters;
  sortBy?: string;
  sortOrder?: 'asc' | 'desc';
  page?: number;
  limit?: number;
}

// Statistics and Analytics Types
export interface SalesforceStats {
  events: {
    total: number;
    upcoming: number;
    completed: number;
    byLocation: Record<string, number>;
  };
  leads: {
    total: number;
    new: number;
    working: number;
    qualified: number;
    byStatus: Record<LeadStatus, number>;
    bySource: Record<string, number>;
  };
  tasks: {
    total: number;
    open: number;
    completed: number;
    overdue: number;
    byStatus: Record<TaskStatus, number>;
    byPriority: Record<TaskPriority, number>;
  };
  accounts: {
    total: number;
    byIndustry: Record<string, number>;
    byType: Record<string, number>;
    totalRevenue: number;
  };
  opportunities: {
    total: number;
    open: number;
    won: number;
    lost: number;
    totalValue: number;
    averageDealSize: number;
    byStage: Record<OpportunityStage, number>;
    pipelineValue: number;
    winRate: number;
  };
}

