package com.zyzf.coffeepreorder.database.model

import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.ForeignKey
import androidx.room.Ignore
import androidx.room.PrimaryKey

@Entity(tableName = "cart", foreignKeys = [
    ForeignKey(
        entity = Cart::class,
        parentColumns = ["uid"],
        childColumns = ["coffee_id"],
        onDelete = ForeignKey.RESTRICT,
        onUpdate = ForeignKey.RESTRICT
    )
])
data class Cart(
    @PrimaryKey(autoGenerate = true)
    val uid: Int = 0,
    @ColumnInfo(name = "coffee_id", index = true)
    val coffeeId: Int,
    @ColumnInfo(name = "count", index = true)
    val count: Int = 0
) {
    @Ignore
    constructor(
        coffeeId: Int,
        count: Int
    ) : this(0, coffeeId, count)

    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (javaClass != other?.javaClass) return false
        other as Cart
        if (uid != other.uid) return false
        if (coffeeId != other.coffeeId) return false
        return count == other.count
    }

    override fun hashCode(): Int {
        var result = uid
        result = 31 * result + coffeeId.hashCode()
        result = 31 * result + count.hashCode()
        return result
    }
}
