"use strict";
// ─── YAML workflow CRUD ─────────────────────────────────────────────────────
Object.defineProperty(exports, "__esModule", { value: true });
exports.FIND_BY_TAGS_ALL = exports.FIND_BY_TAGS_ANY = exports.GET_CRON_SCHEDULED_WORKFLOWS = exports.CLEAR_CRON_SCHEDULE = exports.UPDATE_CRON_SCHEDULE = exports.UPDATE_STATUS_SUFFIX = exports.UPDATE_STATUS_BASE = exports.GET_VERSION_SNAPSHOT = exports.DISCOVER_WORKFLOWS = exports.LIST_VERSIONS = exports.COUNT_VERSIONS = exports.CREATE_VERSION_SNAPSHOT = exports.MARK_APP_ID_CONTENT_DEPLOYED = exports.MARK_CONTENT_DEPLOYED = exports.GET_DISTINCT_APP_IDS = exports.GET_MAX_APP_VERSION = exports.LIST_BY_APP_ID = exports.GET_ACTIVE_YAML_WORKFLOWS = exports.DELETE_YAML_WORKFLOW = exports.UPDATE_YAML_WORKFLOW_VERSION = exports.GET_YAML_WORKFLOW_BY_NAME = exports.GET_YAML_WORKFLOW = exports.CHECK_TOPIC_UNIQUE = exports.CREATE_YAML_WORKFLOW = void 0;
exports.CREATE_YAML_WORKFLOW = `
  INSERT INTO lt_yaml_workflows
    (name, description, app_id, app_version, source_workflow_id,
     source_workflow_type, yaml_content, graph_topic,
     input_schema, output_schema, activity_manifest, input_field_meta,
     original_prompt, category, tags, metadata, content_version,
     set_id, set_role, set_build_order)
  VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, 1, $17, $18, $19)
  RETURNING *`;
exports.CHECK_TOPIC_UNIQUE = `
  SELECT id, name FROM lt_yaml_workflows
  WHERE app_id = $1 AND graph_topic = $2 AND status != 'archived'
  LIMIT 1`;
exports.GET_YAML_WORKFLOW = `
  SELECT * FROM lt_yaml_workflows WHERE id = $1`;
exports.GET_YAML_WORKFLOW_BY_NAME = `
  SELECT * FROM lt_yaml_workflows WHERE name = $1`;
exports.UPDATE_YAML_WORKFLOW_VERSION = `
  UPDATE lt_yaml_workflows SET app_version = $2 WHERE id = $1`;
exports.DELETE_YAML_WORKFLOW = `
  DELETE FROM lt_yaml_workflows WHERE id = $1`;
// ─── Status and deployment ──────────────────────────────────────────────────
exports.GET_ACTIVE_YAML_WORKFLOWS = `
  SELECT * FROM lt_yaml_workflows
  WHERE status = 'active'
  ORDER BY name`;
exports.LIST_BY_APP_ID = `
  SELECT * FROM lt_yaml_workflows
  WHERE app_id = $1 AND status != 'archived'
  ORDER BY name`;
exports.GET_MAX_APP_VERSION = `
  SELECT COALESCE(MAX(CAST(app_version AS INTEGER)), 0) AS max_version
  FROM lt_yaml_workflows
  WHERE app_id = $1 AND status != 'archived'`;
exports.GET_DISTINCT_APP_IDS = `
  SELECT DISTINCT app_id FROM (
    SELECT app_id FROM lt_yaml_workflows WHERE status != 'archived'
    UNION
    SELECT app_id FROM hmsh_applications WHERE active = true
  ) all_apps
  ORDER BY app_id`;
exports.MARK_CONTENT_DEPLOYED = `
  UPDATE lt_yaml_workflows
  SET deployed_content_version = content_version
  WHERE id = $1`;
exports.MARK_APP_ID_CONTENT_DEPLOYED = `
  UPDATE lt_yaml_workflows
  SET deployed_content_version = content_version
  WHERE app_id = $1 AND status != 'archived'`;
// ─── Version history ────────────────────────────────────────────────────────
exports.CREATE_VERSION_SNAPSHOT = `
  INSERT INTO lt_yaml_workflow_versions
    (workflow_id, version, yaml_content, activity_manifest, input_schema, output_schema, input_field_meta, change_summary)
  VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
  ON CONFLICT (workflow_id, version) DO NOTHING
  RETURNING *`;
exports.COUNT_VERSIONS = `
  SELECT COUNT(*) FROM lt_yaml_workflow_versions WHERE workflow_id = $1`;
exports.LIST_VERSIONS = `
  SELECT * FROM lt_yaml_workflow_versions
  WHERE workflow_id = $1
  ORDER BY version DESC
  LIMIT $2 OFFSET $3`;
// ─── Discovery ─────────────────────────────────────────────────────────────
exports.DISCOVER_WORKFLOWS = `
  SELECT *,
    ts_rank_cd(search_vector, plainto_tsquery('english', $1), 32) AS fts_rank
  FROM lt_yaml_workflows
  WHERE status = 'active'
    AND (
      search_vector @@ plainto_tsquery('english', $1)
      OR tags && $2::text[]
      OR ($3::text IS NOT NULL AND category = $3)
    )
  ORDER BY fts_rank DESC, activated_at DESC NULLS LAST
  LIMIT $4`;
exports.GET_VERSION_SNAPSHOT = `
  SELECT * FROM lt_yaml_workflow_versions
  WHERE workflow_id = $1 AND version = $2`;
// ─── Status updates ─────────────────────────────────────────────────────────
exports.UPDATE_STATUS_BASE = `UPDATE lt_yaml_workflows SET status = $2`;
exports.UPDATE_STATUS_SUFFIX = ` WHERE id = $1 RETURNING *`;
// ─── Cron scheduling ────────────────────────────────────────────────────────
exports.UPDATE_CRON_SCHEDULE = `
  UPDATE lt_yaml_workflows
  SET cron_schedule = $2, cron_envelope = $3, execute_as = $4
  WHERE id = $1
  RETURNING *`;
exports.CLEAR_CRON_SCHEDULE = `
  UPDATE lt_yaml_workflows
  SET cron_schedule = NULL, cron_envelope = NULL, execute_as = NULL
  WHERE id = $1
  RETURNING *`;
exports.GET_CRON_SCHEDULED_WORKFLOWS = `
  SELECT * FROM lt_yaml_workflows
  WHERE cron_schedule IS NOT NULL AND status = 'active'
  ORDER BY name`;
// ─── Tag-based lookup ────────────────────────────────────────────────────────
exports.FIND_BY_TAGS_ANY = `
  SELECT * FROM lt_yaml_workflows
  WHERE status = 'active' AND tags && $1::text[]
  ORDER BY name`;
exports.FIND_BY_TAGS_ALL = `
  SELECT * FROM lt_yaml_workflows
  WHERE status = 'active' AND tags @> $1::text[]
  ORDER BY name`;
