import {
  Column,
  Entity,
  ManyToOne,
  PrimaryGeneratedColumn,
  Unique,
} from 'typeorm';
import { Bid } from './bid.entity';
import { Timestamp } from './timestamp';
import { WebBid } from './wed-bid.entity';

@Entity('bid_metadata')
@Unique(['key_name', 'bid'])
export class BidMetadata extends Timestamp {
  // value 'live' | 'sandbox'
  public static MODE_KEY = 'mode_key';

  public static DEMO_RESPONSE = 'demo_response';
  public static LATEST_RECROD_LINK = 'lastest_record_link';

  // value is minutes esg: arrival_offset_seconds of web bid parent
  public static ARRIAVAL_OFFSET_SECONDS_LIVE = 'arrival_offset_seconds_live';
  public static ARRIAVAL_OFFSET_SECONDS_SANDBOX =
    'arrival_offset_seconds_sandbox';

  // value is minutes esg: early_tracking_seconds of web bid parent
  public static EARLY_TRACKING_SECONDS_LIVE = 'early_tracking_seconds_live';
  public static EARLY_TRACKING_SECONDS_SANDBOX =
    'early_tracking_seconds_sandbox';

  public static DEFAULT_META_DATA = (webBid: WebBid) => {
    return [
      {
        key_name: BidMetadata.MODE_KEY,
        value: JSON.stringify('live'),
      },
      {
        key_name: BidMetadata.ARRIAVAL_OFFSET_SECONDS_LIVE,
        value: String(webBid.arrival_offset_seconds),
      },
      {
        key_name: BidMetadata.EARLY_TRACKING_SECONDS_LIVE,
        value: String(webBid.early_tracking_seconds),
      },
      {
        key_name: BidMetadata.ARRIAVAL_OFFSET_SECONDS_SANDBOX,
        value: String(webBid.arrival_offset_seconds),
      },
      {
        key_name: BidMetadata.EARLY_TRACKING_SECONDS_SANDBOX,
        value: String(webBid.early_tracking_seconds),
      },
    ] as BidMetadata[];
  };

  @PrimaryGeneratedColumn('increment')
  id: number;

  @Column()
  key_name: string;

  @Column({ type: 'json' })
  value: string;

  @ManyToOne(() => Bid, (bid) => bid.metadata, { onDelete: 'CASCADE' })
  bid: Bid;
}
