import { Schema, model, Model } from "mongoose";
import type { ISmartcarOAuthBridgeState } from "../interfaces/models/ISmartcarOAuthBridgeState";

const SmartcarOAuthBridgeStateSchema = new Schema<ISmartcarOAuthBridgeState>(
  {
    stateToken: {
      type: String,
      required: true,
      unique: true,
      index: true,
    },
    userId: { type: Schema.Types.ObjectId, ref: "User", required: true },
    expiresAt: { type: Date, required: true },
  },
  { timestamps: true }
);

/** Drop expired rows shortly after expiry (Mongo TTL). */
SmartcarOAuthBridgeStateSchema.index({ expiresAt: 1 }, { expireAfterSeconds: 0 });

const SmartcarOAuthBridgeStateModel: Model<ISmartcarOAuthBridgeState> =
  model<ISmartcarOAuthBridgeState>(
    "SmartcarOAuthBridgeState",
    SmartcarOAuthBridgeStateSchema
  );

export default SmartcarOAuthBridgeStateModel;
