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

@Entity('user_tokens')
export class UserToken {
  @PrimaryGeneratedColumn()
  id!: number;

  @Column({ type: 'int', name: 'user_id' })
  userId!: number;

  @Column({ type: 'varchar', length: 500, name: 'refresh_token' })
  refreshToken!: string;

  @Column({ type: 'varchar', length: 255, nullable: true, name: 'device_info' })
  deviceInfo!: string | null;

  @Column({ type: 'varchar', length: 50, nullable: true, name: 'ip_address' })
  ipAddress!: string | null;

  @Column({ type: 'timestamp', name: 'expires_at' })
  expiresAt!: Date;

  @CreateDateColumn({ name: 'created_at' })
  createdAt!: Date;

  // 关联
  @ManyToOne(() => User, (user) => user.tokens, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'user_id' })
  user!: User;
}
