import { existsSync } from "node:fs";
import fs from "node:fs/promises";
import { displayTodoId, isTodoClosed, validateTodoId } from "./todos-model.js";
import { getTodoPath } from "./todos-paths.js";
import { withTodoLock } from "./todos-locks.js";
import { ensureTodoExists, writeTodoFile } from "./todos-storage.js";
export async function claimTodoAssignment(todosDir, id, ctx, force = false) {
    const validated = validateTodoId(id);
    if ("error" in validated) {
        return { error: validated.error };
    }
    const normalizedId = validated.id;
    const filePath = getTodoPath(todosDir, normalizedId);
    if (!existsSync(filePath)) {
        return { error: `Todo ${displayTodoId(id)} not found` };
    }
    const sessionId = ctx.sessionManager.getSessionId();
    return withTodoLock(todosDir, normalizedId, ctx, async () => {
        const existing = await ensureTodoExists(filePath, normalizedId);
        if (!existing)
            return { error: `Todo ${displayTodoId(id)} not found` };
        if (isTodoClosed(existing.status)) {
            return { error: `Todo ${displayTodoId(id)} is closed` };
        }
        const assigned = existing.assigned_to_session;
        if (assigned && assigned !== sessionId && !force) {
            return {
                error: `Todo ${displayTodoId(id)} is already assigned to session ${assigned}. Use force to override.`,
            };
        }
        if (assigned !== sessionId) {
            existing.assigned_to_session = sessionId;
            await writeTodoFile(filePath, existing);
        }
        return existing;
    });
}
export async function releaseTodoAssignment(todosDir, id, ctx, force = false) {
    const validated = validateTodoId(id);
    if ("error" in validated) {
        return { error: validated.error };
    }
    const normalizedId = validated.id;
    const filePath = getTodoPath(todosDir, normalizedId);
    if (!existsSync(filePath)) {
        return { error: `Todo ${displayTodoId(id)} not found` };
    }
    const sessionId = ctx.sessionManager.getSessionId();
    return withTodoLock(todosDir, normalizedId, ctx, async () => {
        const existing = await ensureTodoExists(filePath, normalizedId);
        if (!existing)
            return { error: `Todo ${displayTodoId(id)} not found` };
        const assigned = existing.assigned_to_session;
        if (!assigned) {
            return existing;
        }
        if (assigned !== sessionId && !force) {
            return {
                error: `Todo ${displayTodoId(id)} is assigned to session ${assigned}. Use force to release.`,
            };
        }
        existing.assigned_to_session = undefined;
        await writeTodoFile(filePath, existing);
        return existing;
    });
}
export async function deleteTodo(todosDir, id, ctx) {
    const validated = validateTodoId(id);
    if ("error" in validated) {
        return { error: validated.error };
    }
    const normalizedId = validated.id;
    const filePath = getTodoPath(todosDir, normalizedId);
    if (!existsSync(filePath)) {
        return { error: `Todo ${displayTodoId(id)} not found` };
    }
    return withTodoLock(todosDir, normalizedId, ctx, async () => {
        const existing = await ensureTodoExists(filePath, normalizedId);
        if (!existing)
            return { error: `Todo ${displayTodoId(id)} not found` };
        await fs.unlink(filePath);
        return existing;
    });
}
//# sourceMappingURL=todos-mutations.js.map