import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  Index,
  ManyToOne,
  JoinColumn,
} from 'typeorm';
import { User } from './user.entity.js';

export enum TokenType {
  ACCESS = 'access',
  REFRESH = 'refresh',
}

@Entity({ schema: 'auth', name: 'token_blacklist' })
@Index('idx_token_blacklist_expires', ['expiresAt'])
@Index('idx_token_blacklist_jti', ['jti'])
export class TokenBlacklist {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Column({ type: 'varchar', length: 255, unique: true, nullable: false })
  jti: string;

  @Column({ type: 'uuid', nullable: false, name: 'user_id' })
  userId: string;

  @Column({
    type: 'varchar',
    length: 20,
    nullable: false,
    name: 'token_type',
  })
  tokenType: string;

  // Metadata
  @Column({ type: 'varchar', length: 100, nullable: true })
  reason: string | null;

  @Column({ type: 'uuid', nullable: true, name: 'revoked_by' })
  revokedBy: string | null;

  // Timestamps
  @Column({ type: 'timestamptz', nullable: false, name: 'expires_at' })
  expiresAt: Date;

  @Column({
    type: 'timestamptz',
    nullable: true,
    name: 'revoked_at',
    default: () => 'CURRENT_TIMESTAMP',
  })
  revokedAt: Date | null;

  // Relations
  @ManyToOne(() => User, {
    onDelete: 'CASCADE',
  })
  @JoinColumn({ name: 'user_id' })
  user: User;

  @ManyToOne(() => User, { nullable: true })
  @JoinColumn({ name: 'revoked_by' })
  revokedByUser: User | null;
}
