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

@Entity('operation_logs')
export class OperationLog {
  @PrimaryGeneratedColumn()
  id!: number;

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

  @Column({ type: 'varchar', length: 50 })
  action!: string;

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

  @Column({ type: 'int', nullable: true, name: 'target_id' })
  targetId!: number | null;

  @Column({ type: 'json', nullable: true })
  detail!: Record<string, unknown> | null;

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

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

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

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