// CRM Data Types for Zoho CRM Integration

export interface CrmLead {
  id: string;
  Owner: {
    name: string;
    id: string;
    email: string;
  };
  Company: string;
  Email: string;
  Phone: string;
  Mobile: string;
  Lead_Status: LeadStatus;
  Lead_Source: LeadSource;
  Created_Time: string;
  Last_Activity_Time: string;
  Annual_Revenue?: number;
  Description?: string;
  Industry?: string;
  Street?: string;
  City?: string;
  State?: string;
  Country?: string;
  Zip_Code?: string;
  Website?: string;
  First_Name: string;
  Last_Name: string;
  Full_Name: string;
  Salutation?: string;
  Designation?: string;
  Twitter?: string;
  Skype_ID?: string;
  Secondary_Email?: string;
  Fax?: string;
  No_of_Employees?: number;
  Tag: string[];
}

export interface CrmTask {
  id: string;
  Owner: {
    name: string;
    id: string;
    email: string;
  };
  Subject: string;
  Description: string;
  Status: TaskStatus;
  Priority: TaskPriority;
  Due_Date: string;
  Created_Time: string;
  Closed_Time?: string;
  Who_Id?: {
    name: string;
    id: string;
  };
  What_Id?: {
    name: string;
    id: string;
  };
  Remind_At?: string;
  Send_Notification_Email: boolean;
  Recurring_Activity?: string;
  Tag: string[];
}

export interface CrmContact {
  id: string;
  Owner: {
    name: string;
    id: string;
    email: string;
  };
  First_Name: string;
  Last_Name: string;
  Full_Name: string;
  Email: string;
  Phone: string;
  Mobile: string;
  Home_Phone?: string;
  Other_Phone?: string;
  Account_Name?: {
    name: string;
    id: string;
  };
  Title: string;
  Department?: string;
  Lead_Source: LeadSource;
  Last_Activity_Time: string;
  Created_Time: string;
  Mailing_Street?: string;
  Mailing_City?: string;
  Mailing_State?: string;
  Mailing_Country?: string;
  Mailing_Zip?: string;
  Other_Street?: string;
  Other_City?: string;
  Other_State?: string;
  Other_Country?: string;
  Other_Zip?: string;
  Salutation?: string;
  Assistant?: string;
  Asst_Phone?: string;
  Reporting_To?: string;
  Date_of_Birth?: string;
  Secondary_Email?: string;
  Twitter?: string;
  Skype_ID?: string;
  Fax?: string;
  Description?: string;
  Tag: string[];
}

export interface CrmDeal {
  id: string;
  Owner: {
    name: string;
    id: string;
    email: string;
  };
  Deal_Name: string;
  Amount: number;
  Stage: DealStage;
  Probability: number;
  Closing_Date: string;
  Contact_Name?: {
    name: string;
    id: string;
  };
  Account_Name?: {
    name: string;
    id: string;
  };
  Description?: string;
  Lead_Source?: LeadSource;
  Type?: string;
  Next_Step?: string;
  Created_Time: string;
  Modified_Time: string;
  Expected_Revenue?: number;
  Overall_Sales_Duration?: number;
  Sales_Cycle_Duration?: number;
  Lead_Conversion_Time?: string;
  Reason_For_Loss__s?: string;
  Campaign_Source?: string;
  Tag: string[];
}

