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

export const THIRD_PARTY_ORDER_PROVIDER = 'biedawo';

@Entity('third_party_orders')
@Index('uk_third_party_orders_provider_external', ['provider', 'externalOrderNo'], {
  unique: true,
})
@Index('idx_third_party_orders_last_seen', ['provider', 'lastSeenAt'])
@Index('idx_third_party_orders_local_order', ['localOrderId'])
export class ThirdPartyOrder {
  @PrimaryColumn({ type: 'varchar', length: 36 })
  id: string;

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

  @Column({ name: 'external_order_no', type: 'varchar', length: 160 })
  externalOrderNo: string;

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

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

  @Column({ name: 'course_name', type: 'varchar', length: 255, nullable: true })
  courseName: string | null;

  @Column({ name: 'remote_status', type: 'varchar', length: 120, nullable: true })
  remoteStatus: string | null;

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

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

  @Column({ name: 'first_seen_at', type: 'datetime' })
  firstSeenAt: Date;

  @Column({ name: 'last_seen_at', type: 'datetime' })
  lastSeenAt: Date;

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

  @UpdateDateColumn({ name: 'updated_at', type: 'datetime', precision: 6 })
  updatedAt: Date;
}
