import { tasks } from "../../db/runtime/runtime-schema.js";
import { projectScopedWhere } from "../manage/scope-helpers.js";
import { and, eq } from "drizzle-orm";

//#region src/data-access/runtime/tasks.ts
const createTask = (db) => async (params) => {
	const now = (/* @__PURE__ */ new Date()).toISOString();
	const [created] = await db.insert(tasks).values({
		...params,
		createdAt: now,
		updatedAt: now
	}).returning();
	return created;
};
const getTask = (db) => async (params) => {
	return (await db.select().from(tasks).where(and(projectScopedWhere(tasks, params.scopes), eq(tasks.id, params.id))).limit(1))[0] ?? null;
};
const updateTask = (db) => async (params) => {
	const now = (/* @__PURE__ */ new Date()).toISOString();
	const [updated] = await db.update(tasks).set({
		...params.data,
		updatedAt: now
	}).where(and(projectScopedWhere(tasks, params.scopes), eq(tasks.id, params.taskId))).returning();
	return updated;
};
const listTaskIdsByContextId = (db) => async (params) => {
	return (await db.select({ id: tasks.id }).from(tasks).where(and(projectScopedWhere(tasks, params.scopes), eq(tasks.contextId, params.contextId)))).map((r) => r.id);
};

//#endregion
export { createTask, getTask, listTaskIdsByContextId, updateTask };