package com.example.shiftalarm

import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Update

@Dao
interface ShiftDao {
    // Override Queries
    @Query("SELECT * FROM shift_overrides WHERE factory = :factory AND team = :team AND date = :date")
    suspend fun getOverride(factory: String, team: String, date: String): ShiftOverride?

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertOverride(override: ShiftOverride)

    @Query("DELETE FROM shift_overrides WHERE factory = :factory AND team = :team AND date = :date")
    suspend fun deleteOverride(factory: String, team: String, date: String)

    @Query("SELECT * FROM shift_overrides WHERE factory = :factory AND team = :team AND date LIKE :month || '%'")
    suspend fun getOverridesForMonth(factory: String, team: String, month: String): List<ShiftOverride>

    @Query("SELECT * FROM shift_overrides")
    suspend fun getAllOverrides(): List<ShiftOverride>

    @Query("DELETE FROM shift_overrides")
    suspend fun clearOverrides()

    // Memo Queries
    @Query("SELECT * FROM daily_memos WHERE date = :date")
    suspend fun getMemo(date: String): DailyMemo?

    @Query("SELECT * FROM daily_memos WHERE date LIKE :month || '%'")
    suspend fun getMemosForMonth(month: String): List<DailyMemo>

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertMemo(memo: DailyMemo)

    @Query("DELETE FROM daily_memos WHERE date = :date")
    suspend fun deleteMemo(date: String)

    @Query("SELECT * FROM daily_memos")
    suspend fun getAllMemos(): List<DailyMemo>

    @Query("DELETE FROM daily_memos")
    suspend fun clearMemos()

    // Custom Alarm Queries
    @Query("SELECT * FROM custom_alarms ORDER BY time ASC")
    suspend fun getAllCustomAlarms(): List<CustomAlarm>

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertCustomAlarm(alarm: CustomAlarm): Long

    @Update
    suspend fun updateCustomAlarm(alarm: CustomAlarm)

    @Delete
    suspend fun deleteCustomAlarm(alarm: CustomAlarm)

    @Query("DELETE FROM custom_alarms")
    suspend fun clearCustomAlarms()

    // Annual Leave Queries
    @Query("SELECT * FROM annual_leave WHERE id = 1")
    suspend fun getAnnualLeave(): AnnualLeave?

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertAnnualLeave(annualLeave: AnnualLeave)

    @Query("UPDATE annual_leave SET remainingDays = :remainingDays, updatedAt = :timestamp WHERE id = 1")
    suspend fun updateRemainingDays(remainingDays: Float, timestamp: Long = System.currentTimeMillis())

    @Query("DELETE FROM annual_leave")
    suspend fun clearAnnualLeave()
}
