import { z } from 'zod';

/**
 * CRUDA enum
 * Can Create, Read, Update, Delete, Archive
 */
export const CRUDA = {
  None: 0,
  Create: 1,
  Read: 2,
  Update: 4,
  Delete: 8,
  Archive: 16,
} as const;

const ALL_FLAGS = Object.values(CRUDA).reduce<number>((a, b) => a | b, 0);

export const CRUDASchema = z
  .number()
  .int()
  .nonnegative()
  .refine((val) => (val & ~ALL_FLAGS) === 0, {
    message: 'Invalid CRUDA permission: contains unknown flags',
  })
  .describe('CRUDA permissions (bitflags)');

export type CRUDA = z.infer<typeof CRUDASchema>;
