import { SCOPE_KEYS } from "../../db/manage/scope-definitions.js";
import { and, eq } from "drizzle-orm";

//#region src/data-access/manage/scope-helpers.ts
/**
* Build a WHERE clause that filters by all scope columns for the given level.
*
* The columns and config shape are both derived from `SCOPE_KEYS` in
* `scope-definitions.ts`, so they cannot drift apart.
*/
function scopedWhere(level, table, scopes) {
	const conditions = SCOPE_KEYS[level].map((key) => eq(table[key], scopes[key]));
	return conditions.length === 1 ? conditions[0] : and(...conditions);
}
const tenantScopedWhere = (table, scopes) => scopedWhere("tenant", table, scopes);
const projectScopedWhere = (table, scopes) => scopedWhere("project", table, scopes);
const agentScopedWhere = (table, scopes) => scopedWhere("agent", table, scopes);
const subAgentScopedWhere = (table, scopes) => scopedWhere("subAgent", table, scopes);
const toolScopedWhere = (table, scopes) => scopedWhere("tool", table, scopes);

//#endregion
export { agentScopedWhere, projectScopedWhere, scopedWhere, subAgentScopedWhere, tenantScopedWhere, toolScopedWhere };