// src/models/user.ts
// Copyright (C) 2026 Robert Colbert <rob.colbert@openplatform.us>
// All Rights Reserved

import { Schema, model } from "mongoose";

import { IUser, IUserFlags } from "@gadget/api";
import { nanoid } from "nanoid";

export const UserFlagsSchema = new Schema<IUserFlags>(
  {
    isEmailVerified: { type: Boolean, default: false, required: true },
    isAdmin: { type: Boolean, default: false, required: true },
    isTest: { type: Boolean, default: false, required: true },
    isBanned: { type: Boolean, default: false, required: true },
  },
  { _id: false },
);

export const UserSchema = new Schema<IUser>({
  _id: { type: String, default: () => nanoid() },
  email: { type: String, required: true },
  email_lc: { type: String, required: true, lowercase: true, unique: true },
  passwordSalt: { type: String, required: true, select: false },
  password: { type: String, required: true, select: false },
  displayName: { type: String, minlength: 3, maxlength: 30, required: true },
  flags: { type: UserFlagsSchema, required: true },
  persona: { type: String },
});

export const User = model<IUser>("User", UserSchema);
export default User;

(async () => {
  await User.syncIndexes();
})();
