export const generateRequestNumber = (): string => {
  const year = new Date().getFullYear();
  const randomNumber = Math.floor(Math.random() * 100000).toString().padStart(5, '0');
  return `REQ-${year}-${randomNumber}`;
};

export const calculateTATDays = (tatHours: number): number => {
  return Math.ceil(tatHours / 24);
};

export const calculateElapsedHours = (startTime: Date, endTime?: Date): number => {
  const end = endTime || new Date();
  const diffMs = end.getTime() - startTime.getTime();
  return Math.round((diffMs / (1000 * 60 * 60)) * 100) / 100; // Round to 2 decimal places
};

export const calculateTATPercentage = (elapsedHours: number, totalTatHours: number): number => {
  if (totalTatHours === 0) return 0;
  return Math.min(Math.round((elapsedHours / totalTatHours) * 100), 100);
};

export const isTATBreached = (elapsedHours: number, totalTatHours: number): boolean => {
  return elapsedHours > totalTatHours;
};

export const isTATApproaching = (elapsedHours: number, totalTatHours: number, threshold: number = 80): boolean => {
  const percentage = calculateTATPercentage(elapsedHours, totalTatHours);
  return percentage >= threshold && percentage < 100;
};

export const formatDate = (date: Date): string => {
  return date.toISOString().split('T')[0];
};

export const formatDateTime = (date: Date): string => {
  return date.toISOString();
};

export const isValidEmail = (email: string): boolean => {
  const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
  return emailRegex.test(email);
};

export const isValidUUID = (uuid: string): boolean => {
  const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
  return uuidRegex.test(uuid);
};

export const sanitizeString = (str: string): string => {
  return str.trim().replace(/[<>]/g, '');
};

export const generateChecksum = (data: string): string => {
  const crypto = require('crypto');
  return crypto.createHash('sha256').update(data).digest('hex');
};

export const sleep = (ms: number): Promise<void> => {
  return new Promise(resolve => setTimeout(resolve, ms));
};
