import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm';

@Entity('standing_order_executions')
export class StandingOrderExecution {
  @PrimaryGeneratedColumn('uuid')
  id!: string;

  @Column({ type: 'varchar', length: 20 })
  tenantId!: string;

  @Column({ type: 'varchar', length: 30 })
  orderId!: string;

  @Column({ type: 'varchar', length: 20 })
  triggerType!: string;

  @Column({ type: 'jsonb', nullable: true })
  triggerContext?: Record<string, unknown>;

  @Column({ type: 'varchar', length: 100, nullable: true })
  sessionId?: string;

  @Column({ type: 'varchar', length: 20, default: 'running' })
  status!: 'running' | 'completed' | 'failed' | 'awaiting_human' | 'aborted_by_human';

  @Column({ type: 'text', nullable: true })
  resultSummary?: string;

  @Column({ type: 'jsonb', default: [] })
  actionsTaken!: Record<string, unknown>[];

  @Column({ type: 'text', nullable: true })
  error?: string;

  @Column({ type: 'timestamptz', nullable: true })
  escalatedAt?: Date;

  @Column({ type: 'varchar', length: 20, nullable: true })
  escalationChannel?: string;

  @Column({ type: 'jsonb', nullable: true })
  humanResponse?: Record<string, unknown>;

  @Column({ type: 'uuid', nullable: true })
  respondedBy?: string;

  @Column({ type: 'timestamptz', nullable: true })
  respondedAt?: Date;

  @Column({ type: 'timestamptz', default: () => 'NOW()' })
  startedAt!: Date;

  @Column({ type: 'timestamptz', nullable: true })
  completedAt?: Date;

  @Column({ type: 'int', nullable: true })
  durationMs?: number;
}
