/**
 * DSR API Types & Transform
 *
 * Backend DSR type definition and transformation to frontend DSRRequest format.
 */

import { DSRRequest } from './types'

// =============================================================================
// BACKEND TYPE
// =============================================================================

export interface BackendDSR {
  id: string
  tenant_id: string
  request_number: string
  request_type: string
  status: string
  priority: string
  requester_name: string
  requester_email: string
  requester_phone?: string
  requester_address?: string
  requester_customer_id?: string
  source: string
  source_details?: string
  request_text?: string
  notes?: string
  internal_notes?: string
  received_at: string
  deadline_at: string
  extended_deadline_at?: string
  extension_reason?: string
  extension_approved_by?: string
  extension_approved_at?: string
  identity_verified: boolean
  verification_method?: string
  verified_at?: string
  verified_by?: string
  verification_notes?: string
  verification_document_ref?: string
  assigned_to?: string
  assigned_at?: string
  assigned_by?: string
  completed_at?: string
  completion_notes?: string
  rejection_reason?: string
  rejection_legal_basis?: string
  erasure_checklist?: any[]
  data_export?: any
  rectification_details?: any
  objection_details?: any
  affected_systems?: string[]
  created_at: string
  updated_at: string
  created_by?: string
  updated_by?: string
}

// =============================================================================
// TRANSFORM
// =============================================================================

/**
 * Transform flat backend DSR to nested SDK DSRRequest format.
 * New compliance backend already uses the same status names as frontend types.
 */
export function transformBackendDSR(b: BackendDSR): DSRRequest {
  return {
    id: b.id,
    referenceNumber: b.request_number,
    type: b.request_type as DSRRequest['type'],
    status: (b.status as DSRRequest['status']) || 'intake',
    priority: (b.priority as DSRRequest['priority']) || 'normal',
    requester: {
      name: b.requester_name,
      email: b.requester_email,
      phone: b.requester_phone,
      address: b.requester_address,
      customerId: b.requester_customer_id,
    },
    source: (b.source as DSRRequest['source']) || 'email',
    sourceDetails: b.source_details,
    requestText: b.request_text,
    receivedAt: b.received_at,
    deadline: {
      originalDeadline: b.deadline_at,
      currentDeadline: b.extended_deadline_at || b.deadline_at,
      extended: !!b.extended_deadline_at,
      extensionReason: b.extension_reason,
      extensionApprovedBy: b.extension_approved_by,
      extensionApprovedAt: b.extension_approved_at,
    },
    completedAt: b.completed_at,
    identityVerification: {
      verified: b.identity_verified,
      method: b.verification_method as any,
      verifiedAt: b.verified_at,
      verifiedBy: b.verified_by,
      notes: b.verification_notes,
      documentRef: b.verification_document_ref,
    },
    assignment: {
      assignedTo: b.assigned_to || null,
      assignedAt: b.assigned_at,
      assignedBy: b.assigned_by,
    },
    notes: b.notes,
    internalNotes: b.internal_notes,
    erasureChecklist: b.erasure_checklist ? { items: b.erasure_checklist, canProceedWithErasure: true } : undefined,
    dataExport: b.data_export && Object.keys(b.data_export).length > 0 ? b.data_export : undefined,
    rectificationDetails: b.rectification_details && Object.keys(b.rectification_details).length > 0 ? b.rectification_details : undefined,
    objectionDetails: b.objection_details && Object.keys(b.objection_details).length > 0 ? b.objection_details : undefined,
    createdAt: b.created_at,
    createdBy: b.created_by || 'system',
    updatedAt: b.updated_at,
    updatedBy: b.updated_by,
    tenantId: b.tenant_id,
  }
}

// =============================================================================
// SHARED HELPERS
// =============================================================================

export function getSdkHeaders(): HeadersInit {
  if (typeof window === 'undefined') return {}
  return {
    'Content-Type': 'application/json',
    'X-Tenant-ID': localStorage.getItem('bp_tenant_id') || '',
    'X-User-ID': localStorage.getItem('bp_user_id') || '',
  }
}
