// app/models/web-token.ts
// Copyright (C) 2026 Robert Colbert <rob.colbert@openplatform.us>
// All Rights Reserved

import { Schema, model } from "mongoose";

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

import { nanoid } from "nanoid";

export interface IWebToken {
  _id: GadgetId;
  created: Date;
  expires: Date;
  user: IUser | GadgetId;
  token: string;
}
export const WebTokenSchema = new Schema<IWebToken>({
  _id: { type: String, default: () => nanoid() },
  created: { type: Date, required: true, index: 1 },
  expires: { type: Date, required: true, index: -1 },
  user: { type: String, required: true, ref: "User" },
  token: { type: String, required: true },
});

export const WebToken = model<IWebToken>("WebToken", WebTokenSchema);
export default WebToken;

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