export interface CrmReportDefinitionRecord {
  id: string;
  code: string;
  name: string;
  description: string | null;
  category: string;
  categoryLabel: string;
  isSystem: boolean;
  isActive: boolean;
}

export interface CrmReportCatalogGroup {
  category: string;
  categoryLabel: string;
  items: CrmReportDefinitionRecord[];
}

export interface CrmReportFilterOption {
  id: string;
  code: string;
  label: string;
}

export interface CrmReportUserOption {
  id: string;
  name: string;
}

export interface CrmSharedReportFilters {
  dateFrom?: string | null;
  dateTo?: string | null;
  branch?: string | null;
  productType?: string | null;
  sales?: string | null;
  customer?: string | null;
  customerOwner?: string | null;
  leadSource?: string | null;
  pipelineStage?: string | null;
  outcome?: string | null;
  lostReason?: string | null;
}

export interface CrmReportFilterMetadata {
  branches: CrmReportFilterOption[];
  productTypes: CrmReportFilterOption[];
  sales: CrmReportUserOption[];
  customers: CrmReportFilterOption[];
  customerOwners: CrmReportUserOption[];
  leadSources: CrmReportFilterOption[];
  pipelineStages: CrmReportFilterOption[];
  outcomes: CrmReportFilterOption[];
  lostReasons: CrmReportFilterOption[];
}

export interface CrmReportsResponse {
  success: boolean;
  time: string;
  message: string;
  items: CrmReportDefinitionRecord[];
  groups: CrmReportCatalogGroup[];
}

export interface CrmReportCatalogResponse {
  success: boolean;
  time: string;
  message: string;
  groups: CrmReportCatalogGroup[];
}

export interface CrmReportFiltersResponse {
  success: boolean;
  time: string;
  message: string;
  filters: CrmReportFilterMetadata;
}

export interface CrmPipelineReportRow {
  leadSourceLabel: string;
  customerOwnerName: string;
  createdByName: string;
  branchLabel: string;
  productTypeLabel: string;
  totalLeads: number;
  newLeads: number;
  assignedLeads: number;
  unassignedLeads: number;
  convertedLeads: number;
}

export interface CrmOpportunityPipelineReportRow {
  salesmanName: string;
  branchLabel: string;
  productTypeLabel: string;
  customerOwnerName: string;
  openOpportunities: number;
  convertedToQuotation: number;
  won: number;
  lost: number;
}

export interface CrmLeadConversionReportRow {
  leadSourceLabel: string;
  branchLabel: string;
  productTypeLabel: string;
  marketingUserName: string;
  totalLeads: number;
  convertedOpportunities: number;
  conversionRate: number;
}

export interface CrmOpportunityConversionReportRow {
  salesmanName: string;
  branchLabel: string;
  productTypeLabel: string;
  totalOpportunities: number;
  opportunitiesWithQuotation: number;
  conversionRate: number;
}

export interface CrmPipelineFunnelRow {
  stageKey: 'lead' | 'opportunity' | 'quotation' | 'closed_won' | 'closed_lost';
  stageLabel: string;
  count: number;
  value: number | null;
  conversionRate: number | null;
}

export interface CrmPipelineKpiSummary {
  totalLeads: number;
  openOpportunities: number;
  quotations: number;
  won: number;
  lost: number;
  canViewRevenueValues: boolean;
}

export interface CrmPipelineReportResponse {
  success: boolean;
  time: string;
  message: string;
  filters: Required<CrmSharedReportFilters>;
  summary: CrmPipelineKpiSummary;
  leadPipeline: CrmPipelineReportRow[];
  opportunityPipeline: CrmOpportunityPipelineReportRow[];
  leadConversion: CrmLeadConversionReportRow[];
  opportunityConversion: CrmOpportunityConversionReportRow[];
  funnel: CrmPipelineFunnelRow[];
}

export interface CrmAgingBucket {
  key: string;
  label: string;
  count: number;
}

export interface CrmLeadAgingRow {
  leadId: string;
  leadCode: string;
  customerName: string;
  assignedUserName: string | null;
  createdDate: string;
  agingDays: number;
}

export interface CrmLeadAgingReportResponse {
  success: boolean;
  time: string;
  message: string;
  filters: Required<CrmSharedReportFilters>;
  summary: {
    leadCount: number;
    averageAgingDays: number;
    maximumAgingDays: number;
  };
  buckets: CrmAgingBucket[];
  rows: CrmLeadAgingRow[];
}

export interface CrmOpportunityAgingRow {
  opportunityId: string;
  opportunityCode: string;
  customerName: string;
  salesmanName: string | null;
  lastFollowupDate: string | null;
  agingDays: number;
}

export interface CrmOpportunityAgingReportResponse {
  success: boolean;
  time: string;
  message: string;
  filters: Required<CrmSharedReportFilters>;
  summary: {
    openOpportunities: number;
    averageAgingDays: number;
    maximumAgingDays: number;
  };
  buckets: CrmAgingBucket[];
  rows: CrmOpportunityAgingRow[];
}

