/**
 * 管理员操作日志实体
 * 
 * 功能描述：
 * - 记录管理员的所有数据库操作
 * - 提供详细的审计跟踪
 * - 支持操作前后数据状态记录
 * - 便于安全审计和问题排查
 * 
 * 职责分离：
 * - 数据持久化：操作日志的数据库存储
 * - 审计跟踪：完整的操作历史记录
 * - 安全监控：敏感操作的详细记录
 * - 问题排查：操作异常的详细信息
 * 
 * 最近修改：
 * - 2026-01-08: 注释规范优化 - 修正@author字段，更新版本号和修改记录 (修改者: moyin)
 * - 2026-01-08: 功能新增 - 创建管理员操作日志实体 (修改者: assistant)
 * 
 * @author moyin
 * @version 1.0.1
 * @since 2026-01-08
 * @lastModified 2026-01-08
 */

import { Entity, PrimaryGeneratedColumn, Column, CreateDateColumn, Index } from 'typeorm';
import { OPERATION_TYPES, OPERATION_RESULTS } from './admin_constants';

@Entity('admin_operation_logs')
@Index(['admin_user_id', 'created_at'])
@Index(['operation_type', 'created_at'])
@Index(['target_type', 'target_id'])
export class AdminOperationLog {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Column({ type: 'varchar', length: 50, comment: '管理员用户ID' })
  @Index()
  admin_user_id: string;

  @Column({ type: 'varchar', length: 100, comment: '管理员用户名' })
  admin_username: string;

  @Column({ type: 'varchar', length: 50, comment: '操作类型 (CREATE/UPDATE/DELETE/QUERY/BATCH)' })
  operation_type: keyof typeof OPERATION_TYPES;

  @Column({ type: 'varchar', length: 100, comment: '目标资源类型 (users/user_profiles/zulip_accounts)' })
  target_type: string;

  @Column({ type: 'varchar', length: 50, nullable: true, comment: '目标资源ID' })
  target_id?: string;

  @Column({ type: 'varchar', length: 200, comment: '操作描述' })
  operation_description: string;

  @Column({ type: 'varchar', length: 100, comment: 'HTTP方法和路径' })
  http_method_path: string;

  @Column({ type: 'json', nullable: true, comment: '请求参数' })
  request_params?: Record<string, any>;

  @Column({ type: 'json', nullable: true, comment: '操作前数据状态' })
  before_data?: Record<string, any>;

  @Column({ type: 'json', nullable: true, comment: '操作后数据状态' })
  after_data?: Record<string, any>;

  @Column({ type: 'varchar', length: 20, comment: '操作结果 (SUCCESS/FAILED)' })
  operation_result: keyof typeof OPERATION_RESULTS;

  @Column({ type: 'text', nullable: true, comment: '错误信息' })
  error_message?: string;

  @Column({ type: 'varchar', length: 50, nullable: true, comment: '错误码' })
  error_code?: string;

  @Column({ type: 'int', comment: '操作耗时(毫秒)' })
  duration_ms: number;

  @Column({ type: 'varchar', length: 45, nullable: true, comment: '客户端IP地址' })
  client_ip?: string;

  @Column({ type: 'varchar', length: 500, nullable: true, comment: '用户代理' })
  user_agent?: string;

  @Column({ type: 'varchar', length: 50, comment: '请求ID' })
  request_id: string;

  @Column({ type: 'json', nullable: true, comment: '额外的上下文信息' })
  context?: Record<string, any>;

  @CreateDateColumn({ comment: '创建时间' })
  created_at: Date;

  @Column({ type: 'boolean', default: false, comment: '是否为敏感操作' })
  is_sensitive: boolean;

  @Column({ type: 'int', default: 0, comment: '影响的记录数量' })
  affected_records: number;

  @Column({ type: 'varchar', length: 100, nullable: true, comment: '批量操作的批次ID' })
  batch_id?: string;
}