package com.omixlab.lckcontrol.data.local.dao

import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import com.omixlab.lckcontrol.data.local.entity.LinkedAccountEntity
import kotlinx.coroutines.flow.Flow

@Dao
interface LinkedAccountDao {
    @Query("SELECT * FROM linked_accounts")
    fun observeAll(): Flow<List<LinkedAccountEntity>>

    @Query("SELECT * FROM linked_accounts")
    suspend fun getAll(): List<LinkedAccountEntity>

    @Query("SELECT * FROM linked_accounts WHERE serviceId = :serviceId")
    suspend fun getByService(serviceId: String): List<LinkedAccountEntity>

    @Query("SELECT * FROM linked_accounts WHERE id = :id")
    suspend fun getById(id: String): LinkedAccountEntity?

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun upsert(account: LinkedAccountEntity)

    @Delete
    suspend fun delete(account: LinkedAccountEntity)

    @Query("DELETE FROM linked_accounts WHERE id = :id")
    suspend fun deleteById(id: String)

    @Query("DELETE FROM linked_accounts WHERE serviceId = :serviceId")
    suspend fun deleteByService(serviceId: String)

    @Query("UPDATE linked_accounts SET isEnabled = :isEnabled WHERE id = :id")
    suspend fun setEnabled(id: String, isEnabled: Boolean)
}
