import { traceEmbedding } from "@giselles-ai/langfuse";
import type { EmbeddingProfileId } from "@giselles-ai/protocol";
import type { EmbeddingCompleteCallback } from "@giselles-ai/rag";
import type {
	DocumentVectorStoreId,
	DocumentVectorStoreSourceId,
} from "@giselles-ai/types";
import type { TeamWithSubscription } from "@/services/teams";

export type DocumentIngestTrigger =
	| { type: "manual"; userId: string }
	| { type: "cron"; userId?: string };

interface DocumentIngestEmbeddingMetadata {
	team: TeamWithSubscription;
	documentVectorStore: {
		id: DocumentVectorStoreId;
		dbId: number;
	};
	source: {
		id: DocumentVectorStoreSourceId;
		dbId: number;
		fileName: string;
	};
	trigger: DocumentIngestTrigger;
	embeddingProfileId: EmbeddingProfileId;
}

export function createDocumentIngestEmbeddingCallback(
	args: DocumentIngestEmbeddingMetadata,
): EmbeddingCompleteCallback {
	return async (metrics) => {
		const planTag = `plan:${args.team.plan}`;
		const userId =
			args.trigger.type === "manual"
				? args.trigger.userId
				: (args.trigger.userId ?? "cron");

		try {
			await traceEmbedding({
				metrics,
				userId,
				sessionId: args.source.id,
				tags: [planTag, "embedding-purpose:ingestion"],
				metadata: {
					teamId: args.team.id,
					teamDbId: args.team.dbId,
					teamPlan: args.team.plan,
					subscriptionId: args.team.activeSubscriptionId ?? "",
					userId,
					triggerType: args.trigger.type,
					documentVectorStoreId: args.documentVectorStore.id,
					documentVectorStoreDbId: args.documentVectorStore.dbId,
					documentVectorStoreSourceId: args.source.id,
					documentVectorStoreSourceDbId: args.source.dbId,
					documentFileName: args.source.fileName,
					embeddingProfileId: args.embeddingProfileId,
				},
			});
		} catch (error) {
			console.error("Failed to emit document ingest telemetry:", {
				error: error instanceof Error ? error.message : String(error),
				sourceId: args.source.id,
			});
		}
	};
}
