/**
 * Request data transformation utilities
 */

import { ClosedRequest } from '../types/closedRequests.types';

export function transformClosedRequest(r: any): ClosedRequest {
  return {
    id: r.requestNumber || r.request_number || r.requestId, // Use requestNumber as primary identifier
    requestId: r.requestId, // Keep requestId for reference
    displayId: r.requestNumber || r.request_number || r.requestId,
    title: r.title,
    description: r.description,
    status: (r.status || '').toString().toLowerCase() as 'rejected' | 'closed',
    priority: (r.priority || '').toString().toLowerCase() as 'express' | 'standard',
    initiator: {
      name: r.initiator?.displayName || r.initiator?.email || '—',
      avatar: (r.initiator?.displayName || 'NA')
        .split(' ')
        .map((s: string) => s[0])
        .join('')
        .slice(0, 2)
        .toUpperCase()
    },
    createdAt: r.submittedAt || r.createdAt || r.created_at || '—',
    dueDate: r.closureDate || r.closure_date || r.closedAt || undefined,
    reason: r.conclusionRemark || r.conclusion_remark,
    department: r.department,
    totalLevels: r.totalLevels || 0,
    completedLevels: r.summary?.approvedLevels || 0,
    templateType: r.templateType || r.template_type, // Template type for badge display
    form16Submission: r.form16Submission ?? null, // Form 16 columns when incorporated from Form 16 Closed
  };
}

export function transformClosedRequests(data: any[]): ClosedRequest[] {
  return data.map(transformClosedRequest);
}