export interface CrmSalesOrder {
  id: string;
  Owner: {
    name: string;
    id: string;
    email: string;
  };
  $currency_symbol: string;
  Customer_No?: string;
  Tax: number;
  Last_Activity_Time?: string;
  $state: string;
  $converted: boolean;
  $process_flow: boolean;
  Deal_Name: {
    name: string;
    id: string;
  };
  Billing_Country: string;
  $locked_for_me: boolean;
  Carrier: string;
  $approved: boolean;
  Quote_Name?: string;
  Status: string;
  Grand_Total: number;
  $approval: {
    delegate: boolean;
    takeover: boolean;
    approve: boolean;
    reject: boolean;
    resubmit: boolean;
  };
  Adjustment: number;
  Billing_Street: string;
  Created_Time: string;
  $editable: boolean;
  Billing_Code: string;
  Product_Details: Array<{
    product: {
      Product_Code: string;
      name: string;
      id: string;
    };
    quantity: number;
    Discount: number;
    total_after_discount: number;
    net_total: number;
    book?: string;
    Tax: number;
    list_price: number;
    unit_price?: number;
    quantity_in_stock: number;
    total: number;
    id: string;
    product_description?: string;
    line_tax: any[];
  }>;
  Excise_Duty?: number;
  Shipping_City?: string;
  Shipping_Country?: string;
  Shipping_Code?: string;
  Billing_City: string;
  Purchase_Order?: string;
  Created_By: {
    name: string;
    id: string;
    email: string;
  };
  Shipping_Street?: string;
  Description?: string;
  Discount: number;
  Shipping_State?: string;
  $review_process: {
    approve: boolean;
    reject: boolean;
    resubmit: boolean;
  };
  $layout_id: {
    display_label: string;
    name: string;
    id: string;
  };
  Modified_By: {
    name: string;
    id: string;
    email: string;
  };
  $review?: any;
  Account_Name: {
    name: string;
    id: string;
  };
  Sales_Commission?: number;
  Modified_Time: string;
  Due_Date?: string;
  Terms_and_Conditions?: string;
  Sub_Total: number;
  Subject: string;
  $orchestration: boolean;
  Contact_Name: {
    name: string;
    id: string;
  };
  $in_merge: boolean;
  SO_Number: string;
  Locked__s: boolean;
  Billing_State: string;
  $line_tax: any[];
  Tag: string[];
  $approval_state: string;
  Pending?: any;
}

export interface CrmPurchaseOrder {
  id: string;
  Owner: {
    name: string;
    id: string;
    email: string;
  };
  $currency_symbol: string;
  Tax: number;
  Last_Activity_Time?: string;
  PO_Date: string;
  $state: string;
  $process_flow: boolean;
  Billing_Country: string;
  $locked_for_me: boolean;
  Carrier: string;
  $approved: boolean;
  Status: string;
  Grand_Total: number;
  $approval: {
    delegate: boolean;
    takeover: boolean;
    approve: boolean;
    reject: boolean;
    resubmit: boolean;
  };
  PO_Number?: string;
  Adjustment: number;
  Billing_Street: string;
  Created_Time: string;
  $editable: boolean;
  Billing_Code: string;
  Product_Details: Array<{
    product: {
      Product_Code: string;
      name: string;
      id: string;
    };
    quantity: number;
    Discount: number;
    total_after_discount: number;
    net_total: number;
    book?: string;
    Tax: number;
    list_price: number;
    unit_price: number;
    quantity_in_stock: number;
    total: number;
    id: string;
    product_description?: string;
    line_tax: any[];
  }>;
  Tracking_Number?: string;
  Excise_Duty?: number;
  Shipping_City?: string;
  Shipping_Country?: string;
  Shipping_Code?: string;
  Billing_City: string;
  Requisition_No?: string;
  Created_By: {
    name: string;
    id: string;
    email: string;
  };
  Shipping_Street?: string;
  Description?: string;
  Discount: number;
  Vendor_Name: {
    name: string;
    id: string;
  };
  Shipping_State?: string;
  $review_process: {
    approve: boolean;
    reject: boolean;
    resubmit: boolean;
  };
  $layout_id: {
    display_label: string;
    name: string;
    id: string;
  };
  Modified_By: {
    name: string;
    id: string;
    email: string;
  };
  $review?: any;
  Sales_Commission?: number;
  Modified_Time: string;
  Due_Date?: string;
  Terms_and_Conditions?: string;
  Sub_Total: number;
  Subject: string;
  $orchestration: boolean;
  Contact_Name?: {
    name: string;
    id: string;
  };
  $in_merge: boolean;
  Locked__s: boolean;
  Billing_State: string;
  $line_tax: any[];
  Tag: string[];
  $approval_state: string;
}

