package com.ecep.contract.model;

import java.time.Year;
import java.util.Objects;

import com.ecep.contract.util.HibernateProxyUtils;

import jakarta.persistence.AttributeOverride;
import jakarta.persistence.AttributeOverrides;
import jakarta.persistence.Column;
import jakarta.persistence.Embedded;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@Getter
@Setter
@ToString
@Entity
@Table(name = "INVENTORY_HISTORY", schema = "supplier_ms")
public class InventoryHistoryPrice implements IdentityEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ID", nullable = false)
    private Integer id;

    @JoinColumn(name = "INVENTORY_ID")
    @ManyToOne(fetch = FetchType.LAZY)
    @ToString.Exclude
    private Inventory inventory;

    @Column(name = "YEAR")
    private Year year;

    public void setYear(int year) {
        this.year = Year.of(year);
    }

    /**
     * 最新价格
     */
    @Embedded
    @AttributeOverrides({
            @AttributeOverride(name = "taxRate", column = @Column(name = "LATEST_PURCHASE_TAX")),
            @AttributeOverride(name = "preTaxPrice", column = @Column(name = "LATEST_PURCHASE_PRICE")),
            @AttributeOverride(name = "postTaxPrice", column = @Column(name = "LATEST_PURCHASE_TAX_PRICE")),
            @AttributeOverride(name = "monthDay", column = @Column(name = "LATEST_PURCHASE_MONTH_DAY"))
    })
    private HistoryPrice latestPurchasePrice = new HistoryPrice();
    @Embedded
    @AttributeOverrides({
            @AttributeOverride(name = "taxRate", column = @Column(name = "LATEST_SALE_TAX")),
            @AttributeOverride(name = "preTaxPrice", column = @Column(name = "LATEST_SALE_PRICE")),
            @AttributeOverride(name = "postTaxPrice", column = @Column(name = "LATEST_SALE_TAX_PRICE")),
            @AttributeOverride(name = "monthDay", column = @Column(name = "LATEST_SALE_MONTH_DAY"))
    })
    private HistoryPrice latestSalePrice = new HistoryPrice();
    /**
     * 最低价格
     */
    @Embedded
    @AttributeOverrides({
            @AttributeOverride(name = "taxRate", column = @Column(name = "MINI_PURCHASE_TAX")),
            @AttributeOverride(name = "preTaxPrice", column = @Column(name = "MINI_PURCHASE_PRICE")),
            @AttributeOverride(name = "postTaxPrice", column = @Column(name = "MINI_PURCHASE_TAX_PRICE")),
            @AttributeOverride(name = "monthDay", column = @Column(name = "MINI_PURCHASE_MONTH_DAY"))
    })
    private HistoryPrice miniPurchasePrice = new HistoryPrice();
    @Embedded
    @AttributeOverrides({
            @AttributeOverride(name = "taxRate", column = @Column(name = "MINI_SALE_TAX")),
            @AttributeOverride(name = "preTaxPrice", column = @Column(name = "MINI_SALE_PRICE")),
            @AttributeOverride(name = "postTaxPrice", column = @Column(name = "MINI_SALE_TAX_PRICE")),
            @AttributeOverride(name = "monthDay", column = @Column(name = "MINI_SALE_MONTH_DAY"))
    })
    private HistoryPrice miniSalePrice = new HistoryPrice();
    /**
     * 最高价格
     */
    @Embedded
    @AttributeOverrides({
            @AttributeOverride(name = "taxRate", column = @Column(name = "MAX_PURCHASE_TAX")),
            @AttributeOverride(name = "preTaxPrice", column = @Column(name = "MAX_PURCHASE_PRICE")),
            @AttributeOverride(name = "postTaxPrice", column = @Column(name = "MAX_PURCHASE_TAX_PRICE")),
            @AttributeOverride(name = "monthDay", column = @Column(name = "MAX_PURCHASE_MONTH_DAY"))
    })
    private HistoryPrice maxPurchasePrice = new HistoryPrice();
    @Embedded
    @AttributeOverrides({
            @AttributeOverride(name = "taxRate", column = @Column(name = "MAX_SALE_TAX")),
            @AttributeOverride(name = "preTaxPrice", column = @Column(name = "MAX_SALE_PRICE")),
            @AttributeOverride(name = "postTaxPrice", column = @Column(name = "MAX_SALE_TAX_PRICE")),
            @AttributeOverride(name = "monthDay", column = @Column(name = "MAX_SALE_MONTH_DAY"))
    })
    private HistoryPrice maxSalePrice = new HistoryPrice();

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

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