package com.rssuper.database.entities

import androidx.room.Entity
import androidx.room.ForeignKey
import androidx.room.Index
import androidx.room.PrimaryKey
import com.rssuper.models.HttpAuth
import java.util.Date

@Entity(
    tableName = "subscriptions",
    indices = [Index(value = ["url"], unique = true)]
)
data class SubscriptionEntity(
    @PrimaryKey
    val id: String,
    
    val url: String,
    
    val title: String,
    
    val category: String? = null,
    
    val enabled: Boolean = true,
    
    val fetchInterval: Long = 3600000,
    
    val createdAt: Date,
    
    val updatedAt: Date,
    
    val lastFetchedAt: Date? = null,
    
    val nextFetchAt: Date? = null,
    
    val error: String? = null,
    
    val httpAuthUsername: String? = null,
    
    val httpAuthPassword: String? = null
) {
    fun toHttpAuth(): HttpAuth? {
        return if (httpAuthUsername != null && httpAuthPassword != null) {
            HttpAuth(httpAuthUsername, httpAuthPassword)
        } else null
    }
    
    fun fromHttpAuth(auth: HttpAuth?): SubscriptionEntity {
        return copy(
            httpAuthUsername = auth?.username,
            httpAuthPassword = auth?.password
        )
    }
}
