import { Injectable } from '@nestjs/common';
import axios from 'axios';
import AppResponse from 'src/response/app-response';
import { Bid } from '../entities/bid.entity';
import { BidsService } from '../services/bids.service';

@Injectable()
export class AllBidsApi {
  constructor(private readonly bidsService: BidsService) {}

  async getHistoriesBid(lot_id: Bid['lot_id']) {
    const bid = await this.bidsService.bidsRepo.findOne({
      where: { lot_id },
      relations: { web_bid: true },
    });

    try {
      switch (bid.web_bid.origin_url) {
        // GRAYS
        case 'https://www.grays.com': {
          const response = await axios({
            url: `https://www.grays.com/api/LotInfo/GetBiddingHistory?lotId=${lot_id}&currencyCode=AUD`,
          });

          if (response.data && response.data?.Bids) {
            return AppResponse.toResponse(response.data.Bids);
          }

          return AppResponse.toResponse([]);
        }

        // PICKLES
        case 'https://www.pickles.com.au': {
          const response = await axios({
            url: `https://www.pickles.com.au/PWR-Web/services/api/bidHistoryService/bidHistory?item=${lot_id}`,
          });

          if (response.data) {
            return AppResponse.toResponse(response.data.Bids);
          }

          return AppResponse.toResponse([]);
        }
        default:
          return AppResponse.toResponse([]);
      }
    } catch (error) {
      return AppResponse.toResponse([]);
    }
  }
}
