import { z } from 'zod';

const CANONICAL_SECTIONS = [
  'dashboard',
  'orders', 'reservations',
  'ai_config', 'knowledge_base', 'chat_widget', 'conversations',
  'telephone', 'whatsapp',
  'menu', 'customers', 'loyalty', 'storefront',
  'marketing', 'gift_cards', 'coupons',
  'staff', 'branches', 'analytics', 'settings',
] as const;

const crudPermsSchema = z.object({
  create: z.boolean(),
  read: z.boolean(),
  update: z.boolean(),
  delete: z.boolean(),
});

export const permissionsMapSchema = z.object({
  dashboard: crudPermsSchema,
  orders: crudPermsSchema,
  reservations: crudPermsSchema,
  ai_config: crudPermsSchema,
  knowledge_base: crudPermsSchema,
  chat_widget: crudPermsSchema,
  conversations: crudPermsSchema,
  telephone: crudPermsSchema,
  whatsapp: crudPermsSchema,
  menu: crudPermsSchema,
  customers: crudPermsSchema,
  loyalty: crudPermsSchema,
  storefront: crudPermsSchema,
  marketing: crudPermsSchema,
  gift_cards: crudPermsSchema,
  coupons: crudPermsSchema,
  staff: crudPermsSchema,
  branches: crudPermsSchema,
  analytics: crudPermsSchema,
  settings: crudPermsSchema,
}).strict();

export const createRoleSchema = z.object({
  name: z.string().min(1, 'Role name is required').max(100),
  description: z.string().max(500).optional(),
  permissions: permissionsMapSchema,
});

export const updateRoleSchema = z.object({
  name: z.string().min(1).max(100).optional(),
  description: z.string().max(500).optional(),
  permissions: permissionsMapSchema.optional(),
});

export type CreateRoleInput = z.infer<typeof createRoleSchema>;
export type UpdateRoleInput = z.infer<typeof updateRoleSchema>;

export { CANONICAL_SECTIONS };
