package com.github.nullptroma.wallenc.infrastructure.android.db.app.dao

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.Query
import com.github.nullptroma.wallenc.infrastructure.android.db.app.model.DbYandexAccount
import kotlinx.coroutines.flow.Flow

@Dao
interface YandexAccountDao {
    @Query("SELECT * FROM yandex_accounts WHERE yandexUserId = :id LIMIT 1")
    suspend fun getByYandexUserId(id: String): DbYandexAccount?

    @Query("SELECT * FROM yandex_accounts WHERE vaultUuid = :vaultUuid LIMIT 1")
    suspend fun getByVaultUuid(vaultUuid: String): DbYandexAccount?

    @Insert
    suspend fun insert(account: DbYandexAccount)

    @Query(
        "UPDATE yandex_accounts SET oauthToken = :token, email = :email WHERE vaultUuid = :vaultUuid",
    )
    suspend fun updateCredentials(vaultUuid: String, email: String, token: String)

    @Query("SELECT * FROM yandex_accounts ORDER BY email COLLATE NOCASE ASC")
    fun observeAll(): Flow<List<DbYandexAccount>>

    @Query("DELETE FROM yandex_accounts WHERE vaultUuid = :vaultUuid")
    suspend fun deleteByVaultUuid(vaultUuid: String)
}
