package com.placeholder.sherpai2.domain.repository

import com.placeholder.sherpai2.data.local.dao.ImageDao
import com.placeholder.sherpai2.data.local.entity.ImageEntity

/**
 * Extension functions for ImageRepository to support face detection cache
 *
 * Add these methods to your ImageRepository interface or implementation
 */

/**
 * Update face detection cache for a single image
 * Called after detecting faces in an image
 */
suspend fun ImageRepository.updateFaceDetectionCache(
    imageId: String,
    hasFaces: Boolean,
    faceCount: Int
) {
    // Assuming you have access to ImageDao in your repository
    // Adjust based on your actual repository structure
    getImageDao().updateFaceDetectionCache(
        imageId = imageId,
        hasFaces = hasFaces,
        faceCount = faceCount,
        timestamp = System.currentTimeMillis(),
        version = ImageEntity.CURRENT_FACE_DETECTION_VERSION
    )
}

/**
 * Get cache statistics
 * Useful for displaying cache coverage in UI
 */
suspend fun ImageRepository.getFaceCacheStats() =
    getImageDao().getFaceCacheStats()

/**
 * Get images that need face detection
 * For background maintenance tasks
 */
suspend fun ImageRepository.getImagesNeedingFaceDetection() =
    getImageDao().getImagesNeedingFaceDetection()

/**
 * Batch populate face detection cache
 * For initial cache population or maintenance
 */
suspend fun ImageRepository.populateFaceDetectionCache(
    onProgress: (current: Int, total: Int) -> Unit = { _, _ -> }
) {
    val imagesToProcess = getImageDao().getImagesNeedingFaceDetection()
    val total = imagesToProcess.size

    imagesToProcess.forEachIndexed { index, image ->
        try {
            // Detect faces (implement based on your face detection logic)
            val faceCount = detectFaceCount(image.imageUri)

            updateFaceDetectionCache(
                imageId = image.imageId,
                hasFaces = faceCount > 0,
                faceCount = faceCount
            )

            if (index % 10 == 0) {
                onProgress(index, total)
            }
        } catch (e: Exception) {
            // Skip errors, continue with next image
        }
    }

    onProgress(total, total)
}

/**
 * Helper to get ImageDao from repository
 * Adjust based on your actual repository structure
 */
private fun ImageRepository.getImageDao(): ImageDao {
    // This assumes your ImageRepository has a reference to ImageDao
    // Adjust based on your actual implementation:
    // Option 1: If ImageRepository is an interface, add this as a method
    // Option 2: If it's a class, access the dao directly
    // Option 3: Pass ImageDao as a parameter to these functions
    throw NotImplementedError("Implement based on your repository structure")
}

/**
 * Helper to detect face count
 * Implement based on your face detection logic
 */
private suspend fun ImageRepository.detectFaceCount(imageUri: String): Int {
    // Implement your face detection logic here
    // This is a placeholder - adjust based on your FaceDetectionHelper
    throw NotImplementedError("Implement based on your face detection logic")
}

/**
 * ALTERNATIVE: If you prefer to add methods directly to ImageRepository,
 * add these to your ImageRepository interface:
 *
 * interface ImageRepository {
 *     // ... existing methods
 *
 *     suspend fun updateFaceDetectionCache(
 *         imageId: String,
 *         hasFaces: Boolean,
 *         faceCount: Int
 *     )
 *
 *     suspend fun getFaceCacheStats(): FaceCacheStats?
 *
 *     suspend fun getImagesNeedingFaceDetection(): List<ImageEntity>
 *
 *     suspend fun populateFaceDetectionCache(
 *         onProgress: (current: Int, total: Int) -> Unit = { _, _ -> }
 *     )
 * }
 *
 * Then implement these in your ImageRepositoryImpl class.
 */