package com.rssuper.database.daos

import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Update
import com.rssuper.database.entities.FeedItemEntity
import kotlinx.coroutines.flow.Flow
import java.util.Date

@Dao
interface FeedItemDao {
    @Query("SELECT * FROM feed_items WHERE subscriptionId = :subscriptionId ORDER BY published DESC")
    fun getItemsBySubscription(subscriptionId: String): Flow<List<FeedItemEntity>>
    
    @Query("SELECT * FROM feed_items WHERE id = :id")
    suspend fun getItemById(id: String): FeedItemEntity?
    
    @Query("SELECT * FROM feed_items WHERE subscriptionId IN (:subscriptionIds) ORDER BY published DESC")
    fun getItemsBySubscriptions(subscriptionIds: List<String>): Flow<List<FeedItemEntity>>
    
    @Query("SELECT * FROM feed_items WHERE isRead = 0 ORDER BY published DESC")
    fun getUnreadItems(): Flow<List<FeedItemEntity>>
    
    @Query("SELECT * FROM feed_items WHERE isStarred = 1 ORDER BY published DESC")
    fun getStarredItems(): Flow<List<FeedItemEntity>>
    
    @Query("SELECT * FROM feed_items WHERE published > :date ORDER BY published DESC")
    fun getItemsAfterDate(date: Date): Flow<List<FeedItemEntity>>
    
    @Query("SELECT * FROM feed_items WHERE subscriptionId = :subscriptionId AND published > :date ORDER BY published DESC")
    fun getSubscriptionItemsAfterDate(subscriptionId: String, date: Date): Flow<List<FeedItemEntity>>
    
    @Query("SELECT COUNT(*) FROM feed_items WHERE subscriptionId = :subscriptionId AND isRead = 0")
    fun getUnreadCount(subscriptionId: String): Flow<Int>
    
    @Query("SELECT COUNT(*) FROM feed_items WHERE isRead = 0")
    fun getTotalUnreadCount(): Flow<Int>
    
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertItem(item: FeedItemEntity): Long
    
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertItems(items: List<FeedItemEntity>): List<Long>
    
    @Update
    suspend fun updateItem(item: FeedItemEntity): Int
    
    @Delete
    suspend fun deleteItem(item: FeedItemEntity): Int
    
    @Query("DELETE FROM feed_items WHERE id = :id")
    suspend fun deleteItemById(id: String): Int
    
    @Query("DELETE FROM feed_items WHERE subscriptionId = :subscriptionId")
    suspend fun deleteItemsBySubscription(subscriptionId: String): Int
    
    @Query("UPDATE feed_items SET isRead = 1 WHERE id = :id")
    suspend fun markAsRead(id: String): Int
    
    @Query("UPDATE feed_items SET isRead = 0 WHERE id = :id")
    suspend fun markAsUnread(id: String): Int
    
    @Query("UPDATE feed_items SET isStarred = 1 WHERE id = :id")
    suspend fun markAsStarred(id: String): Int
    
    @Query("UPDATE feed_items SET isStarred = 0 WHERE id = :id")
    suspend fun markAsUnstarred(id: String): Int
    
    @Query("UPDATE feed_items SET isRead = 1 WHERE subscriptionId = :subscriptionId")
    suspend fun markAllAsRead(subscriptionId: String): Int
    
    @Query("SELECT * FROM feed_items WHERE subscriptionId = :subscriptionId LIMIT :limit OFFSET :offset")
    suspend fun getItemsPaginated(subscriptionId: String, limit: Int, offset: Int): List<FeedItemEntity>
}
