package com.example.myapplication.database.room.model.item

import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.ForeignKey
import androidx.room.Ignore
import androidx.room.PrimaryKey
import com.example.myapplication.database.room.model.bike.Bike
import org.threeten.bp.LocalDateTime

@Entity(
    tableName = "items", foreignKeys = [
        ForeignKey(
            entity = Bike::class,
            parentColumns = ["uid"],
            childColumns = ["bike_id"],
            onDelete = ForeignKey.CASCADE,
            onUpdate = ForeignKey.RESTRICT
        )
    ]
)
data class Item(
    @PrimaryKey(autoGenerate = true)
    val uid: Int = 0,
    @ColumnInfo(name = "date_time")
    val dateTime: LocalDateTime,
    val weight: Double,
    val radius: Double,
    val colorbike: String,
    @ColumnInfo(name = "max_count")
    val maxCount: Int,
    @ColumnInfo(name = "bike_id", index = true)
    val bikeId: Int = 0,
) {
    @Ignore
    constructor(
        dateTime: LocalDateTime,
        weight: Double,
        radius: Double,
        colorbike: String,
        maxCount: Int,
        bike: Bike,
    ) : this(0, dateTime, weight,radius,colorbike, maxCount, bike.uid)

    companion object {
        fun getItem(index: Int = 0): Item {
            return Item(
                index,
                LocalDateTime.MIN,
                0.0,
                0.0,
                "",
                0,
                0
            )
        }
    }

    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (javaClass != other?.javaClass) return false
        other as Item
        if (uid != other.uid) return false
        if (dateTime != other.dateTime) return false
        if (weight != other.weight) return false
        if (radius != other.radius) return false
        if (colorbike != other.colorbike) return false
        if (maxCount != other.maxCount) return false
        if (bikeId != other.bikeId) return false
        return true
    }

    override fun hashCode(): Int {
        var result = uid
        result = 31 * result + dateTime.hashCode()
        result = 31 * result + weight.hashCode()
        result = 31 * result + radius.hashCode()
        result = 31 * result + colorbike.hashCode()
        result = 31 * result + maxCount.hashCode()
        result = 31 * result + bikeId.hashCode()
        return result
    }
}

