import { readKitSqliteUserVersion } from "../../core/state/workspace-kit-sqlite.js";
export const IDEAS_KIT_MIN_USER_VERSION = 29;
export function assertIdeasKitSchema(dbPathAbs) {
    const userVersion = readKitSqliteUserVersion(dbPathAbs);
    if (userVersion < IDEAS_KIT_MIN_USER_VERSION) {
        return {
            ok: false,
            message: `ideas commands require kit SQLite user_version >= ${IDEAS_KIT_MIN_USER_VERSION} (current ${userVersion}); open the workspace DB once with a current workspace-kit to migrate`
        };
    }
    return { ok: true };
}
export function isIdeaId(raw) {
    return /^I[0-9]+$/.test(raw);
}
export function parseIdeaStatus(raw) {
    return raw === "open" || raw === "planning" || raw === "planned" ? raw : undefined;
}
export function readStringArray(raw) {
    try {
        const parsed = JSON.parse(raw);
        return Array.isArray(parsed) ? parsed.filter((value) => typeof value === "string") : [];
    }
    catch {
        return [];
    }
}
export function toIdeaRecord(row) {
    return {
        id: row.id,
        title: row.title,
        ...(row.note ? { note: row.note } : {}),
        status: row.status,
        sortOrder: row.sort_order,
        ...(row.linked_plan_artifact ? { linkedPlanArtifact: row.linked_plan_artifact } : {}),
        previousPlanArtifacts: readStringArray(row.previous_plan_artifacts_json),
        createdAt: row.created_at,
        updatedAt: row.updated_at
    };
}
export function allocateNextIdeaId(db) {
    const rows = db.prepare("SELECT id FROM workflow_ideas WHERE id GLOB 'I[0-9]*'").all();
    const max = rows.reduce((acc, row) => {
        const n = Number(row.id.slice(1));
        return Number.isInteger(n) && n > acc ? n : acc;
    }, 0);
    return `I${String(max + 1).padStart(3, "0")}`;
}
export function nextIdeaSortOrder(db) {
    const row = db.prepare("SELECT COALESCE(MAX(sort_order), -1) + 1 AS next_order FROM workflow_ideas").get();
    return row?.next_order ?? 0;
}
export function createIdea(db, input, nowIso) {
    const id = allocateNextIdeaId(db);
    const sortOrder = nextIdeaSortOrder(db);
    const status = input.status ?? "open";
    const previousPlanArtifacts = input.previousPlanArtifacts ?? [];
    db.prepare(`INSERT INTO workflow_ideas (
       id, title, note, status, sort_order, linked_plan_artifact,
       previous_plan_artifacts_json, created_at, updated_at
     ) VALUES (
       @id, @title, @note, @status, @sort_order, @linked_plan_artifact,
       @previous_plan_artifacts_json, @created_at, @updated_at
     )`).run({
        id,
        title: input.title,
        note: input.note ?? null,
        status,
        sort_order: sortOrder,
        linked_plan_artifact: input.linkedPlanArtifact ?? null,
        previous_plan_artifacts_json: JSON.stringify(previousPlanArtifacts),
        created_at: nowIso,
        updated_at: nowIso
    });
    return {
        id,
        title: input.title,
        ...(input.note ? { note: input.note } : {}),
        status,
        sortOrder,
        ...(input.linkedPlanArtifact ? { linkedPlanArtifact: input.linkedPlanArtifact } : {}),
        previousPlanArtifacts,
        createdAt: nowIso,
        updatedAt: nowIso
    };
}
export function getIdea(db, ideaId) {
    const row = db.prepare("SELECT * FROM workflow_ideas WHERE id = ?").get(ideaId);
    return row ? toIdeaRecord(row) : null;
}
export function listIdeas(db, status) {
    const rows = status
        ? db
            .prepare("SELECT * FROM workflow_ideas WHERE status = ? ORDER BY sort_order ASC, id ASC")
            .all(status)
        : db.prepare("SELECT * FROM workflow_ideas ORDER BY sort_order ASC, id ASC").all();
    return rows.map(toIdeaRecord);
}
export function updateIdea(db, ideaId, input, nowIso) {
    const existing = getIdea(db, ideaId);
    if (!existing) {
        return null;
    }
    db.prepare(`UPDATE workflow_ideas
        SET title = @title,
            note = @note,
            status = @status,
            linked_plan_artifact = @linked_plan_artifact,
            previous_plan_artifacts_json = @previous_plan_artifacts_json,
            updated_at = @updated_at
      WHERE id = @id`).run({
        id: ideaId,
        title: input.title ?? existing.title,
        note: input.note === undefined ? existing.note ?? null : input.note,
        status: input.status ?? existing.status,
        linked_plan_artifact: input.linkedPlanArtifact === undefined ? existing.linkedPlanArtifact ?? null : input.linkedPlanArtifact,
        previous_plan_artifacts_json: JSON.stringify(input.previousPlanArtifacts ?? existing.previousPlanArtifacts),
        updated_at: nowIso
    });
    return getIdea(db, ideaId);
}
export function deleteIdea(db, ideaId) {
    const existing = getIdea(db, ideaId);
    if (!existing) {
        return null;
    }
    db.prepare("DELETE FROM workflow_ideas WHERE id = ?").run(ideaId);
    return existing;
}
export function reorderIdeas(db, ideaIds, nowIso) {
    const current = listIdeas(db);
    const currentIds = current.map((idea) => idea.id);
    if (ideaIds.length !== currentIds.length) {
        return null;
    }
    const currentSet = new Set(currentIds);
    const nextSet = new Set(ideaIds);
    if (nextSet.size !== ideaIds.length || currentIds.some((id) => !nextSet.has(id)) || ideaIds.some((id) => !currentSet.has(id))) {
        return null;
    }
    const update = db.prepare("UPDATE workflow_ideas SET sort_order = ?, updated_at = ? WHERE id = ?");
    db.transaction(() => {
        ideaIds.forEach((id, index) => update.run(index, nowIso, id));
    })();
    return listIdeas(db);
}
