package com.placeholder.sherpai2.data.local.dao

import androidx.room.*
import com.placeholder.sherpai2.data.local.entity.PersonEntity
import kotlinx.coroutines.flow.Flow

@Dao
interface PersonDao {

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insert(person: PersonEntity): Long

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertAll(persons: List<PersonEntity>)

    @Update
    suspend fun update(person: PersonEntity)

    /**
     * FIXED: Removed default parameter
     */
    @Query("UPDATE persons SET updatedAt = :timestamp WHERE id = :personId")
    suspend fun updateTimestamp(personId: String, timestamp: Long)

    @Delete
    suspend fun delete(person: PersonEntity)

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

    @Query("SELECT * FROM persons WHERE id = :personId")
    suspend fun getPersonById(personId: String): PersonEntity?

    @Query("SELECT * FROM persons WHERE id IN (:personIds)")
    suspend fun getPersonsByIds(personIds: List<String>): List<PersonEntity>

    @Query("SELECT * FROM persons ORDER BY name ASC")
    suspend fun getAllPersons(): List<PersonEntity>

    @Query("SELECT * FROM persons ORDER BY name ASC")
    fun getAllPersonsFlow(): Flow<List<PersonEntity>>

    @Query("SELECT * FROM persons WHERE name LIKE '%' || :query || '%' ORDER BY name ASC")
    suspend fun searchByName(query: String): List<PersonEntity>

    @Query("SELECT COUNT(*) FROM persons")
    suspend fun getPersonCount(): Int

    @Query("SELECT EXISTS(SELECT 1 FROM persons WHERE id = :personId)")
    suspend fun personExists(personId: String): Boolean
}