import {
  model,
  Model,
  modelAction,
  ModelAutoTypeCheckingMode,
  setGlobalConfig,
  tProp,
  types,
} from 'mobx-keystone';
import { v4 as uuidv4 } from 'uuid';

setGlobalConfig({
  modelAutoTypeChecking: ModelAutoTypeCheckingMode.AlwaysOn,
});

@model('keystone/Application/User')
export default class User extends Model({
  id: tProp(types.string, () => uuidv4()), // an optional string that will use a random id when not provided
  name: tProp(types.string), // a required string
  pincode: tProp(types.string), // an optional boolean that will default to false
  userlevel: tProp(types.number), // an optional boolean that will default to false
  hidden: tProp(types.boolean, false),
  removable: tProp(types.boolean, true),
}) {
  @modelAction
  setPassword(pincode: string) {
    this.pincode = pincode;
  }

  @modelAction
  change(name: string, pincode: string, userlevel: number) {
    this.name = name;
    this.pincode = pincode;
    this.userlevel = userlevel;
  }

  @modelAction
  setName(name: string) {
    this.name = name;
  }
}
