/**
 * id rules: user_id / project_id / agent_id are semantic ids
 * chosen by their creator at creation time — starting with a lowercase letter,
 * containing only lowercase letters, digits, and underscores. The id doubles as the
 * directory name, so keeping it all-lowercase avoids directory name collisions on
 * case-insensitive filesystems (e.g. macOS) at the source.
 * The hyphen is a **reserved separator**: it only appears at the join point of a
 * non-admin project_id's "<username>-<suffix>" concatenation. Usernames never
 * contain a hyphen, so the first hyphen is the ownership boundary — no username can
 * ever be crafted to collide with another user's prefix, keeping namespaces
 * non-overlapping. session_id and temporary workspace ids are still generated by
 * the server (randomHex8).
 */
import { randomBytes } from "node:crypto";

/** 8-character lowercase hex random string (used for server-generated ids like session_id / temporary workspace). */
export function randomHex8(): string {
  return randomBytes(4).toString("hex");
}

/** General semantic id rule: starts with a lowercase letter, followed by lowercase letters, digits, or underscores only, 2-64 chars (no hyphen). */
export const SEMANTIC_ID_PATTERN = /^[a-z][a-z0-9_]{1,63}$/;

/** Username tightens the general rule to 2-32 chars: leaves headroom for the default Project id `<username>-default_project`. */
export const USERNAME_PATTERN = /^[a-z][a-z0-9_]{1,31}$/;

/** Suffix segment of a non-admin project_id (after `<username>-`): lowercase letters, digits, and underscores only. */
export const PROJECT_SUFFIX_PATTERN = /^[a-z0-9_]+$/;

/** Upper bound on total project_id length (username <=32 + separator + suffix). */
export const PROJECT_ID_MAX_LENGTH = 64;

/** Human-readable description of the rule (reused in error messages). */
export const SEMANTIC_ID_RULE =
  "starts with a lowercase letter; lowercase letters, digits and underscores only";
