import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { HydratedDocument } from 'mongoose';

export type SuperAdminRefreshTokenDocument = HydratedDocument<SuperAdminRefreshToken>;

@Schema({ timestamps: true, versionKey: false })
export class SuperAdminRefreshToken {
  @Prop({ required: true, trim: true, lowercase: true, index: true })
  adminEmail!: string;

  @Prop({ required: true, trim: true, index: true })
  jti!: string;

  @Prop({ required: true, select: false })
  tokenHash!: string;

  @Prop({ required: true })
  expiresAt!: Date;

  @Prop({ default: false })
  revoked!: boolean;
}

export const SuperAdminRefreshTokenSchema = SchemaFactory.createForClass(SuperAdminRefreshToken);
SuperAdminRefreshTokenSchema.index({ adminEmail: 1, revoked: 1 });
SuperAdminRefreshTokenSchema.index({ adminEmail: 1, jti: 1 }, { unique: true, sparse: true });
SuperAdminRefreshTokenSchema.index({ expiresAt: 1 }, { expireAfterSeconds: 0 });