export interface CrmInvoice {
  id: string;
  Owner: {
    name: string;
    id: string;
    email: string;
  };
  $currency_symbol: string;
  Tax: number;
  Last_Activity_Time?: string;
  $state: string;
  $process_flow: boolean;
  Billing_Country: string;
  $locked_for_me: boolean;
  $approved: boolean;
  Status: string;
  Grand_Total: number;
  $approval: {
    delegate: boolean;
    takeover: boolean;
    approve: boolean;
    reject: boolean;
    resubmit: boolean;
  };
  Adjustment: number;
  Billing_Street: string;
  Created_Time: string;
  $editable: boolean;
  Billing_Code: string;
  Product_Details: Array<{
    product: {
      Product_Code: string;
      name: string;
      id: string;
    };
    quantity: number;
    Discount: number;
    total_after_discount: number;
    net_total: number;
    book?: string;
    Tax: number;
    list_price: number;
    unit_price?: number;
    quantity_in_stock: number;
    total: number;
    id: string;
    product_description?: string;
    line_tax: any[];
  }>;
  Excise_Duty?: number;
  Shipping_City?: string;
  Shipping_Country?: string;
  Shipping_Code?: string;
  Billing_City: string;
  Purchase_Order?: string;
  Created_By: {
    name: string;
    id: string;
    email: string;
  };
  Shipping_Street?: string;
  Description?: string;
  Discount: number;
  Shipping_State?: string;
  $review_process: {
    approve: boolean;
    reject: boolean;
    resubmit: boolean;
  };
  $layout_id: {
    display_label: string;
    name: string;
    id: string;
  };
  Invoice_Date: string;
  Modified_By: {
    name: string;
    id: string;
    email: string;
  };
  $review?: any;
  Account_Name: {
    name: string;
    id: string;
  };
  Sales_Order: {
    name: string;
    id: string;
  };
  Deal_Name__s: {
    name: string;
    id: string;
  };
  Sales_Commission?: number;
  Modified_Time: string;
  Due_Date: string;
  Terms_and_Conditions?: string;
  Sub_Total: number;
  Invoice_Number: string;
  Subject: string;
  $orchestration: boolean;
  Contact_Name: {
    name: string;
    id: string;
  };
  $in_merge: boolean;
  Locked__s: boolean;
  Billing_State: string;
  $line_tax: any[];
  Tag: string[];
  $approval_state: string;
}

// Enums and Union Types
export type LeadStatus = 'New' | 'Contacted' | 'Qualified' | 'Unqualified' | 'Converted' | 'Lost Lead' | 'Not Contacted' | 'Junk Lead';
export type TaskType = 'Call' | 'Email' | 'Meeting' | 'Follow-up' | 'Demo' | 'Proposal' | 'Other';
export type TaskPriority = 'Highest' | 'High' | 'Medium' | 'Low' | 'Lowest';
export type TaskStatus = 'Not Started' | 'In Progress' | 'Completed' | 'Cancelled' | 'Deferred' | 'Waiting on someone else';
export type ContactStatus = 'Active' | 'Inactive' | 'Unsubscribed';
export type DealStage = 'Prospecting' | 'Qualification' | 'Identify Decision Makers' | 'Proposal' | 'Negotiation' | 'Closed Won' | 'Closed Lost';
export type LeadSource = 'Website' | 'Referral' | 'Event' | 'Cold Call' | 'Email Campaign' | 'Social Media' | 'Partner' | 'Trade Show' | 'Other';

// Combined CRM Data Interface
export interface CrmData {
  leads: CrmLead[];
  tasks: CrmTask[];
  contacts: CrmContact[];
  deals: CrmDeal[];
  salesOrders: CrmSalesOrder[];
  purchaseOrders: CrmPurchaseOrder[];
  invoices: CrmInvoice[];
}

// API Response Types
export interface CrmApiResponse<T> {
  status: 'success' | 'error';
  message: string;
  data?: T;
  error?: string;
  timestamp: string;
}

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

