import { z } from 'zod';
import { QueryOrderBySchema } from './query-order.schema';

/**
 * Schema representing a complete query for retrieving filtered and sorted data.
 * This is the core schema used when making requests to APIs or data sources.
 *
 * @property filter - Record of filter criteria to apply when querying data
 * @property input - Record of user input values from filter controls
 * @property orderBy - Array of sort criteria to determine result ordering
 * @property skip - Number of items to skip (for pagination)
 * @property take - Maximum number of items to return (page size)
 */
export const QuerySchema = z.object({
  filter: z
    .record(z.any())
    .default({})
    .describe(
      'Key-value pairs of filter criteria to apply when querying data. Keys correspond to data properties, values are the filtering constraints.',
    ),
  input: z
    .record(z.any())
    .default({})
    .describe(
      'Key-value pairs representing user input from filter controls. Used to store and restore filter state.',
    ),
  orderBy: z
    .array(QueryOrderBySchema)
    .default([])
    .describe(
      'Array of sorting criteria that determine how results should be ordered. Applied in sequence for multi-level sorting.',
    ),
  skip: z
    .number()
    .default(0)
    .describe(
      'Number of items to skip from the beginning of the result set. Used for implementing pagination.',
    ),
  take: z
    .number()
    .default(25)
    .describe(
      'Maximum number of items to return in a single query. Defines the page size for paginated results.',
    ),
});

export type Query = z.infer<typeof QuerySchema>;
