/**
 * Deployment identity: a stable id + human-readable name for THIS botmux
 * deployment (one install = one owner). Used by federation so a hub can tell
 * member deployments apart and group their bots in the shared roster.
 *
 * Generated once and persisted to `{dataDir}/deployment-identity.json`; the name
 * defaults to the machine hostname and can be renamed by the owner.
 */
import { readFileSync, writeFileSync, existsSync, mkdirSync, renameSync } from 'node:fs';
import { join } from 'node:path';
import { randomUUID } from 'node:crypto';
import { hostname } from 'node:os';
function filePath(dataDir) {
    return join(dataDir, 'deployment-identity.json');
}
function write(dataDir, id) {
    if (!existsSync(dataDir))
        mkdirSync(dataDir, { recursive: true });
    const fp = filePath(dataDir);
    const tmp = `${fp}.${process.pid}.${randomUUID()}.tmp`;
    writeFileSync(tmp, JSON.stringify(id, null, 2) + '\n', 'utf-8');
    renameSync(tmp, fp);
}
/** Load (creating + persisting on first call) this deployment's identity. */
export function getDeploymentIdentity(dataDir) {
    const fp = filePath(dataDir);
    if (existsSync(fp)) {
        try {
            const p = JSON.parse(readFileSync(fp, 'utf-8'));
            if (p && typeof p.deploymentId === 'string' && p.deploymentId) {
                return {
                    deploymentId: p.deploymentId,
                    name: typeof p.name === 'string' && p.name ? p.name : 'botmux',
                    ownerUnionId: typeof p.ownerUnionId === 'string' ? p.ownerUnionId : undefined,
                    ownerName: typeof p.ownerName === 'string' ? p.ownerName : undefined,
                };
            }
        }
        catch { /* corrupt — regenerate */ }
    }
    const id = { deploymentId: `dep_${randomUUID().slice(0, 12)}`, name: hostname() || 'botmux' };
    write(dataDir, id);
    return id;
}
/** Rename this deployment (owner-facing label). Returns the updated identity. */
export function setDeploymentName(dataDir, name) {
    const cur = getDeploymentIdentity(dataDir);
    const next = { ...cur, name: name.trim() || cur.name };
    write(dataDir, next);
    return next;
}
/** Bind the owner's Feishu identity (via /pair or auto-bind). Also adopts the
 *  owner's Feishu name as the DEPLOYMENT name — the deployment is shown to the
 *  team by its owner's real name (no custom labels), so the roster reads
 *  naturally ("申晗 的部署" instead of a hostname). Returns the updated identity. */
export function setDeploymentOwner(dataDir, owner) {
    const cur = getDeploymentIdentity(dataDir);
    const next = {
        ...cur,
        ownerUnionId: owner.unionId || cur.ownerUnionId,
        ownerName: owner.name || cur.ownerName,
        name: owner.name || cur.name, // default the deployment label to the owner's Feishu name
    };
    write(dataDir, next);
    return next;
}
//# sourceMappingURL=deployment-identity.js.map