// CLI bridge for shell scripts that need to scaffold a Project + Tasks
// atomically (e.g. premium-plugins/venture-studio/bin/scaffold.sh). The
// scaffold script is the deterministic enforcement layer for the
// venture-studio scaffold-first principle (see Task 286 +
// feedback_doctrine_paragraph_is_not_a_gate); to remain deterministic it
// must invoke a single binary, not narrate intent for the LLM. This CLI
// is that binary.
//
// Contract:
//   node project-create-cli.js '<json-payload>'
//   payload: { accountId, name, description, tier, workItems[], createdBy }
//   exit 0 + stdout JSON { projectId, existing: boolean } on success
//   exit non-zero + literal stderr message on any failure
//
// Idempotency guard lives here, NOT in projectCreate(). projectCreate()
// always mints a fresh UUID — modifying it to MERGE on (accountId, name)
// would change semantics for every existing caller. The CLI runs a
// pre-check MATCH on (accountId, name); if a Project exists, it returns
// the existing projectId with existing=true and skips the write. This
// makes the scaffold script safely re-runnable after a partial failure
// (e.g. graph write succeeds but mkdir fails — second run finds the
// Project and only retries mkdir).
import { projectCreate } from "../tools/project-create.js";
import { getDriver } from "../lib/neo4j.js";
async function findExistingProject(accountId, name) {
    const driver = getDriver();
    const session = driver.session();
    try {
        const result = await session.run(`MATCH (p:Project {accountId: $accountId, name: $name})
       RETURN p.taskId AS projectId LIMIT 1`, { accountId, name });
        if (result.records.length === 0)
            return null;
        return result.records[0].get("projectId");
    }
    finally {
        await session.close();
    }
}
async function main() {
    const raw = process.argv[2];
    if (!raw) {
        process.stderr.write("project-create-cli: missing JSON payload argument\n");
        process.exit(2);
    }
    let payload;
    try {
        payload = JSON.parse(raw);
    }
    catch (err) {
        process.stderr.write(`project-create-cli: malformed JSON payload: ${err.message}\n`);
        process.exit(2);
    }
    if (!payload.accountId || !payload.name) {
        process.stderr.write("project-create-cli: payload missing required field (accountId, name)\n");
        process.exit(2);
    }
    const createdBy = payload.createdBy ?? {
        tool: "project-create-cli",
        source: "scaffold-script",
    };
    const existing = await findExistingProject(payload.accountId, payload.name);
    if (existing) {
        process.stdout.write(JSON.stringify({ projectId: existing, existing: true }) + "\n");
        return;
    }
    const result = await projectCreate({
        ...payload,
        createdBy,
    });
    process.stdout.write(JSON.stringify({ projectId: result.projectId, existing: false, childTaskIds: result.childTaskIds }) + "\n");
}
main()
    .then(() => process.exit(0))
    .catch((err) => {
    process.stderr.write(`project-create-cli: ${err.message}\n`);
    process.exit(1);
});
//# sourceMappingURL=project-create-cli.js.map