package com.petshop.backend.entity;

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

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

@Entity
@Table(name = "cart")
public class Cart {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long cartId;

    @ManyToOne
    @JoinColumn(name = "userId", nullable = false)
    private User user;

    @ManyToOne
    @JoinColumn(name = "storeId")
    private StoreLocation store;

    @ManyToOne
    @JoinColumn(name = "couponId")
    private Coupon coupon;

    @Column(nullable = false, length = 20)
    private String cartStatus = "ACTIVE";

    @Column(nullable = false, precision = 10, scale = 2)
    private BigDecimal subtotalAmount = BigDecimal.ZERO;

    @Column(nullable = false, precision = 10, scale = 2)
    private BigDecimal discountAmount = BigDecimal.ZERO;

    @Column(nullable = false, precision = 10, scale = 2)
    private BigDecimal totalAmount = BigDecimal.ZERO;

    @Column(nullable = false)
    private Boolean pointsApplied = false;

    @Column(nullable = false, precision = 10, scale = 2)
    private BigDecimal pointsDiscountAmount = BigDecimal.ZERO;

    @Column(nullable = false)
    private Boolean checkoutPending = false;

    @Column(precision = 10, scale = 2)
    private BigDecimal checkoutAmount;

    @Column
    private LocalDateTime checkoutStartedAt;

    @Column(length = 255)
    private String checkoutPaymentIntentId;

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

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

    public Cart() {
    }

    public Long getCartId() {
        return cartId;
    }

    public void setCartId(Long cartId) {
        this.cartId = cartId;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public StoreLocation getStore() {
        return store;
    }

    public void setStore(StoreLocation store) {
        this.store = store;
    }

    public Coupon getCoupon() {
        return coupon;
    }

    public void setCoupon(Coupon coupon) {
        this.coupon = coupon;
    }

    public String getCartStatus() {
        return cartStatus;
    }

    public void setCartStatus(String cartStatus) {
        this.cartStatus = cartStatus;
    }

    public BigDecimal getSubtotalAmount() {
        return subtotalAmount;
    }

    public void setSubtotalAmount(BigDecimal subtotalAmount) {
        this.subtotalAmount = subtotalAmount;
    }

    public BigDecimal getDiscountAmount() {
        return discountAmount;
    }

    public void setDiscountAmount(BigDecimal discountAmount) {
        this.discountAmount = discountAmount;
    }

    public BigDecimal getTotalAmount() {
        return totalAmount;
    }

    public void setTotalAmount(BigDecimal totalAmount) {
        this.totalAmount = totalAmount;
    }

    public Boolean getPointsApplied() {
        return pointsApplied;
    }

    public void setPointsApplied(Boolean pointsApplied) {
        this.pointsApplied = pointsApplied;
    }

    public BigDecimal getPointsDiscountAmount() {
        return pointsDiscountAmount;
    }

    public void setPointsDiscountAmount(BigDecimal pointsDiscountAmount) {
        this.pointsDiscountAmount = pointsDiscountAmount;
    }

    public Boolean getCheckoutPending() {
        return checkoutPending;
    }

    public void setCheckoutPending(Boolean checkoutPending) {
        this.checkoutPending = checkoutPending;
    }

    public BigDecimal getCheckoutAmount() {
        return checkoutAmount;
    }

    public void setCheckoutAmount(BigDecimal checkoutAmount) {
        this.checkoutAmount = checkoutAmount;
    }

    public LocalDateTime getCheckoutStartedAt() {
        return checkoutStartedAt;
    }

    public void setCheckoutStartedAt(LocalDateTime checkoutStartedAt) {
        this.checkoutStartedAt = checkoutStartedAt;
    }

    public String getCheckoutPaymentIntentId() {
        return checkoutPaymentIntentId;
    }

    public void setCheckoutPaymentIntentId(String checkoutPaymentIntentId) {
        this.checkoutPaymentIntentId = checkoutPaymentIntentId;
    }

    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;
        Cart cart = (Cart) o;
        return Objects.equals(cartId, cart.cartId);
    }

    @Override
    public int hashCode() {
        return Objects.hash(cartId);
    }

    @Override
    public String toString() {
        return "Cart{" +
                "cartId=" + cartId +
                ", cartStatus='" + cartStatus + '\'' +
                ", totalAmount=" + totalAmount +
                '}';
    }
}
