import { z } from 'zod';
import { dateTransformSchema, defaultTransformSchema } from '../shared/shared.dto';

const optionalString = z.preprocess((value) => {
  if (typeof value === 'string' && value.trim() === '') return undefined;
  return value;
}, z.string().trim().optional());

export const createIpWhitelistSchema = z.object({
  label: z.string().min(1, 'Label is required'),
  ip_or_cidr: z.string().min(1, 'IP address or CIDR is required').trim(),
  is_active: z.boolean().default(true),
});

export const updateIpWhitelistSchema = z.object({
  label: optionalString,
  ip_or_cidr: optionalString,
  is_active: z.boolean().optional(),
});

export const ipWhitelistResponseSchema = z.object({
  id: z.string().uuid(),
  owner_id: z.string(),
  label: defaultTransformSchema,
  ip_or_cidr: z.string(),
  is_active: z.boolean(),
  created_at: dateTransformSchema,
  updated_at: dateTransformSchema,
});

export const ipWhitelistListResponseSchema = z.array(ipWhitelistResponseSchema);

export type CreateIpWhitelistDto = z.infer<typeof createIpWhitelistSchema>;
export type UpdateIpWhitelistDto = z.infer<typeof updateIpWhitelistSchema>;
export type IpWhitelistResponseDto = z.infer<typeof ipWhitelistResponseSchema>;