// Filter and Search Types
export interface CrmFilters {
  leads?: {
    status?: LeadStatus[];
    source?: LeadSource[];
    assignedTo?: string[];
    dateRange?: {
      start: string;
      end: string;
    };
  };
  tasks?: {
    status?: TaskStatus[];
    priority?: TaskPriority[];
    type?: TaskType[];
    assignedTo?: string[];
    dueDateRange?: {
      start: string;
      end: string;
    };
  };
  contacts?: {
    status?: ContactStatus[];
    company?: string[];
    assignedTo?: string[];
  };
  deals?: {
    stage?: DealStage[];
    owner?: string[];
    amountRange?: {
      min: number;
      max: number;
    };
    closeDateRange?: {
      start: string;
      end: string;
    };
  };
}

export interface CrmSearchParams {
  query?: string;
  type?: 'leads' | 'tasks' | 'contacts' | 'deals' | 'all';
  filters?: CrmFilters;
  sortBy?: string;
  sortOrder?: 'asc' | 'desc';
  page?: number;
  limit?: number;
}

// Statistics and Analytics Types
export interface CrmStats {
  leads: {
    total: number;
    new: number;
    qualified: number;
    converted: number;
    bySource: Record<LeadSource, number>;
    byStatus: Record<LeadStatus, number>;
  };
  tasks: {
    total: number;
    completed: number;
    overdue: number;
    byStatus: Record<TaskStatus, number>;
    byPriority: Record<TaskPriority, number>;
  };
  contacts: {
    total: number;
    active: number;
    inactive: number;
    byCompany: Record<string, number>;
  };
  deals: {
    total: number;
    won: number;
    lost: number;
    totalValue: number;
    averageDealSize: number;
    byStage: Record<DealStage, number>;
    pipelineValue: number;
  };
  salesOrders: {
    total: number;
    totalValue: number;
    averageOrderValue: number;
    byStatus: Record<string, number>;
    byCountry: Record<string, number>;
  };
  purchaseOrders: {
    total: number;
    totalValue: number;
    averageOrderValue: number;
    byStatus: Record<string, number>;
    byVendor: Record<string, number>;
  };
  invoices: {
    total: number;
    totalValue: number;
    averageInvoiceValue: number;
    byStatus: Record<string, number>;
    overdue: number;
    paid: number;
  };
  // Customer & Sales KPIs
  customerKPIs: {
    salesCycleLength: number; // Average days from first touch to close
    averageRevenuePerAccount: number; // Total recurring revenue / total customers
    churnRate: number; // Lost customers / start-of-period customers
    customerLifetimeValue: number; // Average gross profit per customer
    ltvToCacRatio: number; // LTV / CAC (estimated)
    customerRetentionRate: number; // (1 - churn rate) * 100
    averageDealSize: number; // Average deal amount
    conversionRate: number; // Converted leads / total leads
    winRate: number; // Won deals / total deals
  };
}

// Form Types for Creating/Editing CRM Records
export interface CreateLeadForm {
  name: string;
  company: string;
  email: string;
  phone: string;
  status: LeadStatus;
  source: LeadSource;
  value?: number;
  description?: string;
  industry?: string;
  assignedTo?: string;
}

export interface CreateTaskForm {
  subject: string;
  description: string;
  type: TaskType;
  priority: TaskPriority;
  dueDate: string;
  assignedTo: string;
  relatedTo: string;
  relatedId?: string;
}

export interface CreateContactForm {
  firstName: string;
  lastName: string;
  email: string;
  phone: string;
  company: string;
  title: string;
  status: ContactStatus;
  leadSource: LeadSource;
  address?: string;
  website?: string;
  notes?: string;
  assignedTo?: string;
}

export interface CreateDealForm {
  name: string;
  amount: number;
  stage: DealStage;
  probability: number;
  closeDate: string;
  contactName: string;
  accountName: string;
  owner: string;
  description?: string;
  source?: LeadSource;
}

