import {
  Column,
  CreateDateColumn,
  Entity,
  Index,
  PrimaryColumn,
} from 'typeorm';
import type { SourceType } from '../../third-party/entities/category.entity';

export type OrderActionStatus = 'success' | 'failed';

@Entity('order_actions')
@Index('idx_order_actions_order_id', ['orderId', 'createdAt'])
@Index('idx_order_actions_action_created', ['action', 'createdAt'])
export class OrderAction {
  @PrimaryColumn({ type: 'varchar', length: 36 })
  id: string;

  @Column({ name: 'order_id', type: 'varchar', length: 36 })
  orderId: string;

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

  @Column({ name: 'source_type', type: 'varchar', length: 32 })
  sourceType: SourceType;

  @Column({ type: 'varchar', length: 64, nullable: true })
  provider: string | null;

  @Column({ type: 'varchar', length: 32 })
  status: OrderActionStatus;

  @Column({ name: 'operator_id', type: 'varchar', length: 36, nullable: true })
  operatorId: string | null;

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

  @Column({ name: 'response_payload', type: 'json', nullable: true })
  responsePayload: unknown | null;

  @Column({ name: 'error_message', type: 'varchar', length: 500, nullable: true })
  errorMessage: string | null;

  @Column({ type: 'varchar', length: 500, nullable: true })
  remark: string | null;

  @CreateDateColumn({ name: 'created_at', type: 'datetime', precision: 6 })
  createdAt: Date;
}
