/**
 * Calculation utilities for request stats
 */

import type { RequestStats, BackendStats } from '../types/requests.types';

export function calculateStatsFromFilteredData(
  allFilteredRequests: any[],
  isOrgLevel: boolean,
  backendStats: BackendStats | null,
  hasActiveFilters: boolean,
  totalRecords: number,
  convertedRequests: any[]
): RequestStats {
  // Check if we have active filters (excluding default date range)
  const hasFilters = hasActiveFilters;

  // Use allFilteredRequests (data filtered by all filters EXCEPT status) for stats calculation
  // This ensures stats show all status counts based on other filters (priority, department, etc.)
  if (allFilteredRequests.length > 0) {
    // Always show all status counts - status filter only affects the list, not the stats
    const total = allFilteredRequests.length;
    const pending = allFilteredRequests.filter((r: any) => {
      const status = (r.status || '').toString().toUpperCase();
      return status === 'PENDING' || status === 'IN_PROGRESS';
    }).length;
    const approved = allFilteredRequests.filter((r: any) => {
      const status = (r.status || '').toString().toUpperCase();
      return status === 'APPROVED';
    }).length;
    const rejected = allFilteredRequests.filter((r: any) => {
      const status = (r.status || '').toString().toUpperCase();
      return status === 'REJECTED';
    }).length;
    const draft = allFilteredRequests.filter((r: any) => {
      const status = (r.status || '').toString().toUpperCase();
      return status === 'DRAFT';
    }).length;
    const closed = allFilteredRequests.filter((r: any) => {
      const status = (r.status || '').toString().toUpperCase();
      return status === 'CLOSED';
    }).length;

    const paused = allFilteredRequests.filter((r: any) => {
      const status = (r.status || '').toString().toUpperCase();
      return status === 'PAUSED';
    }).length;

    return {
      total: total, // Total based on other filters (priority, department, etc.)
      pending,
      paused,
      approved,
      rejected,
      draft,
      closed
    };
  } else if (isOrgLevel && !hasFilters && backendStats) {
    // No filters and backend stats available - use backend stats for better performance
    return {
      total: backendStats.total,
      pending: backendStats.pending,
      paused: backendStats.paused || 0,
      approved: backendStats.approved,
      rejected: backendStats.rejected,
      draft: backendStats.draft,
      closed: backendStats.closed
    };
  } else {
    // Fallback: calculate from convertedRequests (paginated data - less accurate)
    // Note: This fallback should ideally not be used, but if it is, we still show all status counts
    const total = totalRecords || convertedRequests.length;
    
    return {
      total: total,
      pending: convertedRequests.filter(r => r.status === 'pending' || r.status === 'in-progress').length,
      paused: convertedRequests.filter(r => r.status === 'paused').length,
      approved: convertedRequests.filter(r => r.status === 'approved').length,
      rejected: convertedRequests.filter(r => r.status === 'rejected').length,
      draft: convertedRequests.filter(r => r.status === 'draft').length,
      closed: convertedRequests.filter(r => r.status === 'closed').length
    };
  }
}

