package kr.co.vividnext.sodalive.mypage.alarm.db

import androidx.lifecycle.LiveData
import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Transaction
import androidx.room.Update
import kotlinx.coroutines.flow.Flow

@Dao
interface AlarmDao {
    @Query("SELECT * FROM alarms WHERE id = :id")
    fun getAlarmById(id: Int): LiveData<Alarm>

    @Query("SELECT * FROM alarms")
    fun getAllAlarms(): Flow<List<Alarm>>

    @Query("DELETE FROM alarms")
    fun deleteAllAlarms()

    @Query("DELETE FROM sqlite_sequence WHERE name = 'alarms'")
    fun resetAutoIncrement()

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insertAlarm(alarm: Alarm): Long

    @Update
    fun updateAlarm(alarm: Alarm)

    @Delete
    fun deleteAlarm(alarm: Alarm)

    @Transaction
    fun insertAndReturnAlarm(alarm: Alarm): Alarm {
        val id = insertAlarm(alarm)
        return alarm.copy(id = id.toInt())
    }

    @Transaction
    fun truncateTable() {
        deleteAllAlarms()
        resetAutoIncrement()
    }
}
