import { NextResponse } from "next/server";
import { z } from "zod";
import { createRequestContainer } from "@open-mercato/shared/lib/di/container";
import { Tenant } from "@open-mercato/core/modules/directory/data/entities";
const metadata = {
  path: "/directory/tenants/lookup",
  GET: {
    requireAuth: false
  }
};
const tenantLookupQuerySchema = z.object({
  tenantId: z.string().uuid()
});
async function GET(req) {
  const url = new URL(req.url);
  const tenantId = url.searchParams.get("tenantId") || url.searchParams.get("tenant") || "";
  const parsed = tenantLookupQuerySchema.safeParse({ tenantId });
  if (!parsed.success) {
    return NextResponse.json({ ok: false, error: "Invalid tenant id." }, { status: 400 });
  }
  const container = await createRequestContainer();
  const em = container.resolve("em");
  const tenant = await em.findOne(Tenant, { id: parsed.data.tenantId, deletedAt: null });
  if (!tenant) {
    return NextResponse.json({ ok: false, error: "Tenant not found." }, { status: 404 });
  }
  return NextResponse.json({
    ok: true,
    tenant: { id: String(tenant.id), name: tenant.name }
  });
}
const lookupTag = "Directory";
const tenantLookupSuccessSchema = z.object({
  ok: z.literal(true),
  tenant: z.object({
    id: z.string().uuid(),
    name: z.string()
  })
});
const tenantLookupErrorSchema = z.object({
  ok: z.literal(false),
  error: z.string()
});
const tenantLookupDoc = {
  summary: "Public tenant lookup",
  description: "Resolves tenant metadata for login/activation flows.",
  tags: [lookupTag],
  query: tenantLookupQuerySchema,
  responses: [
    { status: 200, description: "Tenant resolved.", schema: tenantLookupSuccessSchema }
  ],
  errors: [
    { status: 400, description: "Invalid tenant id", schema: tenantLookupErrorSchema },
    { status: 404, description: "Tenant not found", schema: tenantLookupErrorSchema }
  ]
};
const openApi = {
  tag: lookupTag,
  summary: "Public tenant lookup",
  methods: {
    GET: tenantLookupDoc
  }
};
var lookup_default = GET;
export {
  GET,
  lookup_default as default,
  metadata,
  openApi
};
//# sourceMappingURL=lookup.js.map
