/**
 * Dashboard calculation and filtering utilities
 */

import { CriticalRequest, UpcomingDeadline } from '@/services/dashboard.service';
import { CriticalAlertData } from '@/components/dashboard/CriticalAlertCard';

/**
 * Filter critical requests to show only breached requests
 */
export function getBreachedRequests(
  criticalRequests: (CriticalRequest | CriticalAlertData)[]
): (CriticalRequest | CriticalAlertData)[] {
  return criticalRequests.filter(r => {
    const isBreached = (r.breachCount || 0) > 0;
    const isCritical = (r as any).isCritical === true;
    const status = (r as any).status;
    
    if (status) {
      const isPendingOrInProgress = status === 'pending' || status === 'in-progress' || 
                                     status === 'PENDING' || status === 'IN_PROGRESS';
      return (isBreached || isCritical) && isPendingOrInProgress;
    }
    
    return isBreached || isCritical;
  });
}

/**
 * Filter upcoming deadlines to show only requests about to breach (not yet breached)
 */
export function getUpcomingDeadlinesNotBreached(
  upcomingDeadlines: UpcomingDeadline[]
): UpcomingDeadline[] {
  return upcomingDeadlines.filter(deadline => {
    const tatPercentage = Number(deadline.tatPercentageUsed) || 0;
    const remainingHours = Number(deadline.remainingHours) || 0;
    return remainingHours > 0 && tatPercentage < 100;
  });
}

/**
 * Format breach time for display
 * Backend returns working hours, so we divide by 8 (working hours per day) not 24
 */
export function formatBreachTime(hours: number): string {
  if (hours <= 0) return 'Just breached';
  if (hours < 1) return `${Math.round(hours * 60)} min`;
  
  const WORKING_HOURS_PER_DAY = 8;
  
  // If less than one working day, show hours and minutes
  if (hours < WORKING_HOURS_PER_DAY) {
    const h = Math.floor(hours);
    const m = Math.round((hours - h) * 60);
    return m > 0 ? `${h}h ${m}m` : `${h}h`;
  }
  
  // Convert working hours to working days
  const days = Math.floor(hours / WORKING_HOURS_PER_DAY);
  const remainingHours = hours % WORKING_HOURS_PER_DAY;
  
  if (remainingHours > 0) {
    const h = Math.floor(remainingHours);
    const m = Math.round((remainingHours - h) * 60);
    return m > 0 ? `${days}d ${h}h ${m}m` : `${days}d ${h}h`;
  }
  return `${days}d`;
}

/**
 * Get TAT color class based on percentage
 */
export function getTATColorClass(tat: number): string {
  if (tat >= 95) return 'bg-green-100 text-green-700';
  if (tat >= 90) return 'bg-blue-100 text-blue-700';
  if (tat >= 85) return 'bg-orange-100 text-orange-700';
  return 'bg-red-100 text-red-700';
}

/**
 * Get page numbers for pagination display
 */
export function getPageNumbers(currentPage: number, totalPages: number, maxPagesToShow: number = 3): number[] {
  const pages = [];
  let startPage = Math.max(1, currentPage - Math.floor(maxPagesToShow / 2));
  let endPage = Math.min(totalPages, startPage + maxPagesToShow - 1);
  
  if (endPage - startPage < maxPagesToShow - 1) {
    startPage = Math.max(1, endPage - maxPagesToShow + 1);
  }
  
  for (let i = startPage; i <= endPage; i++) {
    pages.push(i);
  }
  
  return pages;
}

