/**
 * Request data transformation utilities
 */

import { MyRequest } from '../types/myRequests.types';

/**
 * Transform API/dynamic request data to MyRequest format
 */
export function transformRequest(req: any): MyRequest {
  const createdAt = req.submittedAt || req.submitted_at || req.createdAt || req.created_at;
  const priority = (req.priority || '').toString().toLowerCase();

  return {
    id: req.requestNumber || req.request_number || req.requestId || req.id || req.request_id,
    requestId: req.requestId || req.id || req.request_id,
    displayId: req.requestNumber || req.request_number || req.id,
    title: req.title,
    description: req.description,
    status: (req.status || '').toString().toLowerCase().replace('_', '-'),
    priority: priority,
    department: req.department,
    submittedDate: req.submittedAt || (req.createdAt ? new Date(req.createdAt).toISOString().split('T')[0] : undefined),
    createdAt: createdAt,
    currentApprover: req.currentApprover?.name || req.currentApprover?.email || '—',
    approverLevel:
      req.currentLevel && req.totalLevels
        ? `${req.currentLevel} of ${req.totalLevels}`
        : req.currentStep && req.totalSteps
          ? `${req.currentStep} of ${req.totalSteps}`
          : '—',
    templateType: req.templateType || req.template_type,
    workflowType: req.workflowType || req.workflow_type,
    templateName: req.templateName || req.template_name,
  };
}

/**
 * Transform array of requests
 */
export function transformRequests(requests: any[]): MyRequest[] {
  return Array.isArray(requests) ? requests.map(transformRequest) : [];
}

