import { workAppSlackMcpToolAccessConfig, workAppSlackUserMappings } from "../../db/runtime/runtime-schema.js";
import { tenantScopedWhere, toolScopedWhere } from "../manage/scope-helpers.js";
import { eq } from "drizzle-orm";

//#region src/data-access/runtime/slack-work-app-mcp.ts
const getSlackMcpToolAccessConfig = (db) => async (scope) => {
	return (await db.select({
		channelAccessMode: workAppSlackMcpToolAccessConfig.channelAccessMode,
		dmEnabled: workAppSlackMcpToolAccessConfig.dmEnabled,
		channelIds: workAppSlackMcpToolAccessConfig.channelIds
	}).from(workAppSlackMcpToolAccessConfig).where(toolScopedWhere(workAppSlackMcpToolAccessConfig, scope)).limit(1))[0] ?? {
		channelAccessMode: "selected",
		dmEnabled: false,
		channelIds: []
	};
};
const setSlackMcpToolAccessConfig = (db) => async (params) => {
	const now = (/* @__PURE__ */ new Date()).toISOString();
	await db.insert(workAppSlackMcpToolAccessConfig).values({
		toolId: params.toolId,
		tenantId: params.tenantId,
		projectId: params.projectId,
		channelAccessMode: params.channelAccessMode,
		dmEnabled: params.dmEnabled,
		channelIds: params.channelIds,
		createdAt: now,
		updatedAt: now
	}).onConflictDoUpdate({
		target: [
			workAppSlackMcpToolAccessConfig.tenantId,
			workAppSlackMcpToolAccessConfig.projectId,
			workAppSlackMcpToolAccessConfig.toolId
		],
		set: {
			channelAccessMode: params.channelAccessMode,
			dmEnabled: params.dmEnabled,
			channelIds: params.channelIds,
			updatedAt: now
		}
	});
};
const updateSlackMcpToolAccessChannelIds = (db) => async (scope, channelIds) => {
	await db.update(workAppSlackMcpToolAccessConfig).set({
		channelIds,
		updatedAt: (/* @__PURE__ */ new Date()).toISOString()
	}).where(toolScopedWhere(workAppSlackMcpToolAccessConfig, scope));
};
const deleteSlackMcpToolAccessConfig = (db) => async (scope) => {
	return (await db.delete(workAppSlackMcpToolAccessConfig).where(toolScopedWhere(workAppSlackMcpToolAccessConfig, scope)).returning()).length > 0;
};
const deleteAllSlackMcpToolAccessConfigsByTenant = (db) => async (tenantId) => {
	return (await db.delete(workAppSlackMcpToolAccessConfig).where(tenantScopedWhere(workAppSlackMcpToolAccessConfig, { tenantId })).returning()).length;
};
const isSlackWorkAppTool = (tool) => {
	return tool.isWorkApp && tool.config.mcp.server.url.includes("/slack/mcp");
};
const resolveSlackUserContext = (db) => async (inkeepUserId) => {
	const mappings = await db.select({
		slackUserId: workAppSlackUserMappings.slackUserId,
		slackUsername: workAppSlackUserMappings.slackUsername
	}).from(workAppSlackUserMappings).where(eq(workAppSlackUserMappings.inkeepUserId, inkeepUserId)).limit(1);
	if (mappings.length === 0) return void 0;
	const mapping = mappings[0];
	const parts = [`The current user's Slack user ID is ${mapping.slackUserId}.`];
	if (mapping.slackUsername) parts.push(`Their Slack username is @${mapping.slackUsername}.`);
	return parts.join(" ");
};

//#endregion
export { deleteAllSlackMcpToolAccessConfigsByTenant, deleteSlackMcpToolAccessConfig, getSlackMcpToolAccessConfig, isSlackWorkAppTool, resolveSlackUserContext, setSlackMcpToolAccessConfig, updateSlackMcpToolAccessChannelIds };