import { requireProjectPermission } from "../../../middleware/projectAccess.js";
import { speakeasyOffsetLimitPagination } from "../../../utils/speakeasy.js";
import { OpenAPIHono } from "@hono/zod-openapi";
import { PaginationQueryParamsSchema, SkillApiInsertSchema, SkillApiUpdateSchema, SkillListResponse, SkillResponse, TenantProjectIdParamsSchema, TenantProjectParamsSchema, commonGetErrorResponses, createApiError, createSkill, deleteSkill, getSkillById, listSkills, updateSkill } from "@inkeep/agents-core";
import { createProtectedRoute } from "@inkeep/agents-core/middleware";

//#region src/domains/manage/routes/skills.ts
const app = new OpenAPIHono();
app.openapi(createProtectedRoute({
	method: "get",
	path: "/",
	summary: "List Skills",
	operationId: "list-skills",
	tags: ["Skills"],
	permission: requireProjectPermission("view"),
	request: {
		params: TenantProjectParamsSchema,
		query: PaginationQueryParamsSchema
	},
	responses: {
		200: {
			description: "Skills retrieved successfully",
			content: { "application/json": { schema: SkillListResponse } }
		},
		...commonGetErrorResponses
	},
	...speakeasyOffsetLimitPagination
}), async (c) => {
	const db = c.get("db");
	const { tenantId, projectId } = c.req.valid("param");
	const { page, limit } = c.req.valid("query");
	const result = await listSkills(db)({
		scopes: {
			tenantId,
			projectId
		},
		pagination: {
			page,
			limit
		}
	});
	return c.json(result);
});
app.openapi(createProtectedRoute({
	method: "get",
	path: "/{id}",
	summary: "Get Skill",
	operationId: "get-skill",
	tags: ["Skills"],
	permission: requireProjectPermission("view"),
	request: { params: TenantProjectIdParamsSchema },
	responses: {
		200: {
			description: "Skill found",
			content: { "application/json": { schema: SkillResponse } }
		},
		...commonGetErrorResponses
	}
}), async (c) => {
	const db = c.get("db");
	const { tenantId, projectId, id } = c.req.valid("param");
	const skill = await getSkillById(db)({
		scopes: {
			tenantId,
			projectId
		},
		skillId: id
	});
	if (!skill) throw createApiError({
		code: "not_found",
		message: "Skill not found"
	});
	return c.json({ data: skill });
});
app.openapi(createProtectedRoute({
	method: "post",
	path: "/",
	summary: "Create Skill",
	operationId: "create-skill",
	tags: ["Skills"],
	permission: requireProjectPermission("edit"),
	request: {
		params: TenantProjectParamsSchema,
		body: { content: { "application/json": { schema: SkillApiInsertSchema } } }
	},
	responses: {
		201: {
			description: "Skill created successfully",
			content: { "application/json": { schema: SkillResponse } }
		},
		...commonGetErrorResponses
	}
}), async (c) => {
	const db = c.get("db");
	const { tenantId, projectId } = c.req.valid("param");
	const body = c.req.valid("json");
	const skill = await createSkill(db)({
		...body,
		tenantId,
		projectId
	});
	return c.json({ data: skill }, 201);
});
app.openapi(createProtectedRoute({
	method: "put",
	path: "/{id}",
	summary: "Update Skill",
	operationId: "update-skill",
	tags: ["Skills"],
	permission: requireProjectPermission("edit"),
	request: {
		params: TenantProjectIdParamsSchema,
		body: { content: { "application/json": { schema: SkillApiUpdateSchema } } }
	},
	responses: {
		200: {
			description: "Skill updated successfully",
			content: { "application/json": { schema: SkillResponse } }
		},
		...commonGetErrorResponses
	}
}), async (c) => {
	const db = c.get("db");
	const { tenantId, projectId, id } = c.req.valid("param");
	const body = c.req.valid("json");
	const skill = await updateSkill(db)({
		scopes: {
			tenantId,
			projectId
		},
		skillId: id,
		data: body
	});
	if (!skill) throw createApiError({
		code: "not_found",
		message: "Skill not found"
	});
	return c.json({ data: skill });
});
app.openapi(createProtectedRoute({
	method: "delete",
	path: "/{id}",
	summary: "Delete Skill",
	operationId: "delete-skill",
	tags: ["Skills"],
	permission: requireProjectPermission("edit"),
	request: { params: TenantProjectIdParamsSchema },
	responses: {
		204: { description: "Skill deleted successfully" },
		...commonGetErrorResponses
	}
}), async (c) => {
	const db = c.get("db");
	const { tenantId, projectId, id } = c.req.valid("param");
	if (!await deleteSkill(db)({
		scopes: {
			tenantId,
			projectId
		},
		skillId: id
	})) throw createApiError({
		code: "not_found",
		message: "Skill not found"
	});
	return c.body(null, 204);
});
var skills_default = app;

//#endregion
export { skills_default as default };