export interface CrmDashboardFilters {
  dateFrom?: string | null;
  dateTo?: string | null;
  branch?: string | null;
  salesman?: string | null;
  projectPartyRole?: string | null;
  productType?: string | null;
}

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

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

export interface CrmDashboardReferenceData {
  branches: CrmDashboardOption[];
  salesmen: CrmDashboardSalesOption[];
  productTypes: CrmDashboardOption[];
  projectPartyRoles: CrmDashboardOption[];
}

export interface CrmDashboardSummary {
  lead: {
    leadCount: number;
    newLeads: number;
    unassignedLeads: number;
    averageLeadAgingDays: number;
  };
  opportunity: {
    opportunityCount: number;
    activeOpportunities: number;
    wonCount: number;
    lostCount: number;
    winRate: number;
    hotOpportunities: number;
    opportunityValue: number;
  };
  quotation: {
    draftQuotations: number;
    pendingApproval: number;
    approvedQuotations: number;
    sentQuotations: number;
  };
  revenue: {
    quotationValue: number;
    wonValue: number;
    lostValue: number;
  };
}

export interface CrmDashboardFunnelStep {
  key: string;
  label: string;
  count: number;
  value: number;
  conversionRate: number | null;
}

export interface CrmDashboardRevenueRow {
  customerId: string;
  customerCode: string;
  customerName: string;
  roleCode: string;
  revenue: number;
  quotationCount: number;
  wonRevenue: number;
}

export interface CrmDashboardSalesRankingRow {
  salesPersonId: string;
  salesPersonName: string;
  leadCount: number;
  opportunityCount: number;
  quotationCount: number;
  approvedQuotations: number;
  wonRevenue: number;
  conversionRate: number;
}

export interface CrmDashboardFollowupSummary {
  dueToday: number;
  dueThisWeek: number;
  overdue: number;
  completed: number;
}

export interface CrmDashboardFollowupRow {
  id: string;
  sourceType: 'opportunity' | 'quotation';
  followupDate: string;
  customerName: string;
  opportunityName: string;
  assignedSalesName: string | null;
  outcome: string | null;
}

export interface CrmDashboardApprovalSummary {
  pendingApprovals: number;
  approvedToday: number;
  rejectedToday: number;
  returnedToday: number;
  averageApprovalTimeHours: number;
}

export interface CrmDashboardApprovalRow {
  id: string;
  entityCode: string | null;
  entityTitle: string | null;
  workflowName: string;
  currentStepRoleName: string | null;
  requestedAt: string;
}

export interface CrmDashboardHotProjectRow {
  opportunityId: string;
  opportunityCode: string;
  projectName: string;
  customerName: string;
  value: number;
  assignedSalesName: string | null;
  probability: number | null;
}

export interface CrmDashboardResponse {
  success: boolean;
  time: string;
  message: string;
  filters: Required<CrmDashboardFilters>;
  referenceData: CrmDashboardReferenceData;
  summary: CrmDashboardSummary;
  funnel: CrmDashboardFunnelStep[];
  revenueAnalytics: {
    topEndCustomers: CrmDashboardRevenueRow[];
    topBillingCustomers: CrmDashboardRevenueRow[];
    topContractors: CrmDashboardRevenueRow[];
    topConsultants: CrmDashboardRevenueRow[];
  };
  salesRanking: CrmDashboardSalesRankingRow[];
  followups: {
    summary: CrmDashboardFollowupSummary;
    upcoming: CrmDashboardFollowupRow[];
  };
  approvals: {
    summary: CrmDashboardApprovalSummary;
    myPendingApprovals: CrmDashboardApprovalRow[];
  };
  hotProjects: CrmDashboardHotProjectRow[];
  outcomeAnalytics: {
    winRate: number;
    lostByReason: Array<{ key: string; label: string; count: number; revenue: number }>;
    lostByCompetitor: Array<{ key: string; label: string; count: number; revenue: number }>;
  };
  visibility: {
    canViewCommercialData: boolean;
    canViewApprovalData: boolean;
  };
}

export function createEmptyCrmDashboardResponse(
  filters: Required<CrmDashboardFilters>,
  message = 'CRM dashboard is temporarily unavailable.'
): CrmDashboardResponse {
  return {
    success: false,
    time: new Date().toISOString(),
    message,
    filters,
    referenceData: {
      branches: [],
      salesmen: [],
      productTypes: [],
      projectPartyRoles: []
    },
    summary: {
      lead: {
        leadCount: 0,
        newLeads: 0,
        unassignedLeads: 0,
        averageLeadAgingDays: 0
      },
      opportunity: {
        opportunityCount: 0,
        activeOpportunities: 0,
        wonCount: 0,
        lostCount: 0,
        winRate: 0,
        hotOpportunities: 0,
        opportunityValue: 0
      },
      quotation: {
        draftQuotations: 0,
        pendingApproval: 0,
        approvedQuotations: 0,
        sentQuotations: 0
      },
      revenue: {
        quotationValue: 0,
        wonValue: 0,
        lostValue: 0
      }
    },
    funnel: [],
    revenueAnalytics: {
      topEndCustomers: [],
      topBillingCustomers: [],
      topContractors: [],
      topConsultants: []
    },
    salesRanking: [],
    followups: {
      summary: {
        dueToday: 0,
        dueThisWeek: 0,
        overdue: 0,
        completed: 0
      },
      upcoming: []
    },
    approvals: {
      summary: {
        pendingApprovals: 0,
        approvedToday: 0,
        rejectedToday: 0,
        returnedToday: 0,
        averageApprovalTimeHours: 0
      },
      myPendingApprovals: []
    },
    hotProjects: [],
    outcomeAnalytics: {
      winRate: 0,
      lostByReason: [],
      lostByCompetitor: []
    },
    visibility: {
      canViewCommercialData: false,
      canViewApprovalData: false
    }
  };
}
