"use strict";
// ─── Escalation chain queries ───────────────────────────────────────────────
Object.defineProperty(exports, "__esModule", { value: true });
exports.COUNT_ACTIVE_ESCALATION_REFS = exports.COUNT_WORKFLOW_REFS = exports.COUNT_CHAIN_REFS = exports.COUNT_USER_ROLE_REFS = exports.LIST_ROLES_WITH_DETAILS = exports.DELETE_ROLE = exports.LIST_ROLES = exports.ENSURE_ROLE_EXISTS = exports.CHECK_ESCALATION_CHAIN_EXISTS = exports.DELETE_ESCALATION_CHAINS_BY_SOURCE = exports.DELETE_ESCALATION_CHAIN = exports.INSERT_ESCALATION_CHAIN = exports.GET_ALL_ESCALATION_CHAINS = exports.GET_ESCALATION_TARGETS = void 0;
exports.GET_ESCALATION_TARGETS = `
  SELECT target_role
  FROM lt_config_role_escalations
  WHERE source_role = $1
  ORDER BY target_role`;
exports.GET_ALL_ESCALATION_CHAINS = `
  SELECT source_role, target_role
  FROM lt_config_role_escalations
  ORDER BY source_role, target_role`;
exports.INSERT_ESCALATION_CHAIN = `
  INSERT INTO lt_config_role_escalations (source_role, target_role)
  VALUES ($1, $2)
  ON CONFLICT DO NOTHING`;
exports.DELETE_ESCALATION_CHAIN = `
  DELETE FROM lt_config_role_escalations
  WHERE source_role = $1 AND target_role = $2`;
exports.DELETE_ESCALATION_CHAINS_BY_SOURCE = `
  DELETE FROM lt_config_role_escalations
  WHERE source_role = $1`;
exports.CHECK_ESCALATION_CHAIN_EXISTS = `
  SELECT 1
  FROM lt_config_role_escalations
  WHERE source_role = $1 AND target_role = $2`;
// ─── Role CRUD ──────────────────────────────────────────────────────────────
exports.ENSURE_ROLE_EXISTS = `
  INSERT INTO lt_roles (role) VALUES ($1)
  ON CONFLICT DO NOTHING`;
exports.LIST_ROLES = `
  SELECT role FROM lt_roles ORDER BY role`;
exports.DELETE_ROLE = `
  DELETE FROM lt_roles WHERE role = $1`;
// ─── Role detail aggregation ────────────────────────────────────────────────
exports.LIST_ROLES_WITH_DETAILS = `
  WITH
  user_counts AS (
    SELECT role, COUNT(DISTINCT user_id)::int AS cnt
    FROM lt_user_roles
    GROUP BY role
  ),
  chain_counts AS (
    SELECT role, COUNT(*)::int AS cnt
    FROM (
      SELECT source_role AS role FROM lt_config_role_escalations
      UNION ALL
      SELECT target_role AS role FROM lt_config_role_escalations
    ) c
    GROUP BY role
  ),
  workflow_counts AS (
    SELECT role, COUNT(*)::int AS cnt
    FROM lt_config_roles
    GROUP BY role
  )
  SELECT
    r.role,
    COALESCE(uc.cnt, 0) AS user_count,
    COALESCE(cc.cnt, 0) AS chain_count,
    COALESCE(wc.cnt, 0) AS workflow_count
  FROM lt_roles r
  LEFT JOIN user_counts uc ON uc.role = r.role
  LEFT JOIN chain_counts cc ON cc.role = r.role
  LEFT JOIN workflow_counts wc ON wc.role = r.role
  ORDER BY r.role`;
// ─── Reference checks (used before role deletion) ──────────────────────────
exports.COUNT_USER_ROLE_REFS = `
  SELECT COUNT(*)::int AS cnt FROM lt_user_roles WHERE role = $1`;
exports.COUNT_CHAIN_REFS = `
  SELECT COUNT(*)::int AS cnt
  FROM lt_config_role_escalations
  WHERE source_role = $1 OR target_role = $1`;
exports.COUNT_WORKFLOW_REFS = `
  SELECT COUNT(*)::int AS cnt FROM lt_config_roles WHERE role = $1`;
exports.COUNT_ACTIVE_ESCALATION_REFS = `
  SELECT COUNT(*)::int AS cnt
  FROM lt_escalations
  WHERE role = $1 AND status IN ('pending', 'claimed')`;
