package com.rosetta.messenger.database

import androidx.room.*
import kotlinx.coroutines.flow.Flow

@Dao
interface AccountDao {
    @Query("SELECT * FROM encrypted_accounts WHERE is_active = 1 ORDER BY last_used DESC")
    fun getAllAccountsFlow(): Flow<List<EncryptedAccountEntity>>

    @Query("SELECT * FROM encrypted_accounts WHERE is_active = 1 ORDER BY last_used DESC")
    suspend fun getAllAccounts(): List<EncryptedAccountEntity>

    @Query("SELECT * FROM encrypted_accounts WHERE public_key = :publicKey LIMIT 1")
    suspend fun getAccount(publicKey: String): EncryptedAccountEntity?

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertAccount(account: EncryptedAccountEntity): Long

    @Query("UPDATE encrypted_accounts SET last_used = :lastUsed WHERE public_key = :publicKey")
    suspend fun updateLastUsed(publicKey: String, lastUsed: String)

    @Query("DELETE FROM encrypted_accounts WHERE public_key = :publicKey")
    suspend fun deleteAccount(publicKey: String)

    @Query("SELECT COUNT(*) FROM encrypted_accounts WHERE is_active = 1")
    suspend fun getAccountCount(): Int
}
