// src/server/features/reference_books/lib/access.mapper.ts
// Mapper functions for transforming external access database rows to DTOs

import type { ExternalAccess, ExternalAccessPendingEntry } from '@shared/contracts/access';

export type ExternalAccessWithMetadata = ExternalAccess & {
  targetId?: string | null;
  status?: 'pending' | 'approved' | 'rejected' | 'to_delete';
  creatorName?: string | null;
};

export function mapToAccessEntry(entry: any): ExternalAccess {
  return {
    id: entry.id,
    resourceName: entry.resourceName,
    link: entry.link,
    login: entry.login,
    password: entry.password,
    comment: entry.comment,
    isApproved: entry.isApproved,
    createdBy: entry.createdBy,
    createdAt: entry.createdAt.toISOString(),
    updatedAt: entry.updatedAt.toISOString(),
    archivedAt: entry.archivedAt?.toISOString() || null,
  };
}

export function mapPendingToAccessEntryWithMetadata(entry: any): ExternalAccessWithMetadata {
  return {
    id: entry.id,
    resourceName: entry.resourceName,
    link: entry.link,
    login: entry.login,
    password: entry.password,
    comment: entry.comment,
    isApproved: false,
    createdBy: entry.createdBy,
    createdAt: entry.createdAt.toISOString(),
    updatedAt: entry.updatedAt.toISOString(),
    archivedAt: null,
    targetId: entry.targetId,
    status: entry.status,
    creatorName: entry.creatorName || null,
  };
}

export function mapToAccessPendingEntry(entry: any): ExternalAccessPendingEntry {
  return {
    id: entry.id,
    resourceName: entry.resourceName,
    link: entry.link,
    login: entry.login,
    password: entry.password,
    comment: entry.comment,
    status: entry.status,
    targetId: entry.targetId,
    createdBy: entry.createdBy,
    creatorName: entry.creatorName || null,
    reviewedBy: entry.reviewedBy,
    reviewedAt: entry.reviewedAt?.toISOString() || null,
    rejectionReason: entry.rejectionReason,
    createdAt: entry.createdAt.toISOString(),
    updatedAt: entry.updatedAt.toISOString(),
  };
}

