import runDbClient_default from "../../../data/db/runDbClient.js";
import { requireProjectPermission } from "../../../middleware/projectAccess.js";
import { OpenAPIHono, z } from "@hono/zod-openapi";
import { FeedbackApiInsertSchema, FeedbackApiUpdateSchema, FeedbackListResponse, FeedbackResponse, PaginationQueryParamsSchema, TenantProjectIdParamsSchema, TenantProjectParamsSchema, commonCreateErrorResponses, commonDeleteErrorResponses, commonGetErrorResponses, commonUpdateErrorResponses, createApiError, createFeedback, deleteFeedback, generateId, getFeedbackById, listFeedback, updateFeedback } from "@inkeep/agents-core";
import { createProtectedRoute } from "@inkeep/agents-core/middleware";

//#region src/domains/manage/routes/feedback.ts
const app = new OpenAPIHono();
app.openapi(createProtectedRoute({
	method: "get",
	path: "/",
	summary: "List Feedback",
	description: "List feedback for a project, optionally filtered by conversation or message",
	operationId: "list-feedback",
	tags: ["Feedback"],
	permission: requireProjectPermission("view"),
	request: {
		params: TenantProjectParamsSchema,
		query: PaginationQueryParamsSchema.extend({
			conversationId: z.string().optional().describe("Optionally filter by conversation ID"),
			messageId: z.string().optional().describe("Optionally filter by message ID"),
			agentId: z.string().optional().describe("Optionally filter by agent ID"),
			type: z.enum(["positive", "negative"]).optional().describe("Optionally filter by feedback type"),
			startDate: z.string().optional().describe("Filter feedback created on or after this date (YYYY-MM-DD)"),
			endDate: z.string().optional().describe("Filter feedback created on or before this date (YYYY-MM-DD)")
		})
	},
	responses: {
		200: {
			description: "List of feedback retrieved successfully",
			content: { "application/json": { schema: FeedbackListResponse } }
		},
		...commonGetErrorResponses
	}
}), async (c) => {
	const { tenantId, projectId } = c.req.valid("param");
	const { conversationId, messageId, agentId, type, startDate, endDate, page = 1, limit = 10 } = c.req.valid("query");
	const result = await listFeedback(runDbClient_default)({
		scopes: {
			tenantId,
			projectId
		},
		conversationId,
		messageId,
		agentId,
		type,
		startDate,
		endDate,
		pagination: {
			page,
			limit
		}
	});
	const total = result.total;
	const pages = Math.ceil(total / limit);
	return c.json({
		data: result.feedback,
		pagination: {
			page,
			limit,
			total,
			pages
		}
	});
});
app.openapi(createProtectedRoute({
	method: "get",
	path: "/{id}",
	summary: "Get Feedback",
	description: "Get a specific feedback entry by ID",
	operationId: "get-feedback-by-id",
	tags: ["Feedback"],
	permission: requireProjectPermission("view"),
	request: { params: TenantProjectIdParamsSchema },
	responses: {
		200: {
			description: "Feedback found",
			content: { "application/json": { schema: FeedbackResponse } }
		},
		...commonGetErrorResponses
	}
}), async (c) => {
	const { tenantId, projectId, id } = c.req.valid("param");
	const entry = await getFeedbackById(runDbClient_default)({
		scopes: {
			tenantId,
			projectId
		},
		feedbackId: id
	});
	if (!entry) throw createApiError({
		code: "not_found",
		message: "Feedback not found"
	});
	return c.json({ data: entry });
});
app.openapi(createProtectedRoute({
	method: "post",
	path: "/",
	summary: "Create Feedback",
	description: "Create a new feedback entry for a conversation or message",
	operationId: "create-feedback",
	tags: ["Feedback"],
	permission: requireProjectPermission("edit"),
	request: {
		params: TenantProjectParamsSchema,
		body: { content: { "application/json": { schema: FeedbackApiInsertSchema } } }
	},
	responses: {
		201: {
			description: "Feedback created successfully",
			content: { "application/json": { schema: FeedbackResponse } }
		},
		...commonCreateErrorResponses
	}
}), async (c) => {
	const { tenantId, projectId } = c.req.valid("param");
	const body = c.req.valid("json");
	const created = await createFeedback(runDbClient_default)({
		...body,
		id: body.id || generateId(),
		tenantId,
		projectId
	});
	return c.json({ data: created }, 201);
});
app.openapi(createProtectedRoute({
	method: "patch",
	path: "/{id}",
	summary: "Update Feedback",
	description: "Update an existing feedback entry",
	operationId: "update-feedback",
	tags: ["Feedback"],
	permission: requireProjectPermission("edit"),
	request: {
		params: TenantProjectIdParamsSchema,
		body: { content: { "application/json": { schema: FeedbackApiUpdateSchema } } }
	},
	responses: {
		200: {
			description: "Feedback updated successfully",
			content: { "application/json": { schema: FeedbackResponse } }
		},
		...commonUpdateErrorResponses
	}
}), async (c) => {
	const { tenantId, projectId, id } = c.req.valid("param");
	const body = c.req.valid("json");
	const updated = await updateFeedback(runDbClient_default)({
		scopes: {
			tenantId,
			projectId
		},
		feedbackId: id,
		data: body
	});
	if (!updated) throw createApiError({
		code: "not_found",
		message: "Feedback not found"
	});
	return c.json({ data: updated });
});
app.openapi(createProtectedRoute({
	method: "delete",
	path: "/{id}",
	summary: "Delete Feedback",
	description: "Delete a feedback entry",
	operationId: "delete-feedback",
	tags: ["Feedback"],
	permission: requireProjectPermission("edit"),
	request: { params: TenantProjectIdParamsSchema },
	responses: {
		200: {
			description: "Feedback deleted successfully",
			content: { "application/json": { schema: z.object({ success: z.boolean() }) } }
		},
		...commonDeleteErrorResponses
	}
}), async (c) => {
	const { tenantId, projectId, id } = c.req.valid("param");
	if (!await deleteFeedback(runDbClient_default)({
		scopes: {
			tenantId,
			projectId
		},
		feedbackId: id
	})) throw createApiError({
		code: "not_found",
		message: "Feedback not found"
	});
	return c.json({ success: true });
});
var feedback_default = app;

//#endregion
export { feedback_default as default };