import { registerCommand } from "@open-mercato/shared/lib/commands";
import { CrudHttpError } from "@open-mercato/shared/lib/crud/errors";
import { SalesDocumentTag, SalesDocumentTagAssignment } from "../data/entities.js";
import { ensureTenantScope } from "./shared.js";
import {
  salesTagCreateSchema,
  salesTagUpdateSchema
} from "../data/validators.js";
const createTagCommand = {
  id: "sales.tags.create",
  async execute(rawInput, ctx) {
    const parsed = salesTagCreateSchema.parse(rawInput ?? {});
    ensureTenantScope(ctx, parsed.tenantId);
    const em = ctx.container.resolve("em").fork();
    const conflict = await em.findOne(SalesDocumentTag, {
      slug: parsed.slug,
      organizationId: parsed.organizationId,
      tenantId: parsed.tenantId
    });
    if (conflict) throw new CrudHttpError(409, { error: "Tag slug already exists for this scope" });
    const tag = em.create(SalesDocumentTag, {
      organizationId: parsed.organizationId,
      tenantId: parsed.tenantId,
      slug: parsed.slug,
      label: parsed.label,
      color: parsed.color ?? null,
      description: parsed.description ?? null
    });
    await em.persist(tag).flush();
    return { tagId: tag.id };
  }
};
const updateTagCommand = {
  id: "sales.tags.update",
  async execute(rawInput, ctx) {
    const parsed = salesTagUpdateSchema.parse(rawInput ?? {});
    const em = ctx.container.resolve("em").fork();
    const tag = await em.findOne(SalesDocumentTag, { id: parsed.id });
    if (!tag) throw new CrudHttpError(404, { error: "Tag not found" });
    ensureTenantScope(ctx, parsed.tenantId ?? tag.tenantId);
    if (parsed.slug && parsed.slug !== tag.slug) {
      const conflict = await em.findOne(SalesDocumentTag, {
        slug: parsed.slug,
        organizationId: parsed.organizationId ?? tag.organizationId,
        tenantId: parsed.tenantId ?? tag.tenantId
      });
      if (conflict && conflict.id !== tag.id) {
        throw new CrudHttpError(409, { error: "Tag slug already exists for this scope" });
      }
      tag.slug = parsed.slug;
    }
    if (parsed.label !== void 0) tag.label = parsed.label;
    if (parsed.color !== void 0) tag.color = parsed.color ?? null;
    if (parsed.description !== void 0) tag.description = parsed.description ?? null;
    if (parsed.organizationId) tag.organizationId = parsed.organizationId;
    if (parsed.tenantId) tag.tenantId = parsed.tenantId;
    await em.flush();
    return { tagId: tag.id };
  }
};
const deleteTagCommand = {
  id: "sales.tags.delete",
  async execute(input, ctx) {
    const id = typeof input?.id === "string" ? input.id : null;
    if (!id) throw new CrudHttpError(400, { error: "Tag id is required" });
    const em = ctx.container.resolve("em").fork();
    const tag = await em.findOne(SalesDocumentTag, { id });
    if (!tag) throw new CrudHttpError(404, { error: "Tag not found" });
    ensureTenantScope(ctx, tag.tenantId);
    await em.nativeDelete(SalesDocumentTagAssignment, { tag: tag.id });
    em.remove(tag);
    await em.flush();
    return { tagId: id };
  }
};
registerCommand(createTagCommand);
registerCommand(updateTagCommand);
registerCommand(deleteTagCommand);
//# sourceMappingURL=tags.js.map
