import { getLogger } from "../../../logger.js";
import { processWebhook } from "../services/TriggerService.js";
import { OpenAPIHono, createRoute, z } from "@hono/zod-openapi";

//#region src/domains/run/routes/webhooks.ts
const app = new OpenAPIHono();
const logger = getLogger("webhooks");
/**
* Webhook endpoint for trigger invocation
* POST /tenants/{tenantId}/projects/{projectId}/agents/{agentId}/triggers/{triggerId}
*/
const triggerWebhookRoute = createRoute({
	method: "post",
	path: "/tenants/{tenantId}/projects/{projectId}/agents/{agentId}/triggers/{triggerId}",
	tags: ["Webhooks"],
	summary: "Invoke agent via trigger webhook",
	description: "Webhook endpoint for third-party services to invoke an agent via a configured trigger",
	request: {
		params: z.object({
			tenantId: z.string().describe("Tenant ID"),
			projectId: z.string().describe("Project ID"),
			agentId: z.string().describe("Agent ID"),
			triggerId: z.string().describe("Trigger ID")
		}),
		body: { content: { "application/json": { schema: z.record(z.string(), z.unknown()).describe("Webhook payload") } } }
	},
	responses: {
		202: {
			description: "Webhook accepted and trigger invoked",
			content: { "application/json": { schema: z.object({
				success: z.boolean(),
				invocationId: z.string(),
				conversationId: z.string()
			}) } }
		},
		400: {
			description: "Invalid request payload",
			content: { "application/json": { schema: z.object({
				error: z.string(),
				validationErrors: z.array(z.string()).optional()
			}) } }
		},
		401: {
			description: "Missing authentication credentials",
			content: { "application/json": { schema: z.object({ error: z.string() }) } }
		},
		403: {
			description: "Invalid authentication credentials or signature",
			content: { "application/json": { schema: z.object({ error: z.string() }) } }
		},
		404: {
			description: "Trigger not found or disabled",
			content: { "application/json": { schema: z.object({ error: z.string() }) } }
		},
		422: {
			description: "Payload transformation failed",
			content: { "application/json": { schema: z.object({ error: z.string() }) } }
		},
		500: {
			description: "Internal server error",
			content: { "application/json": { schema: z.object({ error: z.string() }) } }
		}
	}
});
app.openapi(triggerWebhookRoute, async (c) => {
	const { tenantId, projectId, agentId, triggerId } = c.req.param();
	const resolvedRef = c.get("resolvedRef");
	logger.info({
		tenantId,
		projectId,
		agentId,
		triggerId
	}, "Processing trigger webhook");
	const result = await processWebhook({
		tenantId,
		projectId,
		agentId,
		triggerId,
		resolvedRef,
		rawBody: await c.req.text(),
		honoContext: c
	});
	if (!result.success) {
		if (result.validationErrors) return c.json({
			error: result.error,
			validationErrors: result.validationErrors
		}, result.status);
		return c.json({ error: result.error }, result.status);
	}
	logger.info({
		tenantId,
		projectId,
		agentId,
		triggerId,
		invocationId: result.invocationId,
		conversationId: result.conversationId
	}, "Trigger webhook accepted, workflow dispatched");
	return c.json({
		success: true,
		invocationId: result.invocationId,
		conversationId: result.conversationId
	}, 202);
});
var webhooks_default = app;

//#endregion
export { webhooks_default as default };