// Copyright (C) 2026 Jason M. Schwefel. All Rights Reserved.
package com.coldboreballisticsllc.scopedope.data.ammo

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

@Dao
interface AmmoDao {
    @Query("SELECT * FROM ammo ORDER BY caliber, name")
    fun observeAll(): Flow<List<AmmoEntity>>

    @Query("SELECT DISTINCT caliber FROM ammo ORDER BY caliber")
    suspend fun getCalibers(): List<String>

    @Query("SELECT * FROM ammo WHERE caliber = :caliber ORDER BY name")
    suspend fun getByCaliber(caliber: String): List<AmmoEntity>

    @Query("SELECT * FROM ammo WHERE id = :id")
    suspend fun getById(id: Int): AmmoEntity?

    @Query("SELECT * FROM ammo WHERE name LIKE '%' || :query || '%' OR caliber LIKE '%' || :query || '%' ORDER BY caliber, name")
    suspend fun search(query: String): List<AmmoEntity>

    // Write operations — only used on the user database
    @Insert(onConflict = OnConflictStrategy.ABORT)
    suspend fun insert(entity: AmmoEntity): Long

    @Update
    suspend fun update(entity: AmmoEntity)

    @Delete
    suspend fun delete(entity: AmmoEntity)

    @Query("DELETE FROM ammo WHERE id = :id")
    suspend fun deleteById(id: Int)
}
