package com.petshop.backend.entity;

import jakarta.persistence.*;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;

import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Objects;

@Entity
@Table(name = "productSupplier")
@IdClass(ProductSupplier.ProductSupplierId.class)
public class ProductSupplier {

    @Id
    @ManyToOne
    @JoinColumn(name = "prodId", nullable = false)
    private Product product;

    @Id
    @ManyToOne
    @JoinColumn(name = "supId", nullable = false)
    private Supplier supplier;

    @Column(nullable = false, precision = 10, scale = 2)
    private BigDecimal cost;

    @CreationTimestamp
    @Column(name = "created_at", updatable = false)
    private LocalDateTime createdAt;

    @UpdateTimestamp
    @Column(name = "updated_at")
    private LocalDateTime updatedAt;

    public ProductSupplier() {
    }

    public ProductSupplier(Product product, Supplier supplier, BigDecimal cost, LocalDateTime createdAt, LocalDateTime updatedAt) {
        this.product = product;
        this.supplier = supplier;
        this.cost = cost;
        this.createdAt = createdAt;
        this.updatedAt = updatedAt;
    }

    public Product getProduct() {
        return product;
    }

    public void setProduct(Product product) {
        this.product = product;
    }

    public Supplier getSupplier() {
        return supplier;
    }

    public void setSupplier(Supplier supplier) {
        this.supplier = supplier;
    }

    public BigDecimal getCost() {
        return cost;
    }

    public void setCost(BigDecimal cost) {
        this.cost = cost;
    }

    public LocalDateTime getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(LocalDateTime createdAt) {
        this.createdAt = createdAt;
    }

    public LocalDateTime getUpdatedAt() {
        return updatedAt;
    }

    public void setUpdatedAt(LocalDateTime updatedAt) {
        this.updatedAt = updatedAt;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        ProductSupplier that = (ProductSupplier) o;
        return Objects.equals(product, that.product) && Objects.equals(supplier, that.supplier);
    }

    @Override
    public int hashCode() {
        return Objects.hash(product, supplier);
    }

    @Override
    public String toString() {
        return "ProductSupplier{" +
                "product=" + product +
                ", supplier=" + supplier +
                ", cost=" + cost +
                ", createdAt=" + createdAt +
                ", updatedAt=" + updatedAt +
                '}';
    }

    public static class ProductSupplierId implements Serializable {
        private Long product;
        private Long supplier;

        public ProductSupplierId() {
        }

        public ProductSupplierId(Long product, Long supplier) {
            this.product = product;
            this.supplier = supplier;
        }

        public Long getProduct() {
            return product;
        }

        public void setProduct(Long product) {
            this.product = product;
        }

        public Long getSupplier() {
            return supplier;
        }

        public void setSupplier(Long supplier) {
            this.supplier = supplier;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            ProductSupplierId that = (ProductSupplierId) o;
            return Objects.equals(product, that.product) && Objects.equals(supplier, that.supplier);
        }

        @Override
        public int hashCode() {
            return Objects.hash(product, supplier);
        }

        @Override
        public String toString() {
            return "ProductSupplierId{" +
                    "product=" + product +
                    ", supplier=" + supplier +
                    '}';
        }
    }
}
