import { NextResponse } from "next/server";
import { createRequestContainer } from "@open-mercato/shared/lib/di/container";
import { getAuthFromRequest } from "@open-mercato/shared/lib/auth/server";
import { resolveOrganizationScopeForRequest } from "@open-mercato/core/modules/directory/utils/organizationScope";
import { claimUserTask } from "../../../../lib/task-handler.js";
import {
  workflowsTag,
  userTaskClaimResponseSchema,
  workflowErrorSchema
} from "../../../openapi.js";
import { createLogger } from "@open-mercato/shared/lib/logger";
const logger = createLogger("workflows");
const metadata = {
  requireAuth: true,
  requireFeatures: ["workflows.tasks.claim"]
};
async function POST(request, { params }) {
  try {
    const container = await createRequestContainer();
    const em = container.resolve("em");
    const auth = await getAuthFromRequest(request);
    if (!auth) {
      return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
    }
    const scope = await resolveOrganizationScopeForRequest({ container, auth, request });
    const tenantId = auth.tenantId;
    const organizationId = scope?.selectedId ?? auth.orgId;
    if (!tenantId || !organizationId) {
      return NextResponse.json(
        { error: "Missing tenant or organization context" },
        { status: 400 }
      );
    }
    await claimUserTask(em, params.id, auth.sub);
    const { UserTask } = await import("../../../../data/entities.js");
    const task = await em.findOne(UserTask, {
      id: params.id,
      tenantId,
      organizationId
    });
    if (!task) {
      return NextResponse.json(
        { error: "Task not found after claim" },
        { status: 500 }
      );
    }
    return NextResponse.json({
      data: task,
      message: "Task claimed successfully"
    });
  } catch (error) {
    logger.error("Error claiming user task", { err: error });
    if (error instanceof Error) {
      if (error.message.includes("not found")) {
        return NextResponse.json(
          { error: error.message },
          { status: 404 }
        );
      }
      if (error.message.includes("already")) {
        return NextResponse.json(
          { error: error.message },
          { status: 409 }
        );
      }
    }
    return NextResponse.json(
      {
        error: "Failed to claim user task",
        details: error instanceof Error ? error.message : String(error)
      },
      { status: 500 }
    );
  }
}
const openApi = {
  tag: workflowsTag,
  summary: "Claim user task",
  methods: {
    POST: {
      summary: "Claim a task from role queue",
      description: "Allows a user to claim a task assigned to their role(s). Once claimed, the task moves to IN_PROGRESS status and is assigned to the claiming user.",
      responses: [
        { status: 200, description: "Task claimed successfully", schema: userTaskClaimResponseSchema }
      ],
      errors: [
        { status: 400, description: "Missing tenant or organization context", schema: workflowErrorSchema },
        { status: 401, description: "Unauthorized", schema: workflowErrorSchema },
        { status: 404, description: "Task not found", schema: workflowErrorSchema },
        { status: 409, description: "Task already claimed", schema: workflowErrorSchema },
        { status: 500, description: "Internal server error", schema: workflowErrorSchema }
      ]
    }
  }
};
export {
  POST,
  metadata,
  openApi
};
//# sourceMappingURL=route.js.map
