package com.fieldlabel.collector.data.local

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

@Dao
interface CustomFieldDefDao {

    @Query("SELECT * FROM custom_field_defs ORDER BY sortOrder ASC, label ASC")
    fun getAllFlow(): Flow<List<CustomFieldDefEntity>>

    @Query("SELECT * FROM custom_field_defs ORDER BY sortOrder ASC, label ASC")
    suspend fun getAll(): List<CustomFieldDefEntity>

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insert(entity: CustomFieldDefEntity)

    @Delete
    suspend fun delete(entity: CustomFieldDefEntity)

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

    /** 全フィールド定義を並び順で取得（appliesTo フィルタ付き） */
    @Query("SELECT * FROM custom_field_defs WHERE appliesTo IN (:targets) ORDER BY sortOrder ASC, label ASC")
    fun getByAppliesToFlow(targets: List<String>): Flow<List<CustomFieldDefEntity>>
}
