// src/services/metadata-service.ts
import { getOneDriveFileBuffer } from "./onedrive";
import { extractMetadata } from "@/lib/metadata-extractor"; 
import { getFileNodeById, updateFileNode } from "@/data-access/file-nodes";

export async function enrichFileMetadata(fileId: string, token: string) {
  const node = await getFileNodeById(fileId);
  if (!node || !node.oneDriveId) throw new Error("Node not found");

  // Fetch file from OneDrive
  const buffer = await getOneDriveFileBuffer(node.oneDriveId, token);

  // Extract deep metadata (PDF, JPEG, etc.)
  const deepMetadata = await extractMetadata(buffer, node.name);

  // Merge with existing metadata
  const updatedMetadata = {
    ...(node.metadata as object),
    ...deepMetadata, 
    magicFilled: true
  };

  // Update DB
  return await updateFileNode(fileId, { metadata: updatedMetadata });
}