import { z } from "zod";
const uuid = () => z.string().uuid();
const coerceNumericString = z.preprocess(
  (val) => typeof val === "number" ? String(val) : val,
  z.string().regex(/^\d+(\.\d+)?$/)
);
const addressSchema = z.object({
  line1: z.string().trim().max(500).optional(),
  line2: z.string().trim().max(500).optional(),
  city: z.string().trim().max(200).optional(),
  state: z.string().trim().max(200).optional(),
  postalCode: z.string().trim().max(50).optional(),
  country: z.string().trim().max(200).optional(),
  company: z.string().trim().max(300).optional(),
  contactName: z.string().trim().max(300).optional()
}).optional();
const orderPayloadSchema = z.object({
  customerEntityId: uuid().optional(),
  customerName: z.string().trim().min(1).max(300),
  customerEmail: z.string().trim().email().max(320).optional(),
  channelId: uuid().optional(),
  currencyCode: z.string().trim().length(3),
  taxRateId: uuid().optional(),
  lineItems: z.array(z.object({
    productName: z.string().trim().min(1).max(300),
    productId: uuid().optional(),
    variantId: uuid().optional(),
    sku: z.string().trim().max(100).optional(),
    quantity: coerceNumericString,
    unitPrice: coerceNumericString.optional(),
    catalogPrice: z.string().optional(),
    kind: z.enum(["product", "service"]).default("product"),
    description: z.string().trim().max(2e3).optional()
  })).min(1).max(100),
  requestedDeliveryDate: z.string().optional(),
  notes: z.string().trim().max(4e3).optional(),
  customerReference: z.string().trim().max(200).optional(),
  shippingAddress: addressSchema,
  billingAddress: addressSchema,
  shippingAddressId: uuid().optional(),
  billingAddressId: uuid().optional()
});
const updateOrderPayloadSchema = z.object({
  orderId: uuid().optional(),
  orderNumber: z.string().trim().max(100).optional(),
  quantityChanges: z.array(z.object({
    lineItemName: z.string().trim().min(1).max(300),
    lineItemId: uuid().optional(),
    oldQuantity: z.string().optional(),
    newQuantity: z.string().regex(/^\d+(\.\d+)?$/)
  })).optional(),
  deliveryDateChange: z.object({
    oldDate: z.string().optional(),
    newDate: z.string()
  }).optional(),
  noteAdditions: z.array(z.string().trim().max(4e3)).optional()
}).refine((value) => Boolean(value.orderId || value.orderNumber), {
  message: "order_reference_required"
});
const updateShipmentPayloadSchema = z.object({
  orderId: uuid().optional(),
  orderNumber: z.string().trim().max(100).optional(),
  trackingNumbers: z.array(z.string().trim().max(200)).optional(),
  carrierName: z.string().trim().max(200).optional(),
  statusLabel: z.string().trim().min(1).max(200),
  shippedAt: z.string().optional(),
  deliveredAt: z.string().optional(),
  estimatedDelivery: z.string().optional(),
  notes: z.string().trim().max(4e3).optional()
}).refine((value) => Boolean(value.orderId || value.orderNumber), {
  message: "order_reference_required"
});
const lowercaseContactType = z.preprocess(
  (val) => typeof val === "string" ? val.toLowerCase() : val,
  z.enum(["person", "company"])
);
const createContactPayloadSchema = z.object({
  type: lowercaseContactType,
  name: z.string().trim().min(1).max(300),
  email: z.string().trim().email().max(320).optional(),
  phone: z.string().trim().max(50).optional(),
  companyName: z.string().trim().max(300).optional(),
  role: z.string().trim().max(150).optional(),
  source: z.literal("inbox_ops").default("inbox_ops")
});
const linkContactPayloadSchema = z.object({
  emailAddress: z.string().trim().email().max(320),
  contactId: uuid(),
  contactType: lowercaseContactType,
  contactName: z.string().trim().min(1).max(300)
});
const createProductPayloadSchema = z.object({
  title: z.string().trim().min(1).max(255),
  sku: z.string().trim().max(100).optional(),
  unitPrice: coerceNumericString.optional(),
  currencyCode: z.string().trim().length(3).optional(),
  kind: z.enum(["product", "service"]).default("product"),
  description: z.string().trim().max(4e3).optional()
});
const logActivityPayloadSchema = z.object({
  contactId: uuid().optional(),
  contactType: lowercaseContactType,
  contactName: z.string().trim().min(1).max(300),
  activityType: z.enum(["email", "call", "meeting", "note"]),
  subject: z.string().trim().min(1).max(200),
  body: z.string().trim().max(8e3)
});
const draftReplyPayloadSchema = z.object({
  to: z.string().trim().email().max(320),
  toName: z.string().trim().max(300).optional().nullable(),
  replyTo: z.string().trim().email().max(320).optional().nullable(),
  subject: z.string().trim().min(1).max(500),
  body: z.string().trim().min(1).max(1e4),
  inReplyToMessageId: z.string().trim().max(500).optional().nullable(),
  references: z.array(z.string().trim().max(500)).optional().nullable(),
  context: z.string().trim().max(4e3).optional().nullable()
});
const inboxProposalCategoryEnum = z.enum([
  "rfq",
  "order",
  "order_update",
  "complaint",
  "shipping_update",
  "inquiry",
  "payment",
  "other"
]);
const ALL_CATEGORIES = inboxProposalCategoryEnum.options;
const proposalCategoryFilterSchema = z.string().trim().max(200).refine((value) => {
  const categories = value.split(",").map((category) => category.trim()).filter(Boolean);
  return categories.length > 0 && categories.every((category) => inboxProposalCategoryEnum.safeParse(category).success);
}, {
  message: `Category filter must contain only: ${ALL_CATEGORIES.join(", ")}`
});
const categorizeProposalSchema = z.object({
  category: inboxProposalCategoryEnum
});
const extractedParticipantSchema = z.object({
  name: z.string(),
  email: z.string(),
  role: z.enum(["buyer", "seller", "logistics", "finance", "other"])
});
const extractedActionSchema = z.object({
  actionType: z.enum([
    "create_order",
    "create_quote",
    "update_order",
    "update_shipment",
    "create_contact",
    "create_product",
    "link_contact",
    "log_activity",
    "draft_reply"
  ]),
  description: z.string(),
  confidence: z.number(),
  requiredFeature: z.string().optional(),
  payloadJson: z.string().describe("JSON-encoded payload object for this action")
});
const extractedDiscrepancySchema = z.object({
  type: z.enum([
    "price_mismatch",
    "quantity_mismatch",
    "unknown_contact",
    "currency_mismatch",
    "date_conflict",
    "product_not_found",
    "duplicate_order",
    "other"
  ]),
  severity: z.enum(["warning", "error"]),
  description: z.string(),
  expectedValue: z.string().optional(),
  foundValue: z.string().optional(),
  actionIndex: z.number().optional()
});
const extractionOutputSchema = z.object({
  summary: z.string(),
  category: inboxProposalCategoryEnum.optional(),
  participants: z.array(extractedParticipantSchema),
  proposedActions: z.array(extractedActionSchema),
  discrepancies: z.array(extractedDiscrepancySchema),
  draftReplies: z.array(z.object({
    to: z.string(),
    toName: z.string().optional(),
    subject: z.string(),
    body: z.string(),
    context: z.string().optional()
  })),
  confidence: z.number(),
  detectedLanguage: z.string().optional(),
  possiblyIncomplete: z.boolean().optional()
});
const translateProposalSchema = z.object({
  targetLocale: z.enum(["en", "de", "es", "pl"])
});
const updateSettingsSchema = z.object({
  workingLanguage: z.enum(["en", "de", "es", "pl"]).optional(),
  isActive: z.boolean().optional(),
  // Per-tenant inbound-webhook secret. Write-only: it is never returned by the
  // settings GET. A non-empty value binds custom-provider webhook signatures for
  // this inbox to this secret; null/empty clears it and reverts to the global key.
  webhookSecret: z.string().trim().min(16).max(256).nullable().optional()
});
const proposalListQuerySchema = z.object({
  status: z.enum(["pending", "partial", "accepted", "rejected"]).optional(),
  category: proposalCategoryFilterSchema.optional(),
  search: z.string().trim().max(200).optional(),
  page: z.coerce.number().int().min(1).default(1),
  pageSize: z.coerce.number().int().min(1).max(100).default(25)
});
const emailListQuerySchema = z.object({
  status: z.enum(["received", "processing", "processed", "needs_review", "failed"]).optional(),
  page: z.coerce.number().int().min(1).default(1),
  pageSize: z.coerce.number().int().min(1).max(100).default(25)
});
const actionEditSchema = z.object({
  payload: z.record(z.string(), z.unknown())
});
const ACTION_PAYLOAD_SCHEMAS = {
  create_order: orderPayloadSchema,
  create_quote: orderPayloadSchema,
  update_order: updateOrderPayloadSchema,
  update_shipment: updateShipmentPayloadSchema,
  create_contact: createContactPayloadSchema,
  create_product: createProductPayloadSchema,
  link_contact: linkContactPayloadSchema,
  log_activity: logActivityPayloadSchema,
  draft_reply: draftReplyPayloadSchema
};
function validateActionPayloadForType(actionType, payload) {
  const schema = ACTION_PAYLOAD_SCHEMAS[actionType];
  if (!schema) {
    return { success: true };
  }
  const result = schema.safeParse(payload);
  if (!result.success) {
    const issues = result.error.issues.map((i) => `${i.path.join(".")}: ${i.message}`).join("; ");
    return { success: false, error: `Invalid payload for ${actionType}: ${issues}` };
  }
  return { success: true };
}
export {
  ALL_CATEGORIES,
  actionEditSchema,
  categorizeProposalSchema,
  createContactPayloadSchema,
  createProductPayloadSchema,
  draftReplyPayloadSchema,
  emailListQuerySchema,
  extractedActionSchema,
  extractedDiscrepancySchema,
  extractedParticipantSchema,
  extractionOutputSchema,
  inboxProposalCategoryEnum,
  linkContactPayloadSchema,
  logActivityPayloadSchema,
  orderPayloadSchema,
  proposalListQuerySchema,
  translateProposalSchema,
  updateOrderPayloadSchema,
  updateSettingsSchema,
  updateShipmentPayloadSchema,
  validateActionPayloadForType
};
//# sourceMappingURL=validators.js.map
