import { BaseEntity, transformerJson } from '../../base/entity/base';
import { Column, Entity, Index } from 'typeorm';

/**
 * 通知发送日志
 * 统一记录不同平台的发送请求和响应。
 */
@Entity('notification_message_log')
@Index(['platform', 'requestId'], { unique: true })
export class NotificationMessageLogEntity extends BaseEntity {
  @Index()
  @Column({ comment: '请求ID', length: 64 })
  requestId: string;

  @Index()
  @Column({ comment: '通知平台', length: 50 })
  platform: string;

  @Column({ comment: '平台应用ID', length: 100, nullable: true })
  appId: string;

  @Column({ comment: '消息类型', length: 50, nullable: true })
  messageType: string;

  @Column({ comment: '接收者ID类型', length: 50, nullable: true })
  receiveIdType: string;

  @Column({ comment: '接收者ID', length: 255, nullable: true })
  receiveId: string;

  @Column({ comment: '业务侧用户ID', length: 100, nullable: true })
  bizUserId: string;

  @Column({ comment: '平台用户ID快照', length: 128, nullable: true })
  externalUserId: string;

  @Column({ comment: 'open_id快照', length: 128, nullable: true })
  openId: string;

  @Column({ comment: '群聊ID快照', length: 128, nullable: true })
  chatId: string;

  @Column({ comment: '消息标题', type: 'varchar', length: 255, nullable: true })
  title: string;

  @Column({ comment: '消息正文', type: 'text' })
  message: string;

  @Column({ comment: '来源类型 skill/api/system', length: 30, nullable: true })
  sourceType: string;

  @Column({ comment: '来源标识', length: 128, nullable: true })
  sourceId: string;

  @Column({ comment: '来源名称', length: 100, nullable: true })
  sourceName: string;

  @Column({
    comment: '状态 pending/success/failed',
    length: 30,
    default: 'pending',
  })
  status: string;

  @Column({ comment: '平台消息ID', length: 128, nullable: true })
  externalMessageId: string;

  @Column({ comment: '平台响应码', length: 64, nullable: true })
  providerCode: string;

  @Column({ comment: '平台响应消息', type: 'text', nullable: true })
  providerMessage: string;

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

  @Column({
    comment: '发送请求体',
    type: 'json',
    transformer: transformerJson,
    nullable: true,
  })
  requestPayload: Record<string, any>;

  @Column({
    comment: '平台实际请求体',
    type: 'json',
    transformer: transformerJson,
    nullable: true,
  })
  providerPayload: Record<string, any>;

  @Column({
    comment: '平台响应体',
    type: 'json',
    transformer: transformerJson,
    nullable: true,
  })
  providerResponse: Record<string, any>;

  @Column({
    comment: '扩展元数据',
    type: 'json',
    transformer: transformerJson,
    nullable: true,
  })
  metadata: Record<string, any>;
}
