import { z } from "zod";
import { findWithDecryption } from "@open-mercato/shared/lib/encryption/find";
import { E } from "../../../generated/entities.ids.generated.js";
import { Attachment } from "@open-mercato/core/modules/attachments/data/entities";
import { CatalogProductTagAssignment } from "../data/entities.js";
import { assertTenantScope } from "./types.js";
function resolveEm(ctx) {
  return ctx.container.resolve("em");
}
function buildScope(ctx, tenantId) {
  return { tenantId, organizationId: ctx.organizationId };
}
const listProductMediaInput = z.object({
  productId: z.string().uuid().describe("Catalog product id (UUID)."),
  limit: z.number().int().min(1).max(100).optional().describe("Max rows (default 50, max 100)."),
  offset: z.number().int().min(0).optional().describe("Rows to skip (default 0).")
}).passthrough();
const listProductMediaTool = {
  name: "catalog.list_product_media",
  displayName: "List product media",
  description: "Enumerate media attachments (metadata only) associated with a catalog product. Use the attachment bridge (Step 3.7) to fetch bytes.",
  inputSchema: listProductMediaInput,
  requiredFeatures: ["catalog.products.view"],
  tags: ["read", "catalog"],
  handler: async (rawInput, ctx) => {
    const { tenantId } = assertTenantScope(ctx);
    const input = listProductMediaInput.parse(rawInput);
    const em = resolveEm(ctx);
    const limit = input.limit ?? 50;
    const offset = input.offset ?? 0;
    const where = {
      tenantId,
      entityId: E.catalog.catalog_product,
      recordId: input.productId
    };
    if (ctx.organizationId) where.organizationId = ctx.organizationId;
    const [rows, total] = await Promise.all([
      findWithDecryption(
        em,
        Attachment,
        where,
        { limit, offset, orderBy: { createdAt: "asc" } },
        buildScope(ctx, tenantId)
      ),
      em.count(Attachment, where)
    ]);
    const filtered = rows.filter((row) => (row.tenantId ?? null) === tenantId);
    return {
      items: filtered.map((row) => ({
        id: row.id,
        fileName: row.fileName,
        mimeType: row.mimeType,
        fileSize: row.fileSize,
        url: row.url,
        storageDriver: row.storageDriver,
        partitionCode: row.partitionCode,
        entityId: row.entityId,
        recordId: row.recordId,
        organizationId: row.organizationId ?? null,
        tenantId: row.tenantId ?? null,
        createdAt: row.createdAt ? new Date(row.createdAt).toISOString() : null
      })),
      total,
      limit,
      offset
    };
  }
};
const listProductTagsInput = z.object({
  productId: z.string().uuid().describe("Catalog product id (UUID).")
}).passthrough();
const listProductTagsTool = {
  name: "catalog.list_product_tags",
  displayName: "List product tags",
  description: "Enumerate tags assigned to a catalog product (label + slug). Returns { items, total }.",
  inputSchema: listProductTagsInput,
  requiredFeatures: ["catalog.products.view"],
  tags: ["read", "catalog"],
  handler: async (rawInput, ctx) => {
    const { tenantId } = assertTenantScope(ctx);
    const input = listProductTagsInput.parse(rawInput);
    const em = resolveEm(ctx);
    const where = { tenantId, product: input.productId };
    if (ctx.organizationId) where.organizationId = ctx.organizationId;
    const assignments = await findWithDecryption(
      em,
      CatalogProductTagAssignment,
      where,
      { populate: ["tag"] },
      buildScope(ctx, tenantId)
    );
    const filtered = assignments.filter((assignment) => assignment.tenantId === tenantId);
    const items = filtered.map((assignment) => {
      const tag = assignment.tag;
      if (!tag || typeof tag === "string") return null;
      return { id: tag.id, label: tag.label, slug: tag.slug };
    }).filter((value) => value !== null);
    return { items, total: items.length };
  }
};
const mediaTagsAiTools = [listProductMediaTool, listProductTagsTool];
var media_tags_pack_default = mediaTagsAiTools;
export {
  media_tags_pack_default as default,
  mediaTagsAiTools
};
//# sourceMappingURL=media-tags-pack.js.map
