/**
 * Utility functions for project budget calculations
 */

import { formatCurrency } from "./salary-utils";

/**
 * Interface for time entry data
 */
interface TimeEntry {
  id: string;
  projectId: string;
  cost: number;
  // Other fields omitted for brevity
}

/**
 * Calculate the total budget used for a project
 * @param projectId - The project ID
 * @param timeEntries - Array of time entries
 * @returns The total cost in INR
 */
export function calculateProjectBudgetUsed(
  projectId: string,
  timeEntries: TimeEntry[]
): number {
  // Filter time entries for the specific project
  const projectEntries = timeEntries.filter(
    (entry) => entry.projectId === projectId
  );

  // Sum up the costs
  const totalCost = projectEntries.reduce((sum, entry) => sum + entry.cost, 0);

  return totalCost;
}

/**
 * Calculate the percentage of budget used
 * @param budgetUsed - Amount of budget used
 * @param totalBudget - Total project budget
 * @returns Percentage as a number (0-100)
 */
export function calculateBudgetPercentage(
  budgetUsed: number,
  totalBudget: number
): number {
  if (totalBudget <= 0) return 0;
  const percentage = (budgetUsed / totalBudget) * 100;
  return Math.min(100, Math.max(0, percentage)); // Clamp between 0-100
}

/**
 * Get a color code based on budget usage percentage
 * @param percentage - Budget usage percentage (0-100)
 * @returns CSS color class
 */
export function getBudgetStatusColor(percentage: number): string {
  if (percentage < 50) return "text-green-600";
  if (percentage < 80) return "text-amber-600";
  return "text-red-600";
}

/**
 * Format budget display with used/total
 * @param budgetUsed - Amount used
 * @param totalBudget - Total budget
 * @returns Formatted string
 */
export function formatBudgetDisplay(
  budgetUsed: number,
  totalBudget: number
): string {
  return `${formatCurrency(budgetUsed)} of ${formatCurrency(totalBudget)}`;
}

/**
 * Group time entries by activity type
 * @param projectId - The project ID
 * @param timeEntries - Array of time entries
 * @returns Object with activity type as key and total cost as value
 */

/**
 * Group time entries by employee
 * @param projectId - The project ID
 * @param timeEntries - Array of time entries
 * @returns Object with employee ID as key and total cost as value
 */

/**
 * Interface for project-employee relationship
 */
interface ProjectEmployee {
  projectId: string;
  employeeId: string;
  // Other fields omitted for brevity
}

/**
 * Get projects assigned to a specific employee
 * @param employeeId - The employee ID
 * @param projectEmployees - Array of project-employee relationships
 * @returns Array of project IDs
 */
export function getProjectsAssignedToEmployee(
  employeeId: string,
  projectEmployees: ProjectEmployee[]
): string[] {
  return projectEmployees
    .filter((pe) => pe.employeeId === employeeId)
    .map((pe) => pe.projectId);
}

/**
 * Filter projects by employee assignment
 * @param projects - Array of projects
 * @param employeeId - The employee ID
 * @param projectEmployees - Array of project-employee relationships
 * @returns Array of filtered projects
 */
export function filterProjectsByEmployee<T extends { id: string }>(
  projects: T[],
  employeeId: string,
  projectEmployees: ProjectEmployee[]
): T[] {
  const assignedProjectIds = getProjectsAssignedToEmployee(
    employeeId,
    projectEmployees
  );
  return projects.filter((project) => assignedProjectIds.includes(project.id));
}
