import { Entity, PrimaryGeneratedColumn, Column, CreateDateColumn, UpdateDateColumn, Index } from 'typeorm';

@Entity('feie_print_task_mt')
@Index(['tenantId'])
@Index(['taskId'], { unique: true })
@Index(['tenantId', 'orderId'])
@Index(['printStatus'])
@Index(['scheduledAt'])
@Index(['createdAt'])
export class FeiePrintTaskMt {
  @PrimaryGeneratedColumn({ unsigned: true })
  id!: number;

  @Column({ name: 'tenant_id', type: 'int', unsigned: true, comment: '租户ID' })
  tenantId!: number;

  @Column({ name: 'task_id', type: 'varchar', length: 100, comment: '飞鹅任务ID' })
  taskId!: string;

  @Column({ name: 'order_id', type: 'int', unsigned: true, nullable: true, comment: '关联订单ID' })
  orderId!: number | null;

  @Column({ name: 'printer_sn', type: 'varchar', length: 50, comment: '打印机序列号' })
  printerSn!: string;

  @Column({ name: 'content', type: 'text', comment: '打印内容' })
  content!: string;

  @Column({ name: 'print_type', type: 'varchar', length: 20, comment: '打印类型: RECEIPT(小票), SHIPPING(发货单), ORDER(订单)' })
  printType!: string;

  @Column({ name: 'print_status', type: 'varchar', length: 20, default: 'PENDING', comment: '打印状态: PENDING, DELAYED(延迟等待), PRINTING, SUCCESS, FAILED, CANCELLED(已取消)' })
  printStatus!: string;

  @Column({ name: 'error_message', type: 'varchar', length: 500, nullable: true, comment: '错误信息' })
  errorMessage!: string | null;

  @Column({ name: 'retry_count', type: 'int', default: 0, comment: '重试次数' })
  retryCount!: number;

  @Column({ name: 'max_retries', type: 'int', default: 3, comment: '最大重试次数' })
  maxRetries!: number;

  @Column({ name: 'scheduled_at', type: 'timestamp', nullable: true, comment: '计划打印时间（用于延迟打印）' })
  scheduledAt!: Date | null;

  @Column({ name: 'printed_at', type: 'timestamp', nullable: true, comment: '打印完成时间' })
  printedAt!: Date | null;

  @Column({ name: 'cancelled_at', type: 'timestamp', nullable: true, comment: '取消时间' })
  cancelledAt!: Date | null;

  @Column({ name: 'cancel_reason', type: 'varchar', length: 100, nullable: true, comment: '取消原因: REFUND(退款), MANUAL(手动取消), TIMEOUT(超时)' })
  cancelReason!: string | null;

  @CreateDateColumn({ name: 'created_at', type: 'timestamp', default: () => 'CURRENT_TIMESTAMP' })
  createdAt!: Date;

  @UpdateDateColumn({ name: 'updated_at', type: 'timestamp', default: () => 'CURRENT_TIMESTAMP', onUpdate: 'CURRENT_TIMESTAMP' })
  updatedAt!: Date;
}