// Note: jsonRecordSchema contains z.lazy() which zodToConvex doesn't support,
// so validators containing metadata use native Convex v instead.

import { v } from 'convex/values';

import {
  jsonRecordValidator,
  jsonValueValidator,
} from '../lib/validators/json';

export const productStatusValidator = v.union(
  v.literal('active'),
  v.literal('inactive'),
  v.literal('draft'),
  v.literal('archived'),
);

export const productTranslationValidator = v.object({
  language: v.string(),
  name: v.optional(v.string()),
  description: v.optional(v.string()),
  category: v.optional(v.string()),
  tags: v.optional(v.array(v.string())),
  metadata: v.optional(jsonRecordValidator),
  createdAt: v.optional(v.number()),
  lastUpdated: v.number(),
});

export const productItemValidator = v.object({
  id: v.string(),
  name: v.string(),
  description: v.optional(v.string()),
  imageUrl: v.optional(v.string()),
  stock: v.optional(v.number()),
  price: v.optional(v.number()),
  currency: v.optional(v.string()),
  category: v.optional(v.string()),
  tags: v.optional(v.array(v.string())),
  status: v.optional(productStatusValidator),
  lastUpdated: v.number(),
  createdAt: v.number(),
  relatedProductsCount: v.optional(v.number()),
  translations: v.optional(v.array(productTranslationValidator)),
  metadata: v.optional(jsonRecordValidator),
});

export const productDocValidator = v.object({
  _id: v.string(),
  _creationTime: v.number(),
  organizationId: v.string(),
  name: v.string(),
  description: v.optional(v.string()),
  imageUrl: v.optional(v.string()),
  stock: v.optional(v.number()),
  price: v.optional(v.number()),
  currency: v.optional(v.string()),
  category: v.optional(v.string()),
  tags: v.optional(v.array(v.string())),
  status: v.optional(productStatusValidator),
  translations: v.optional(v.array(productTranslationValidator)),
  lastUpdated: v.optional(v.number()),
  externalId: v.optional(v.union(v.string(), v.number())),
  metadata: v.optional(jsonRecordValidator),
});

export const createProductArgsValidator = v.object({
  organizationId: v.string(),
  name: v.string(),
  description: v.optional(v.string()),
  imageUrl: v.optional(v.string()),
  stock: v.optional(v.number()),
  price: v.optional(v.number()),
  currency: v.optional(v.string()),
  category: v.optional(v.string()),
  tags: v.optional(v.array(v.string())),
  status: v.optional(productStatusValidator),
  externalId: v.optional(v.union(v.string(), v.number())),
  metadata: v.optional(jsonRecordValidator),
});

export const updateProductArgsValidator = v.object({
  productId: v.string(),
  name: v.optional(v.string()),
  description: v.optional(v.string()),
  imageUrl: v.optional(v.string()),
  stock: v.optional(v.number()),
  price: v.optional(v.number()),
  currency: v.optional(v.string()),
  category: v.optional(v.string()),
  tags: v.optional(v.array(v.string())),
  status: v.optional(productStatusValidator),
  translations: v.optional(v.array(productTranslationValidator)),
  metadata: v.optional(jsonRecordValidator),
});

export const bulkCreateErrorItemValidator = v.object({
  index: v.number(),
  error: v.string(),
  errorCode: v.string(),
  product: jsonValueValidator,
});

export const bulkCreateProductsResponseValidator = v.object({
  success: v.number(),
  failed: v.number(),
  errors: v.array(bulkCreateErrorItemValidator),
});
