/**
 * User Schema - Zod validation for user forms
 */

import { z } from 'zod';

// User creation schema
export const userCreateSchema = z.object({
  firstName: z
    .string()
    .max(255, 'First name too long')
    .optional()
    .or(z.literal('')),
  lastName: z
    .string()
    .max(255, 'Last name too long')
    .optional()
    .or(z.literal('')),
  phoneNumber: z
    .string()
    .max(50, 'Phone number too long')
    .optional()
    .or(z.literal('')),
  email: z.string().email('Invalid email address').min(1, 'Email is required'),
  password: z
    .string()
    .min(6, 'Password must be at least 6 characters')
    .optional()
    .or(z.literal('')),
  disabled: z.boolean().default(false),
  avatar: z.array(z.unknown()).optional(),
  app_role: z.unknown().optional().nullable(),
  custom_permissions: z.array(z.unknown()).optional(),
});

// User update schema (password optional)
export const userUpdateSchema = z.object({
  firstName: z
    .string()
    .max(255, 'First name too long')
    .optional()
    .or(z.literal('')),
  lastName: z
    .string()
    .max(255, 'Last name too long')
    .optional()
    .or(z.literal('')),
  phoneNumber: z
    .string()
    .max(50, 'Phone number too long')
    .optional()
    .or(z.literal('')),
  email: z.string().email('Invalid email address').min(1, 'Email is required'),
  password: z
    .string()
    .min(6, 'Password must be at least 6 characters')
    .optional()
    .or(z.literal('')),
  disabled: z.boolean().default(false),
  avatar: z.array(z.unknown()).optional(),
  app_role: z.unknown().optional().nullable(),
  custom_permissions: z.array(z.unknown()).optional(),
});

// Infer TypeScript types from schemas
export type UserCreateFormData = z.infer<typeof userCreateSchema>;
export type UserUpdateFormData = z.infer<typeof userUpdateSchema>;

// Initial values for user forms
export const userInitialValues: UserUpdateFormData = {
  firstName: '',
  lastName: '',
  phoneNumber: '',
  email: '',
  password: '',
  disabled: false,
  avatar: [],
  app_role: null,
  custom_permissions: [],
};
