/**
 * Workforce module entry point.
 *
 * Exposes a singleton WorkforceRuntime initialized lazily per workspace root.
 * All MCP tools go through getWorkforce(root).
 */
import path from 'path';
import { openWorkforceDb } from './db/client.js';
import { registerWorker, getWorker, listWorkers, listCompanies, heartbeat, retireWorker, markStaleWorkers, recordSpend, adjustScore } from './workers.js';
import { enqueueJob, getJob, listJobs, claimNextReady, advanceToWip, advanceToReview, recordTokens, completeJob, failJob, blockJob, cancelJob } from './jobs.js';
import { claimJob, renewClaim, releaseClaim, reclaimExpiredLocks } from './claim-lock.js';
import { getKanbanBoard, getLaneJobs } from './kanban-view.js';
import { reconcileDag } from './dag.js';
export { registerWorker, getWorker, listWorkers, listCompanies, heartbeat, retireWorker, markStaleWorkers, recordSpend, adjustScore };
export { enqueueJob, getJob, listJobs, claimNextReady, advanceToWip, advanceToReview, recordTokens, completeJob, failJob, blockJob, cancelJob };
export { claimJob, renewClaim, releaseClaim, reclaimExpiredLocks };
export { getKanbanBoard, getLaneJobs };
export { reconcileDag };
// ─── WorkforceRuntime singleton ───────────────────────────────────────────────
export class WorkforceRuntime {
    db;
    root;
    storageRoot;
    degraded;
    constructor(root) {
        const result = openWorkforceDb(root);
        this.db = result.db;
        this.root = path.resolve(root);
        this.storageRoot = result.paths.storageRoot;
        this.degraded = result.degraded;
    }
    // ── Workers ──────────────────────────────────────────────────────────────
    registerWorker(input) { return registerWorker(this.db, input); }
    getWorker(id) { return getWorker(this.db, id); }
    listWorkers(status, companyId) { return listWorkers(this.db, status, companyId); }
    listCompanies() { return listCompanies(this.db); }
    heartbeat(workerId) { heartbeat(this.db, workerId); }
    retireWorker(workerId) { retireWorker(this.db, workerId); }
    markStaleWorkers(staleSecs) { return markStaleWorkers(this.db, staleSecs); }
    // ── Jobs ─────────────────────────────────────────────────────────────────
    enqueueJob(input) { return enqueueJob(this.db, input); }
    getJob(id) { return getJob(this.db, id); }
    listJobs(status, limit, companyId) { return listJobs(this.db, status, limit, companyId); }
    claimNextReady(workerId, leaseMs) { return claimNextReady(this.db, workerId, leaseMs); }
    claimJob(jobId, workerId, leaseMs) { return claimJob(this.db, jobId, workerId, leaseMs); }
    advanceToWip(jobId, workerId) { advanceToWip(this.db, jobId, workerId); }
    advanceToReview(jobId, workerId) { advanceToReview(this.db, jobId, workerId); }
    recordTokens(jobId, workerId, tokens) { recordTokens(this.db, jobId, workerId, tokens); }
    completeJob(jobId, workerId) { return completeJob(this.db, jobId, workerId); }
    failJob(jobId, workerId) { return failJob(this.db, jobId, workerId); }
    blockJob(jobId, workerId) { return blockJob(this.db, jobId, workerId); }
    cancelJob(jobId) { cancelJob(this.db, jobId); }
    // ── Claims ────────────────────────────────────────────────────────────────
    renewClaim(jobId, workerId, leaseMs) { return renewClaim(this.db, jobId, workerId, leaseMs); }
    releaseClaim(jobId, workerId) { releaseClaim(this.db, jobId, workerId); }
    reclaimExpiredLocks() { return reclaimExpiredLocks(this.db); }
    // ── Kanban ────────────────────────────────────────────────────────────────
    getKanbanBoard(client) { return getKanbanBoard(this.db, client); }
    getLaneJobs(lane) { return getLaneJobs(this.db, lane); }
    // ── DAG ───────────────────────────────────────────────────────────────────
    reconcileDag() { return reconcileDag(this.db); }
}
// ─── Process-level registry ───────────────────────────────────────────────────
const _runtimes = new Map();
export function getWorkforce(root = process.cwd()) {
    const key = path.resolve(root);
    if (!_runtimes.has(key)) {
        _runtimes.set(key, new WorkforceRuntime(key));
    }
    return _runtimes.get(key);
}
