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 { findOneWithDecryption } from "@open-mercato/shared/lib/encryption/find";
import { CommunicationChannel } from "../../../../data/entities.js";
import { ChannelAccessDeniedError, assertCanAccessChannel } from "../../../../lib/access-control.js";
const metadata = {
  path: "/communication_channels/channels/[id]",
  GET: { requireAuth: true, requireFeatures: ["communication_channels.view"] }
};
async function GET(req, context) {
  const { id } = await context.params;
  if (!z.string().uuid().safeParse(id).success) {
    return NextResponse.json({ error: "Invalid channel id" }, { status: 400 });
  }
  const auth = await getAuthFromRequest(req);
  if (!auth?.tenantId) {
    return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
  }
  const container = await createRequestContainer();
  const em = container.resolve("em").fork();
  const channel = await findOneWithDecryption(
    em,
    CommunicationChannel,
    {
      id,
      tenantId: auth.tenantId,
      organizationId: auth.orgId ?? null,
      deletedAt: null
    },
    void 0,
    { tenantId: auth.tenantId, organizationId: auth.orgId ?? null }
  );
  if (!channel) {
    return NextResponse.json({ error: "Channel not found" }, { status: 404 });
  }
  let userFeatures = [];
  try {
    const rbac = container.resolve("rbacService");
    const acl = await rbac.loadAcl(auth.sub, {
      tenantId: auth.tenantId,
      organizationId: auth.orgId ?? null
    });
    userFeatures = acl?.isSuperAdmin ? ["*"] : Array.isArray(acl?.features) ? acl.features : [];
  } catch {
    userFeatures = [];
  }
  try {
    assertCanAccessChannel(channel, auth.sub, userFeatures);
  } catch (err) {
    if (err instanceof ChannelAccessDeniedError) {
      return NextResponse.json({ error: "Channel not found" }, { status: 404 });
    }
    throw err;
  }
  return NextResponse.json({
    id: channel.id,
    providerKey: channel.providerKey,
    channelType: channel.channelType,
    displayName: channel.displayName,
    externalIdentifier: channel.externalIdentifier ?? null,
    credentialsRef: channel.credentialsRef ?? null,
    capabilities: channel.capabilities ?? null,
    isActive: channel.isActive,
    organizationId: channel.organizationId ?? null,
    createdAt: channel.createdAt?.toISOString?.() ?? null,
    updatedAt: channel.updatedAt?.toISOString?.() ?? null
  });
}
const openApi = {
  tags: ["CommunicationChannels"],
  methods: {
    GET: {
      summary: "Get a single communication channel by id",
      tags: ["CommunicationChannels"],
      responses: [
        { status: 200, description: "Channel detail" },
        { status: 400, description: "Invalid channel id" },
        { status: 401, description: "Unauthorized" },
        { status: 404, description: "Channel not found" }
      ]
    }
  }
};
var route_default = GET;
export {
  GET,
  route_default as default,
  metadata,
  openApi
};
//# sourceMappingURL=route.js.map
