import { z } from 'zod';

/**
 * Schema for defining order-by parameters in queries.
 * Used for sorting search results.
 */
export const OrderBySchema = z.object({
  by: z.string(), // Field name to sort by
  label: z.string(), // Display label for the sort option
  desc: z.boolean(), // Whether sorting is descending
  selected: z.boolean(), // Whether this sort option is currently selected
});

/**
 * Schema for validating and parsing query tokens.
 * Used for search operations to ensure consistent query structure.
 */
export const QueryTokenSchema = z.object({
  filter: z.record(z.any()).default({}), // Filter criteria as key-value pairs
  input: z.record(z.any()).default({}).optional(), // Input values for the query
  orderBy: z.array(OrderBySchema).default([]).optional(), // Sorting parameters
  skip: z.number().default(0).optional(), // Number of items to skip (for pagination)
  take: z.number().default(25).optional(), // Number of items to take (page size)
});

export const RemissionQueryTokenSchema = QueryTokenSchema.merge(
  z.object({
    assignedStockId: z.number(), // ID of the stock to query
    supplierId: z.number(), // ID of the supplier to query
  }),
);

/**
 * Type representing the structure of a query token input.
 * Generated from the Zod schema for type safety.
 */
export type QueryTokenInput = z.infer<typeof QueryTokenSchema>;
export type RemissionQueryTokenInput = z.infer<
  typeof RemissionQueryTokenSchema
>;
