
export interface User {
  id:                 number;
  civility?:          string;
  first_name:         string;
  last_name:          string;
  phone?:             string | null;
  email?:             string;
  password?:          string;

  address?:           string | null;
  city?:              string | null;
  avatar?:            string | null;

  email_verified_at?: string | null;
  
  role_name?:         String    | null;
  roles?:             string;
  abilities?:         null;
}

export interface UserPi {
  civility: string,
  last_name: string,
  first_name: string,
  city: string,
  address: string,
  phone: string,
  email: string,
  email_verified: Date | null,
  password: string;
  passwordConfirm: string;
}
export interface UsersGet {
  id:           number;
  civility:     string;
  first_name:   string;
  last_name:    string;
  role_name:    string;

  email_verified_at?: Date;
}

export const userRules = (t: (key: string) => string, mode: 'create' | 'view' | 'edit' = 'create') => ({
  id: [],

  civility: [
    (v: any) => v == null || typeof v === 'string' || t('modelsRules.user.civility.string'),
    (v: any) => v == null || v.length <= 10 || t('modelsRules.user.civility.max'),
    (v: any) => v == null || ['M.', 'MME', 'MLLE', 'AUTRE'].includes(v) || t('modelsRules.user.civility.in'),
  ],

  first_name: [
    (v: any) => !!v || t('modelsRules.user.first_name.required'),
    (v: any) => typeof v === 'string' && v.length >= 2 || t('modelsRules.user.first_name.min'),
    (v: any) => typeof v === 'string' && v.length <= 64 || t('modelsRules.user.first_name.max'),
  ],

  last_name: [
    (v: any) => !!v || t('modelsRules.user.last_name.required'),
    (v: any) => typeof v === 'string' && v.length >= 2 || t('modelsRules.user.last_name.min'),
    (v: any) => typeof v === 'string' && v.length <= 64 || t('modelsRules.user.last_name.max'),
  ],

  email: [
    (v: any) => !!v || t('modelsRules.user.email.required'),
    (v: any) => typeof v === 'string' && /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(v) || t('modelsRules.user.email.invalid'),
    (v: any) => typeof v === 'string' && v.length <= 128 || t('modelsRules.user.email.max'),
  ],

  phone: [
    (v: any) => v == null || typeof v === 'string' || t('modelsRules.user.phone.string'),
    (v: any) => v == null || v.length <= 20 || t('modelsRules.user.phone.max'),
  ],

  password: [
    (v: any) => (mode === 'create' ? !!v : true) || t('modelsRules.user.password.required'),
    (v: any) => !v || typeof v === 'string' && v.length >= 8 || t('modelsRules.user.password.min'),
    (v: any) => !v || /[A-Z]/.test(v) || t('modelsRules.user.password.uppercase'),
    (v: any) => !v || /[a-z]/.test(v) || t('modelsRules.user.password.lowercase'),
    (v: any) => !v || /[0-9]/.test(v) || t('modelsRules.user.password.number'),
    (v: any) => !v || /[^A-Za-z0-9]/.test(v) || t('modelsRules.user.password.special'),
  ],

  city: [
    (v: any) => v == null || typeof v === 'string' || t('modelsRules.user.city.string'),
    (v: any) => v == null || v.length <= 64 || t('modelsRules.user.city.max'),
  ],

  address: [
    (v: any) => v == null || typeof v === 'string' || t('modelsRules.user.address.string'),
    (v: any) => v == null || v.length <= 255 || t('modelsRules.user.address.max'),
  ],

  avatar: [
    (v: any) => v == null || typeof v === 'string' || t('modelsRules.user.avatar.string'),
    (v: any) => v == null || v.length <= 255 || t('modelsRules.user.avatar.max'),
  ],

  roles: [
    (v: any) => Array.isArray(v) && v.length > 0 || t('modelsRules.user.roles.required'),
  ],

  client_id: [
    (v: any) => v !== null && v !== undefined || t('modelsRules.user.client_id.required'),
    (v: any) => Number.isInteger(v) && v > 0 || t('modelsRules.user.client_id.required'),
  ],
  verifyEmail: [
    (v: any) => v == null || typeof v === 'boolean' || t('modelsRules.user.verifyEmail.boolean'),
  ],
});
