package com.adx.tracking.baidu.model;

import com.fasterxml.jackson.annotation.JsonProperty;

/** 对应 Go baidu.NormalizedBidResponse */
public class NormalizedBidResponse {
    @JsonProperty("reqId") private String reqId;
    @JsonProperty("bidId") private String bidId;
    @JsonProperty("qk")    private String qk;
    @JsonProperty("bid")   private NormalizedBid bid;

    public String getReqId() { return reqId; }
    public void setReqId(String v) { this.reqId = v; }
    public String getBidId() { return bidId; }
    public void setBidId(String v) { this.bidId = v; }
    public String getQk() { return qk; }
    public void setQk(String v) { this.qk = v; }
    public NormalizedBid getBid() { return bid; }
    public void setBid(NormalizedBid v) { this.bid = v; }

    /**
     * 对应 Go NormalizeBidResponse()：从原始 BidResponse 中提取第一个有效 Bid，
     * 统一字段名（showUrl->showUrls, clickUrl->clickUrls 等）。
     */
    public static NormalizedBidResponse from(BidResponse raw) {
        NormalizedBidResponse n = new NormalizedBidResponse();
        n.reqId = raw.getReqId();
        n.bidId = raw.getBidId();
        n.qk    = raw.getQk();

        if (raw.getSeatBid() != null) {
            for (SeatBid seatBid : raw.getSeatBid()) {
                if (seatBid.getBid() != null && !seatBid.getBid().isEmpty()) {
                    Bid b = seatBid.getBid().get(0);
                    NormalizedBid nb = new NormalizedBid();
                    nb.setId(b.getId());
                    nb.setImpId(b.getImpId());
                    nb.setTagId(b.getTagId());
                    nb.setCrid(b.getCrid());
                    nb.setPrice(b.getPrice());
                    nb.setShowUrls(b.getShowUrl());
                    nb.setClickUrls(b.getClickUrl());
                    if (b.getAdm() != null) {
                        nb.setLandingPage(b.getAdm().getLandingPage());
                        nb.setAppStoreLink(b.getAdm().getAppStoreLink());
                        nb.setAdm(b.getAdm());
                    }
                    n.bid = nb;
                    return n;
                }
            }
        }
        return n;
    }
}
