package com.optix.android.data.local

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import com.optix.android.data.local.entities.NodeEntity
import kotlinx.coroutines.flow.Flow

@Dao
interface NodeDao {
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun upsertAll(nodes: List<NodeEntity>)

    @Query("SELECT * FROM nodes ORDER BY lastSeenAt DESC, ip ASC")
    fun observeAll(): Flow<List<NodeEntity>>

    @Query("DELETE FROM nodes")
    suspend fun clear()

    @Query("UPDATE nodes SET status = 'STALE'")
    suspend fun markAllStale()

    @Query("UPDATE nodes SET status = 'STALE' WHERE nodeId NOT IN (:activeIds)")
    suspend fun markMissing(activeIds: List<String>)
}
