import { z } from 'zod';
import type { LocalizedText } from '@/server/page-layout/types';
import { dealTypeSchema } from '@/lib/deal-types/schema';
import type { DealType } from '@/lib/deal-types';

export interface Config {
  title?: LocalizedText;
  source: 'wishlist' | 'category' | 'ids' | 'custom-query';
  /** For source='ids' */
  ids?: string[];
  /** For source='custom-query' */
  filters?: {
    dealType?: DealType;
    lowStock?: boolean;
    nearExpiry?: boolean;
  };
  sort?: 'recent' | 'trending';
  limit: number;
}

export const configSchema: z.ZodType<Config> = z.object({
  title: z.object({ he: z.string(), en: z.string() }).optional(),
  source: z.enum(['wishlist', 'category', 'ids', 'custom-query']),
  ids: z.array(z.uuid()).max(60).optional(),
  filters: z
    .object({
      dealType: dealTypeSchema.optional(),
      lowStock: z.boolean().optional(),
      nearExpiry: z.boolean().optional(),
    })
    .optional(),
  sort: z.enum(['recent', 'trending']).optional(),
  limit: z.number().int().min(1).max(60),
});

export const defaultConfig: Config = {
  source: 'category',
  limit: 12,
  sort: 'recent',
};

export const type = 'deal-grid' as const;
export const category = 'custom' as const;
export const labelKey = 'module_labels.deal-grid' as const;
