import {
  resolveCreateTenant,
  tenantFilter
} from "./chunk-3F6VNSRK.js";
import {
  jsonOk,
  paginatedOk,
  routeWrap
} from "./chunk-KKJ5D3II.js";
import {
  createUserProfile,
  getUserProfile,
  listUserProfiles,
  updateUserProfile
} from "./chunk-SK4OB66U.js";
import {
  badRequest,
  notFound
} from "./chunk-EZYKRG4W.js";

// src/handlers/anthropic-compat/user-profiles.ts
import { z } from "zod";
var TrustGrantSchema = z.object({
  type: z.literal("vault_credential"),
  vault_id: z.string().min(1),
  credential_id: z.string().min(1)
});
var CreateSchema = z.object({
  external_id: z.string().max(256).optional(),
  display_name: z.string().max(256).optional(),
  trust_grants: z.array(TrustGrantSchema).max(50).optional()
});
var UpdateSchema = z.object({
  external_id: z.string().max(256).nullish(),
  display_name: z.string().max(256).nullish(),
  trust_grants: z.array(TrustGrantSchema).max(50).optional()
});
function handleCreateUserProfile(request) {
  return routeWrap(request, async ({ auth }) => {
    const body = await request.json().catch(() => null);
    const parsed = CreateSchema.safeParse(body);
    if (!parsed.success) {
      throw badRequest(`invalid body: ${parsed.error.issues.map((i) => i.message).join("; ")}`);
    }
    const tenantId = resolveCreateTenant(auth, void 0);
    const profile = createUserProfile({
      external_id: parsed.data.external_id,
      display_name: parsed.data.display_name,
      trust_grants: parsed.data.trust_grants,
      tenant_id: tenantId
    });
    return jsonOk(profile, 201);
  });
}
function handleListUserProfiles(request) {
  return routeWrap(request, async ({ auth }) => {
    const url = new URL(request.url);
    const limit = Math.min(Number(url.searchParams.get("limit") ?? 50), 100);
    const afterId = url.searchParams.get("after_id") ?? void 0;
    const filter = tenantFilter(auth);
    const result = listUserProfiles({
      tenant_id: filter ?? void 0,
      limit,
      after_id: afterId
    });
    return paginatedOk(result.data, limit);
  });
}
function handleGetUserProfile(request, id) {
  return routeWrap(request, async ({ auth }) => {
    const profile = getUserProfile(id);
    if (!profile) throw notFound(`user profile not found: ${id}`);
    const filter = tenantFilter(auth);
    if (filter && profile.tenant_id !== filter) {
      throw notFound(`user profile not found: ${id}`);
    }
    return jsonOk(profile);
  });
}
function handleUpdateUserProfile(request, id) {
  return routeWrap(request, async ({ auth }) => {
    const existing = getUserProfile(id);
    if (!existing) throw notFound(`user profile not found: ${id}`);
    const filter = tenantFilter(auth);
    if (filter && existing.tenant_id !== filter) {
      throw notFound(`user profile not found: ${id}`);
    }
    const body = await request.json().catch(() => null);
    const parsed = UpdateSchema.safeParse(body);
    if (!parsed.success) {
      throw badRequest(`invalid body: ${parsed.error.issues.map((i) => i.message).join("; ")}`);
    }
    const updated = updateUserProfile(id, {
      external_id: parsed.data.external_id,
      display_name: parsed.data.display_name,
      trust_grants: parsed.data.trust_grants
    });
    if (!updated) throw notFound(`user profile not found: ${id}`);
    return jsonOk(updated);
  });
}

export {
  handleCreateUserProfile,
  handleListUserProfiles,
  handleGetUserProfile,
  handleUpdateUserProfile
};
