package com.ecep.contract.model;

import java.io.Serializable;
import java.util.Map;
import java.util.Objects;

import com.ecep.contract.util.HibernateProxyUtils;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Index;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToOne;
import jakarta.persistence.Table;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@Getter
@Setter
@Entity
@Table(name = "COMPANY_CONTRACT", schema = "supplier_ms", indexes = {
        @Index(name = "RK_CONTRACT_ID", columnList = "CONTRACT_ID"),
        @Index(name = "RK_COMPANY_ID", columnList = "COMPANY_ID")
}, uniqueConstraints = {

})
@ToString(exclude = { "company", "contract" })
public class CompanyContract implements IdentityEntity, CompanyBasedEntity, Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ID", nullable = false)
    private Integer id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "COMPANY_ID")
    private Company company;

    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "CONTRACT_ID")
    private Contract contract;

    /**
     * 要和{@link CompanyContactUtils#getContactKey(Map)}一致
     */
    public static String getContactKey(CompanyContact contact) {
        if (contact == null) {
            return null;
        }
        return contact.getName() +
                contact.getPhone() +
                contact.getEmail();
    }

    @Override
    public final boolean equals(Object object) {
        if (this == object)
            return true;
        if (object == null)
            return false;
        if (HibernateProxyUtils.isNotEffectiveClassEquals(object, this)) {
            return false;
        }
        CompanyContract that = (CompanyContract) object;
        return getId() != null && Objects.equals(getId(), that.getId());
    }

    @Override
    public final int hashCode() {
        return HibernateProxyUtils.hashCode(this);
    }
}
