/**
 * Utility functions for building approval levels
 */

import { ApprovalLevel, Approver } from '../types/createRequest.types';

/**
 * Calculate TAT in hours
 */
export function calculateTatHours(tat: number | string, tatType: 'hours' | 'days'): number {
  const tatValue = typeof tat === 'number' ? tat : parseInt(String(tat)) || 0;
  return tatType === 'days' ? tatValue * 24 : tatValue || 24;
}

/**
 * Build a single approval level
 */
export function buildApproverLevel(
  approver: Approver,
  levelNumber: number,
  isFinalApprover: boolean
): ApprovalLevel {
  const tatHours = calculateTatHours(approver.tat || 24, approver.tatType || 'hours');

  return {
    levelNumber,
    levelName: `Level ${levelNumber}`,
    approverId: approver.userId || '',
    approverEmail: approver.email || '',
    approverName: approver.name || approver.email?.split('@')[0] || `Approver ${levelNumber}`,
    tatHours,
    isFinalApprover,
  };
}

/**
 * Build all approval levels from approvers array
 */
export function buildApprovalLevels(
  approvers: Approver[],
  approverCount: number
): ApprovalLevel[] {
  return approvers
    .slice(0, approverCount)
    .map((approver, index) => {
      const levelNumber = index + 1;
      const isFinalApprover = levelNumber === approverCount;
      return buildApproverLevel(approver, levelNumber, isFinalApprover);
    });
}

