"use strict";
// SQL templates for pipeline queries.
// Schema placeholders (${schema}) are interpolated at call time
// because Postgres does not support parameterized schema names.
Object.defineProperty(exports, "__esModule", { value: true });
exports.LIST_JOBS = exports.GET_JOB_ATTRIBUTES = exports.GET_JOB = exports.COUNT_JOBS = exports.ACTIVE_GRAPH_TOPICS = exports.DISTINCT_ENTITIES = exports.DISTINCT_ENTITIES_DURABLE = void 0;
exports.DISTINCT_ENTITIES_DURABLE = `
  SELECT DISTINCT entity FROM durable.jobs WHERE entity IS NOT NULL AND entity != '' ORDER BY entity`;
const DISTINCT_ENTITIES = (schema) => `SELECT DISTINCT entity FROM ${schema}.jobs WHERE entity IS NOT NULL AND entity != '' ORDER BY entity`;
exports.DISTINCT_ENTITIES = DISTINCT_ENTITIES;
exports.ACTIVE_GRAPH_TOPICS = `SELECT DISTINCT graph_topic FROM lt_yaml_workflows WHERE app_id = $1 AND status IN ('active', 'deployed')`;
const COUNT_JOBS = (schema, where) => `SELECT COUNT(*) FROM ${schema}.jobs j ${where}`;
exports.COUNT_JOBS = COUNT_JOBS;
// ─── Execution builder ──────────────────────────────────────────���───────────
const GET_JOB = (schema) => `SELECT id, key, entity, status, created_at, updated_at, expired_at, is_live
   FROM ${schema}.jobs WHERE key = $1 LIMIT 1`;
exports.GET_JOB = GET_JOB;
const GET_JOB_ATTRIBUTES = (schema) => `SELECT symbol, dimension, value FROM ${schema}.jobs_attributes WHERE job_id = $1 ORDER BY symbol, dimension`;
exports.GET_JOB_ATTRIBUTES = GET_JOB_ATTRIBUTES;
// ─── Queries (queries.ts) ───────────────────────────────────────────────────
const LIST_JOBS = (schema, appId, where, limitIdx, offsetIdx, orderBy) => `WITH ju_symbols AS (
     SELECT value FROM ${schema}.symbols
     WHERE key LIKE 'keys:%' AND field = 'metadata/ju'
   )
   SELECT j.key, j.entity, j.status, j.is_live, j.created_at,
     CASE WHEN j.updated_at != j.created_at THEN j.updated_at
          WHEN ju.value IS NOT NULL THEN to_timestamp(ju.value, 'YYYYMMDDHH24MISS.MS')
          ELSE j.updated_at
     END as updated_at
   FROM ${schema}.jobs j
   LEFT JOIN ${schema}.jobs_attributes ju
     ON ju.job_id = j.id
     AND ju.symbol IN (SELECT value FROM ju_symbols)
     AND (ju.dimension IS NULL OR ju.dimension = '')
   ${where}
   ORDER BY ${orderBy || '(CASE WHEN j.status > 0 THEN 0 ELSE 1 END), j.created_at DESC'}
   LIMIT $${limitIdx} OFFSET $${offsetIdx}`;
exports.LIST_JOBS = LIST_JOBS;
