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

import androidx.room.*
import kotlinx.coroutines.flow.Flow
import com.placeholder.sherpai2.data.local.entity.FaceModelEntity
/**
 * FaceModelDao - Manages face recognition models
 *
 * PRIMARY KEY TYPE: String (UUID)
 * FOREIGN KEY: personId (String)
 */
@Dao
interface FaceModelDao {

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertFaceModel(faceModel: FaceModelEntity): Long  // Row ID

    @Update
    suspend fun updateFaceModel(faceModel: FaceModelEntity)

    @Query("UPDATE face_models SET lastUsed = :timestamp WHERE id = :faceModelId")
    suspend fun updateLastUsed(faceModelId: String, timestamp: Long)

    @Query("SELECT * FROM face_models WHERE id = :faceModelId")
    suspend fun getFaceModelById(faceModelId: String): FaceModelEntity?

    @Query("SELECT * FROM face_models WHERE personId = :personId AND isActive = 1")
    suspend fun getFaceModelByPersonId(personId: String): FaceModelEntity?

    @Query("SELECT * FROM face_models WHERE isActive = 1 ORDER BY lastUsed DESC")
    suspend fun getAllActiveFaceModels(): List<FaceModelEntity>

    @Query("SELECT * FROM face_models WHERE isActive = 1 ORDER BY lastUsed DESC")
    fun getAllActiveFaceModelsFlow(): Flow<List<FaceModelEntity>>

    @Query("DELETE FROM face_models WHERE id = :faceModelId")
    suspend fun deleteFaceModelById(faceModelId: String)

    @Query("UPDATE face_models SET isActive = 0 WHERE id = :faceModelId")
    suspend fun deactivateFaceModel(faceModelId: String)

    @Query("SELECT COUNT(*) FROM face_models WHERE isActive = 1")
    suspend fun getActiveFaceModelCount(): Int
}