"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.listEntities = listEntities;
exports.listJobs = listJobs;
exports.getJobExecution = getJobExecution;
exports.interruptJob = interruptJob;
const hotmesh_utils_1 = require("../services/hotmesh-utils");
const pipelines_1 = require("../services/pipelines");
const controlplane_1 = require("../services/controlplane");
/**
 * List distinct entity types for an app (HotMesh namespace).
 *
 * @param input.app_id — HotMesh app ID (e.g. `"durable"`)
 * @returns `{ status: 200, data: { entities: string[] } }`
 */
async function listEntities(input) {
    try {
        if (!input.app_id) {
            return { status: 400, error: 'app_id query parameter is required' };
        }
        const entities = await (0, pipelines_1.listEntities)(input.app_id);
        return { status: 200, data: { entities } };
    }
    catch (err) {
        if (err.message?.includes('does not exist')) {
            return { status: 200, data: { entities: [] } };
        }
        return { status: 500, error: err.message };
    }
}
/**
 * List jobs (workflow executions) for an app, with optional filters.
 *
 * @param input.app_id — HotMesh app ID
 * @param input.limit — max results
 * @param input.offset — pagination offset
 * @param input.entity — filter by entity type
 * @param input.search — full-text search
 * @param input.status — filter by job status
 * @returns `{ status: 200, data: { jobs, total } }`
 */
async function listJobs(input) {
    try {
        if (!input.app_id) {
            return { status: 400, error: 'app_id query parameter is required' };
        }
        const result = await (0, pipelines_1.listJobs)({
            rawAppId: input.app_id,
            limit: input.limit,
            offset: input.offset,
            entity: input.entity,
            search: input.search,
            status: input.status,
            sort_by: input.sort_by,
            order: input.order,
        });
        return { status: 200, data: result };
    }
    catch (err) {
        if (err.message?.includes('does not exist')) {
            return { status: 200, data: { jobs: [], total: 0 } };
        }
        return { status: 500, error: err.message };
    }
}
/**
 * Get the full execution history for a single job.
 *
 * Reconstructs the execution timeline from the HotMesh stream,
 * including activity calls, signals, and state transitions.
 *
 * @param input.jobId — HotMesh job (workflow) ID
 * @param input.app_id — HotMesh app ID
 * @returns `{ status: 200, data: <execution timeline> }` or 404
 */
async function getJobExecution(input) {
    try {
        if (!input.app_id) {
            return { status: 400, error: 'app_id query parameter is required' };
        }
        const appId = (0, hotmesh_utils_1.sanitizeAppId)(input.app_id);
        const schema = (0, hotmesh_utils_1.quoteSchema)(appId);
        const execution = await (0, pipelines_1.buildExecution)(input.jobId, appId, schema);
        return { status: 200, data: execution };
    }
    catch (err) {
        const msg = err.message ?? '';
        if (err.status === 404 || msg.includes('not found') || msg.includes('does not exist')) {
            return { status: 404, error: msg };
        }
        return { status: 500, error: msg };
    }
}
/**
 * Interrupt a running pipeline job via HotMesh.interrupt().
 *
 * This is the pipeline equivalent of the durable terminate endpoint.
 * Pipelines run as raw HotMesh YAML DAGs, not through the Durable
 * abstraction, so they require the app_id (namespace) and topic
 * to locate the correct engine instance.
 *
 * @param input.jobId — HotMesh job ID (workflow_id)
 * @param input.topic — workflow entity/topic name
 * @param input.app_id — HotMesh namespace (e.g. "hmsh", "longtail")
 * @returns `{ status: 200, data: { interrupted: true } }` or error
 */
async function interruptJob(input) {
    try {
        if (!input.app_id) {
            return { status: 400, error: 'app_id is required' };
        }
        if (!input.topic) {
            return { status: 400, error: 'topic is required' };
        }
        const appId = (0, hotmesh_utils_1.sanitizeAppId)(input.app_id);
        const engine = await (0, controlplane_1.getEngine)(appId);
        await engine.interrupt(input.topic, input.jobId);
        return { status: 200, data: { interrupted: true, jobId: input.jobId } };
    }
    catch (err) {
        return { status: 500, error: err.message };
    }
}
