// src/models/csrf-token.ts
// Copyright (C) 2025 DTP Technologies, LLC
// All Rights Reserved

import { Types, Schema, Document, model } from "mongoose";

export interface ICsrfToken extends Document {
  _id: Types.ObjectId;
  createdAt: Date;
  expiresAt: Date;
  claimedAt?: Date;
  name: string;
  token: string;
  user?: Types.ObjectId;
  ip?: string;
}

const CsrfTokenSchema = new Schema<ICsrfToken>({
  createdAt: { type: Date, required: true },
  expiresAt: { type: Date, required: true, index: 1 },
  claimedAt: { type: Date },
  name: { type: String, required: true, index: 1 },
  token: { type: String, required: true, unique: true },
  user: { type: Schema.Types.ObjectId, ref: "User", index: 1 },
  ip: { type: String },
});

CsrfTokenSchema.index({ name: 1, token: 1 });

export const CsrfToken = model<ICsrfToken>("CsrfToken", CsrfTokenSchema);
export default CsrfToken;
