import { NextResponse } from "next/server";
import { z } from "zod";
import { getAuthFromRequest } from "@open-mercato/shared/lib/auth/server";
import { createRequestContainer } from "@open-mercato/shared/lib/di/container";
import { logCrudAccess } from "@open-mercato/shared/lib/crud/factory";
import { FeatureToggle } from "../../../data/entities.js";
import { featureTogglesTag, featureToggleSchema, featureToggleErrorSchema } from "../../openapi.js";
const paramsSchema = z.object({
  id: z.string().uuid()
});
async function GET(_req, ctx) {
  const auth = await getAuthFromRequest(_req);
  if (!auth) {
    return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
  }
  const parse = paramsSchema.safeParse({ id: ctx.params?.id });
  if (!parse.success) {
    return NextResponse.json({ error: "Invalid feature toggle id" }, { status: 400 });
  }
  const container = await createRequestContainer();
  const em = container.resolve("em");
  const toggle = await em.findOne(FeatureToggle, {
    id: parse.data.id,
    deletedAt: null
  });
  if (!toggle) {
    return NextResponse.json({ error: "Feature toggle not found" }, { status: 404 });
  }
  await logCrudAccess({
    container,
    auth,
    request: _req,
    items: [toggle],
    idField: "id",
    resourceKind: "feature_toggles.feature_toggle",
    tenantId: auth.tenantId ?? null,
    query: { id: parse.data.id },
    accessType: "read:item",
    fields: ["id", "identifier", "name", "description", "category", "created_at", "updated_at"]
  });
  const featureToggle = {
    id: toggle.id,
    identifier: toggle.identifier,
    name: toggle.name,
    description: toggle.description ?? null,
    category: toggle.category ?? null,
    type: toggle.type,
    defaultValue: toggle.defaultValue,
    createdAt: toggle.createdAt,
    updatedAt: toggle.updatedAt
  };
  return NextResponse.json(featureToggle);
}
const routeMetadata = {
  GET: { requireAuth: true, requireFeatures: ["feature_toggles.view"] }
};
const metadata = routeMetadata;
const openApi = {
  tag: featureTogglesTag,
  summary: "Fetch feature toggle detail",
  methods: {
    GET: {
      summary: "Fetch feature toggle by ID",
      description: "Returns complete details of a feature toggle.",
      responses: [
        { status: 200, description: "Feature toggle detail", schema: featureToggleSchema }
      ],
      errors: [
        { status: 400, description: "Invalid identifier", schema: featureToggleErrorSchema },
        { status: 401, description: "Unauthorized", schema: featureToggleErrorSchema },
        { status: 404, description: "Feature toggle not found", schema: featureToggleErrorSchema }
      ]
    }
  }
};
export {
  GET,
  metadata,
  openApi
};
//# sourceMappingURL=route.js.map
