// src/server/features/reference_books/lib/inventory.mapper.ts
// Mappers for converting database rows to unified DTOs

import { inventoryTable, inventoryPendingTable } from '../db/inventory.table';
import type { Inventory, InventoryPending } from '../db/inventory.table';
import type { InventoryEntry, InventoryPendingEntry, InventoryUnifiedEntry } from '@shared/contracts/inventory';
import type { InventoryEntryWithMetadata } from '../inventory.schema';

// Type for inventory entry
type InventoryEntryType = typeof inventoryTable.$inferSelect;
type InventoryPendingEntryType = typeof inventoryPendingTable.$inferSelect;

/**
 * Map inventory entry to unified DTO
 */
export function mapToInventoryEntry(entry: InventoryEntryType): InventoryEntry {
  return {
    id: entry.id,
    name: entry.name,
    category: entry.category,
    quantity: entry.quantity,
    unit: entry.unit,
    location: entry.location,
    comment: entry.comment,
    weightPerUnit: entry.weightPerUnit,
    manufacturer: entry.manufacturer,
    model: entry.model,
    serialNumber: entry.serialNumber,
    orderingLink: entry.orderingLink,
    articleNumber: entry.articleNumber,
    isApproved: !entry.archivedAt,
    status: undefined,
    targetId: null,
    createdBy: null,
    creatorName: null,
    reviewedBy: null,
    reviewedAt: null,
    rejectionReason: null,
    createdAt: entry.createdAt.toISOString(),
    updatedAt: entry.updatedAt.toISOString(),
    archivedAt: entry.archivedAt?.toISOString() || null,
  };
}

/**
 * Map inventory pending entry to unified DTO
 * FIX: Support creatorName from left join with employees table
 */
export function mapToInventoryPendingEntry(entry: InventoryPendingEntryType & { creatorName?: string | null }): InventoryPendingEntry {
  return {
    id: entry.id,
    name: entry.name,
    category: entry.category,
    quantity: entry.quantity,
    unit: entry.unit,
    location: entry.location,
    comment: entry.comment,
    weightPerUnit: entry.weightPerUnit,
    manufacturer: entry.manufacturer,
    model: entry.model,
    serialNumber: entry.serialNumber,
    orderingLink: entry.orderingLink,
    articleNumber: entry.articleNumber,
    status: entry.status as 'pending' | 'approved' | 'rejected' | 'to_delete',
    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(),
    archivedAt: null,
    isApproved: false,
  };
}

/**
 * Map inventory pending entry to unified DTO with creator name
 * FIX: Support creatorName from left join with employees table
 */
export function mapToInventoryPendingEntryWithMetadata(entry: InventoryPendingEntryType & { creatorName?: string | null }): InventoryUnifiedEntry {
  return {
    ...mapToInventoryPendingEntry(entry),
    creatorName: entry.creatorName || null,
  };
}

/**
 * Map inventory entry to unified DTO with creator name
 */
export function mapToInventoryEntryWithMetadata(entry: InventoryEntryType): InventoryUnifiedEntry {
  return {
    ...mapToInventoryEntry(entry),
    creatorName: null, // Will be filled by service layer
  };
}

/**
 * Map inventory entry to InventoryEntryWithMetadata (for service layer)
 * FIX: Support creatorName from core functions
 */
export function mapToInventoryEntryWithMetadataStrict(entry: InventoryEntryType & { creatorName?: string | null }): InventoryEntryWithMetadata {
  return {
    ...mapToInventoryEntry(entry),
    creatorName: entry.creatorName || null,
  };
}
