/**
 * Utility functions for salary and cost calculations
 */

/**
 * Calculate the hourly rate for an employee based on their gross salary and company settings
 * @param grossSalary - The employee's gross monthly salary in INR (as string or number)
 * @param workingHoursPerDay - The standard working hours per day (default: 8)
 * @param workingDaysPerMonth - The standard working days per month (default: 22)
 * @returns The hourly rate in INR
 */
export function calculateHourlyRate(
  grossSalary: string | number, 
  workingHoursPerDay: number = 8, 
  workingDaysPerMonth: number = 22
): number {
  // Convert salary to number if it's a string
  const salary = typeof grossSalary === 'string' ? parseFloat(grossSalary) : grossSalary;
  
  // Calculate total working hours per month
  const workingHoursPerMonth = workingHoursPerDay * workingDaysPerMonth;
  
  // Calculate hourly rate
  const hourlyRate = salary / workingHoursPerMonth;
  
  return hourlyRate;
}

/**
 * Calculate the cost of time spent on a project
 * @param hours - Number of hours spent
 * @param hourlyRate - Hourly rate in INR
 * @returns The cost in INR
 */
export function calculateCost(hours: number, hourlyRate: number): number {
  return hours * hourlyRate;
}

/**
 * Format currency in INR format
 * @param amount - The amount to format
 * @returns Formatted string with INR symbol
 */
export function formatCurrency(amount: number): string {
  return new Intl.NumberFormat('en-IN', {
    style: 'currency',
    currency: 'INR',
    maximumFractionDigits: 0
  }).format(amount);
}

/**
 * Convert seconds to hours
 * @param seconds - Total seconds
 * @returns Hours (can be decimal)
 */
export function secondsToHours(seconds: number): number {
  return seconds / 3600;
}

/**
 * Format hours in HH:MM format
 * @param hours - Hours (can be decimal)
 * @returns Formatted string
 */
export function formatHours(hours: number): string {
  const wholeHours = Math.floor(hours);
  const minutes = Math.round((hours - wholeHours) * 60);
  return `${wholeHours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}`;
}
