import { authCondition } from "$lib/server/auth";
import { config } from "$lib/server/config";
import { collections } from "$lib/server/database";
import { logger } from "$lib/server/logger";
import {
	getShareThumbnailPng,
	shareThumbnailPrompt,
} from "$lib/server/shareThumbnail/shareThumbnail";
import type { SharedConversation } from "$lib/types/SharedConversation";
import { hashConv } from "$lib/utils/hashConv";
import { error } from "@sveltejs/kit";
import { ObjectId } from "mongodb";
import { nanoid } from "nanoid";

// Render the social-preview thumbnail as soon as the share link exists, off
// the response path: link unfurlers (Slack, iMessage, ...) typically fetch
// the og:image within seconds of the link being created, and rendering it
// here means they hit the cache instead of paying for a fresh render.
function prewarmShareThumbnail(shared: SharedConversation): void {
	getShareThumbnailPng({
		prompt: shareThumbnailPrompt(shared),
		isHuggingChat: config.isHuggingChat,
		appName: config.PUBLIC_APP_NAME,
	}).catch((err) => logger.warn({ err }, "Failed to prewarm share thumbnail"));
}

export async function POST({ params, locals }) {
	const conversation = await collections.conversations.findOne({
		_id: new ObjectId(params.id),
		...authCondition(locals),
	});

	if (!conversation) {
		error(404, "Conversation not found");
	}

	const hash = await hashConv(conversation);

	const existingShare = await collections.sharedConversations.findOne({ hash });

	if (existingShare) {
		prewarmShareThumbnail(existingShare);
		return new Response(
			JSON.stringify({
				shareId: existingShare._id,
			}),
			{ headers: { "Content-Type": "application/json" } }
		);
	}

	const shared: SharedConversation = {
		_id: nanoid(7),
		hash,
		createdAt: new Date(),
		updatedAt: new Date(),
		rootMessageId: conversation.rootMessageId,
		messages: conversation.messages,
		title: conversation.title,
		model: conversation.model,
		preprompt: conversation.preprompt,
	};

	await collections.sharedConversations.insertOne(shared);

	prewarmShareThumbnail(shared);

	// copy files from `${conversation._id}-` to `${shared._id}-`
	const files = await collections.bucket
		.find({ filename: { $regex: `^${conversation._id}-` } })
		.toArray();

	await Promise.all(
		files.map(async (file) => {
			const newFilename = file.filename.replace(`${conversation._id}-`, `${shared._id}-`);
			// copy files from `${conversation._id}-` to `${shared._id}-` by downloading and reuploaidng
			const downloadStream = collections.bucket.openDownloadStream(file._id);
			const uploadStream = collections.bucket.openUploadStream(newFilename, {
				metadata: { ...file.metadata, conversation: shared._id.toString() },
			});
			downloadStream.pipe(uploadStream);
		})
	);

	return new Response(
		JSON.stringify({
			shareId: shared._id,
		}),
		{ headers: { "Content-Type": "application/json" } }
	);
}
