import { Entity, Enum, Index, Property } from '@mikro-orm/core';

import { BaseEntity } from '../../../common/entities/base.entity';

export enum RefreshTokenType {
  USER = 'user',
  ADMIN = 'admin',
}

@Entity({ tableName: 'refreshtokens' })
@Index({ properties: ['ownerId', 'restId', 'type'] })
@Index({ properties: ['hashedToken'] })
@Index({ properties: ['expiresAt'] })
export class RefreshToken extends BaseEntity {
  @Property({ type: 'varchar', length: 255 })
  hashedToken!: string;

  @Property()
  ownerId!: string;

  @Property()
  restId!: string;

  @Enum(() => RefreshTokenType)
  type!: RefreshTokenType;

  @Property({ type: 'timestamptz' })
  expiresAt!: Date;
}
