package org.jpos.tcpay.db.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.hibernate.annotations.Fetch;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Data
@Accessors(chain = true)
@Entity
@Table(name = "pos_terminal")
public class PosTerminal {
    @Id
    @GeneratedValue
    long id;

    @Column(name="terminalid")
    String terminalid;

    @Column(name="serial_number")
    String serialNumber;

    @OneToOne
    @JoinColumn(name = "pos_merchant_id")
    PosMerchant posMerchant;

    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "pos_terminal_acquirer_terminal",
            joinColumns = @JoinColumn(name = "pos_terminal_id"),
            inverseJoinColumns = @JoinColumn(name = "acquirer_terminal_id")
    )
    List<AcquirerTerminal> acquirerTerminals = new ArrayList<>();

    @Override
    public String toString() {
        return "terminalid:" + terminalid + "\n"
                + "serialNumber:" + serialNumber + "\n"
                + "posMerchant:" + posMerchant.getMerchantid()
                + "acquirerTerminals:" + (Objects.isNull(acquirerTerminals) ? "null"
                : acquirerTerminals.stream().map(AcquirerTerminal::getTerminalId).collect(Collectors.toList()).toString());
    }
}
