import { z } from 'zod';
import { ALL_PERMISSIONS } from '@/lib/auth/permissions';

const permissionEnum = z.enum(ALL_PERMISSIONS);
const permissionTemplateRequiredMessage = 'กรุณาเลือก Permission Template';

export const userPermissionAssignmentSchema = z.object({
  template_id: z
    .number({ message: permissionTemplateRequiredMessage })
    .int()
    .positive(permissionTemplateRequiredMessage),
  allow_permissions: z.array(permissionEnum),
  deny_permissions: z.array(permissionEnum),
  reason: z.string().trim().max(500, 'เหตุผลต้องไม่เกิน 500 ตัวอักษร').optional()
});

export const userPermissionOverrideSchema = z.object({
  permission: permissionEnum,
  effect: z.enum(['allow', 'deny']),
  reason: z.string().trim().max(500, 'เหตุผลต้องไม่เกิน 500 ตัวอักษร').optional(),
  expires_at: z.string().datetime().nullable().optional()
});

export type UserPermissionAssignmentFormValues = z.infer<typeof userPermissionAssignmentSchema>;
export type UserPermissionOverrideFormValues = z.infer<typeof userPermissionOverrideSchema>;
