"use strict";
// ---------------------------------------------------------------------------
// Escalation SQL – externalized from crud.ts, bulk.ts, queries.ts
// ---------------------------------------------------------------------------
Object.defineProperty(exports, "__esModule", { value: true });
exports.LIST_DISTINCT_TYPES = exports.ENRICH_ESCALATION_ROUTING = exports.UPDATE_ESCALATION_METADATA = exports.BULK_RESOLVE_FOR_TRIAGE = exports.BULK_ESCALATE_TO_ROLE = exports.BULK_ASSIGN = exports.BULK_CLAIM = exports.GET_ESCALATIONS_BY_ORIGIN_ID = exports.GET_ESCALATIONS_BY_WORKFLOW_ID = exports.GET_ESCALATIONS_BY_TASK_ID = exports.GET_ESCALATION = exports.ESCALATE_TO_ROLE = exports.RELEASE_EXPIRED_CLAIMS = exports.RELEASE_ESCALATION = exports.GET_ESCALATION_ROLES = exports.UPDATE_ESCALATIONS_PRIORITY = exports.RESOLVE_ESCALATION = exports.CLAIM_ESCALATION = exports.CREATE_ESCALATION = exports.ENSURE_ROLE_EXISTS = void 0;
// --- Role management -------------------------------------------------------
exports.ENSURE_ROLE_EXISTS = 'INSERT INTO lt_roles (role) VALUES ($1) ON CONFLICT DO NOTHING';
// --- Single-record CRUD ---------------------------------------------------
exports.CREATE_ESCALATION = `\
INSERT INTO lt_escalations
  (type, subtype, description, priority, task_id,
   origin_id, parent_id, role, envelope, metadata, escalation_payload,
   workflow_id, task_queue, workflow_type, trace_id, span_id)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16)
RETURNING *`;
exports.CLAIM_ESCALATION = `\
WITH prev AS (
  SELECT assigned_to, assigned_until
  FROM lt_escalations
  WHERE id = $1
),
updated AS (
  UPDATE lt_escalations
  SET assigned_to = $2,
      claimed_at = NOW(),
      assigned_until = NOW() + INTERVAL '1 minute' * $3
  WHERE id = $1
    AND status = 'pending'
    AND (
      assigned_to IS NULL
      OR assigned_until <= NOW()
      OR assigned_to = $2
    )
  RETURNING *
)
SELECT updated.*,
       prev.assigned_to AS prev_assigned_to
FROM updated
CROSS JOIN prev`;
exports.RESOLVE_ESCALATION = `\
UPDATE lt_escalations
SET status = 'resolved',
    resolved_at = NOW(),
    resolver_payload = $2
WHERE id = $1
  AND status = 'pending'
RETURNING *`;
exports.UPDATE_ESCALATIONS_PRIORITY = `\
UPDATE lt_escalations
SET priority = $1, updated_at = NOW()
WHERE id = ANY($2::uuid[])
  AND status = 'pending'`;
exports.GET_ESCALATION_ROLES = 'SELECT DISTINCT role FROM lt_escalations WHERE id = ANY($1::uuid[])';
exports.RELEASE_ESCALATION = `\
UPDATE lt_escalations
SET assigned_to = NULL,
    assigned_until = NULL,
    claimed_at = NULL
WHERE id = $1
  AND status = 'pending'
  AND assigned_to = $2
RETURNING *`;
exports.RELEASE_EXPIRED_CLAIMS = `\
UPDATE lt_escalations
SET assigned_to = NULL,
    assigned_until = NULL,
    claimed_at = NULL
WHERE status = 'pending'
  AND assigned_to IS NOT NULL
  AND assigned_until < NOW()`;
exports.ESCALATE_TO_ROLE = `\
UPDATE lt_escalations
SET role = $2,
    assigned_to = NULL,
    assigned_until = NULL,
    claimed_at = NULL,
    updated_at = NOW()
WHERE id = $1
  AND status = 'pending'
RETURNING *`;
exports.GET_ESCALATION = 'SELECT * FROM lt_escalations WHERE id = $1';
exports.GET_ESCALATIONS_BY_TASK_ID = 'SELECT * FROM lt_escalations WHERE task_id = $1 ORDER BY created_at DESC';
exports.GET_ESCALATIONS_BY_WORKFLOW_ID = 'SELECT * FROM lt_escalations WHERE workflow_id = $1 ORDER BY created_at DESC';
exports.GET_ESCALATIONS_BY_ORIGIN_ID = 'SELECT * FROM lt_escalations WHERE origin_id = $1 ORDER BY created_at DESC';
// --- Bulk operations -------------------------------------------------------
/** Used by both bulkClaimEscalations and bulkAssignEscalations (identical SQL). */
exports.BULK_CLAIM = `\
UPDATE lt_escalations
SET assigned_to = $1,
    claimed_at = NOW(),
    assigned_until = NOW() + INTERVAL '1 minute' * $2
WHERE id = ANY($3::uuid[])
  AND status = 'pending'
  AND (
    assigned_to IS NULL
    OR assigned_until <= NOW()
    OR assigned_to = $1
  )`;
exports.BULK_ASSIGN = exports.BULK_CLAIM;
exports.BULK_ESCALATE_TO_ROLE = `\
UPDATE lt_escalations
SET role = $1,
    assigned_to = NULL,
    assigned_until = NULL,
    claimed_at = NULL,
    updated_at = NOW()
WHERE id = ANY($2::uuid[])
  AND status = 'pending'`;
exports.BULK_RESOLVE_FOR_TRIAGE = `\
UPDATE lt_escalations
SET status = 'resolved',
    resolved_at = NOW(),
    resolver_payload = $1
WHERE id = ANY($2::uuid[])
  AND status = 'pending'
RETURNING *`;
// --- Query helpers ---------------------------------------------------------
exports.UPDATE_ESCALATION_METADATA = `\
UPDATE lt_escalations
SET metadata = COALESCE(metadata, '{}'::jsonb) || $2::jsonb,
    updated_at = NOW()
WHERE id = $1
RETURNING *`;
exports.ENRICH_ESCALATION_ROUTING = `\
UPDATE lt_escalations
SET metadata = COALESCE(metadata, '{}'::jsonb) || $2::jsonb,
    workflow_type = COALESCE(workflow_type, $3),
    workflow_id = COALESCE(workflow_id, $4),
    task_queue = COALESCE(task_queue, $5),
    task_id = COALESCE(task_id, $6),
    updated_at = NOW()
WHERE id = $1
RETURNING *`;
exports.LIST_DISTINCT_TYPES = 'SELECT DISTINCT type FROM lt_escalations ORDER BY type';
