import { createRequestContainer } from "@open-mercato/shared/lib/di/container";
import { Attachment } from "./data/entities.js";
function parseArgs(rest) {
  const args = {};
  for (let i = 0; i < rest.length; i += 1) {
    const part = rest[i];
    if (!part || !part.startsWith("--")) continue;
    const [rawKey, rawValue] = part.replace(/^--/, "").split("=");
    const key = rawKey.trim();
    if (!key) continue;
    if (rawValue !== void 0) {
      args[key] = rawValue;
      continue;
    }
    const next = rest[i + 1];
    if (next && !next.startsWith("--")) {
      args[key] = next;
      i += 1;
    } else {
      args[key] = true;
    }
  }
  return args;
}
function coerceIdList(value) {
  if (typeof value !== "string") return [];
  return value.split(",").map((entry) => entry.trim()).filter((entry) => entry.length > 0);
}
const deleteAttachments = {
  command: "delete",
  async run(rest) {
    const args = parseArgs(rest);
    const ids = /* @__PURE__ */ new Set();
    coerceIdList(args.id).forEach((id) => ids.add(id));
    coerceIdList(args.ids).forEach((id) => ids.add(id));
    if (ids.size === 0) {
      console.error("Usage: mercato attachments delete --id <attachmentId> [--ids id1,id2] [--org <organizationId>] [--tenant <tenantId>]");
      return;
    }
    const organizationId = typeof args.org === "string" ? args.org : typeof args.organizationId === "string" ? args.organizationId : void 0;
    const tenantId = typeof args.tenant === "string" ? args.tenant : typeof args.tenantId === "string" ? args.tenantId : void 0;
    try {
      const container = await createRequestContainer();
      const em = container.resolve("em");
      const storageDriverFactory = container.resolve("storageDriverFactory");
      const idList = Array.from(ids);
      const where = { id: { $in: idList } };
      if (organizationId) where.organizationId = organizationId;
      if (tenantId) where.tenantId = tenantId;
      const attachments = await em.find(Attachment, where);
      if (!attachments.length) {
        console.log("No attachments matched the provided filters.");
        return;
      }
      const removedIds = /* @__PURE__ */ new Set();
      for (const entry of attachments) {
        const driver = await storageDriverFactory.resolveForPartition(entry.partitionCode, {
          tenantId: entry.tenantId ?? "",
          organizationId: entry.organizationId ?? ""
        });
        await driver.delete(entry.partitionCode, entry.storagePath);
        em.remove(entry);
        removedIds.add(entry.id);
        console.log(`Deleted attachment ${entry.id}${entry.fileName ? ` (${entry.fileName})` : ""}`);
      }
      await em.flush();
      const missing = idList.filter((id) => !removedIds.has(id));
      if (missing.length > 0) {
        console.log(`Not found: ${missing.join(", ")}`);
      }
    } catch (err) {
      console.error("[attachments] delete command failed:", err);
    }
  }
};
var cli_default = [deleteAttachments];
export {
  cli_default as default
};
//# sourceMappingURL=cli.js.map
