import mongoose from "mongoose"
import { PluginFastifyInstance } from "types"
import { v4 as uuidv4 } from "uuid"
const { Schema } = mongoose

export const MODEL_BASENAME = "account"

const accountSchema = new Schema(
  {
    _id: {
      type: String,
      default: () => uuidv4(),
    },

    name: {
      type: String,
      required: true,
      unique: true,
      maxLength: 200,
    },

    active: { type: Boolean, default: true },

    blocked: { type: Boolean, default: false },

    expires: { type: Date, default: null },

    owner: {
      type: Schema.Types.String,
      ref: "raclette__core-user",
      default: null,
    },

    tags: {
      type: [Schema.Types.String],
      ref: "raclette__core-tag",
      default: [],
    },

    isDeleted: { type: Boolean, default: false },
  },
  { timestamps: true },
)

export const createModels = (fastify: PluginFastifyInstance) => {
  return fastify.createModel(MODEL_BASENAME, accountSchema)
}
