/**
 * Create a new document
 */

import type { MutationCtx } from '../_generated/server';
import { buildFolderPath } from '../folders/queries';
import { toConvexJsonRecord } from '../lib/type_cast_helpers';
import { extractExtension } from './extract_extension';
import { teamIdToFields } from './team_fields';
import type { CreateDocumentArgs, CreateDocumentResult } from './types';

export async function createDocument(
  ctx: MutationCtx,
  args: CreateDocumentArgs,
): Promise<CreateDocumentResult> {
  if (args.folderId) {
    const folder = await ctx.db.get(args.folderId);
    if (!folder || folder.organizationId !== args.organizationId) {
      throw new Error('Folder not found');
    }
  }

  const extension = args.extension ?? extractExtension(args.title);

  const teamFields = teamIdToFields(args.teamId);

  const folderPath =
    args.folderPath ??
    (args.folderId ? await buildFolderPath(ctx, args.folderId) : undefined);

  const documentId = await ctx.db.insert('documents', {
    organizationId: args.organizationId,
    title: args.title,

    content: args.content,
    fileId: args.fileId,
    mimeType: args.mimeType,
    extension,
    metadata: toConvexJsonRecord(args.metadata),
    sourceProvider: args.sourceProvider,
    externalItemId: args.externalItemId,
    contentHash: args.contentHash,
    ...teamFields,
    sourceCreatedAt: args.sourceCreatedAt,
    sourceModifiedAt: args.sourceModifiedAt,
    createdBy: args.createdBy,
    folderId: args.folderId,
    folderPath,
  });

  return {
    success: true,
    documentId,
  };
}
