package com.placeholder.sherpai2.ml

/**
 * ThresholdStrategy - Smart threshold selection for face recognition
 *
 * Considers:
 * - Training image count
 * - Image quality
 * - Detection context (group photo, selfie, etc.)
 */
object ThresholdStrategy {

    /**
     * Get optimal threshold for face recognition
     *
     * @param trainingCount Number of images used to train the model
     * @param imageQuality Quality assessment of the image being scanned
     * @param detectionContext Context of the detection (group, selfie, etc.)
     * @return Similarity threshold (0.0 - 1.0)
     */
    fun getOptimalThreshold(
        trainingCount: Int,
        imageQuality: ImageQuality = ImageQuality.UNKNOWN,
        detectionContext: DetectionContext = DetectionContext.GENERAL
    ): Float {
        // Base threshold from training count
        val baseThreshold = when {
            trainingCount >= 40 -> 0.68f  // High confidence - strict
            trainingCount >= 30 -> 0.62f  // Good confidence - moderate-strict
            trainingCount >= 20 -> 0.56f  // Moderate confidence
            trainingCount >= 15 -> 0.50f  // Acceptable confidence - lenient
            else -> 0.48f                  // Sparse training - very lenient
        }

        // Adjust based on image quality
        val qualityAdjustment = when (imageQuality) {
            ImageQuality.HIGH -> -0.02f    // Can be stricter with good quality
            ImageQuality.MEDIUM -> 0f      // No change
            ImageQuality.LOW -> +0.03f     // Be more lenient with poor quality
            ImageQuality.UNKNOWN -> 0f     // No change
        }

        // Adjust based on detection context
        val contextAdjustment = when (detectionContext) {
            DetectionContext.GROUP_PHOTO -> +0.02f   // More lenient in groups (faces smaller)
            DetectionContext.SELFIE -> -0.03f         // Stricter for close-ups (more detail)
            DetectionContext.PROFILE -> +0.02f        // More lenient for side profiles
            DetectionContext.DISTANT -> +0.03f        // More lenient for far away faces
            DetectionContext.GENERAL -> 0f            // No change
        }

        // Combine adjustments and clamp to safe range
        return (baseThreshold + qualityAdjustment + contextAdjustment).coerceIn(0.40f, 0.75f)
    }

    /**
     * Get threshold for liberal matching (e.g., during testing)
     */
    fun getLiberalThreshold(trainingCount: Int): Float {
        return when {
            trainingCount >= 30 -> 0.52f
            trainingCount >= 20 -> 0.48f
            else -> 0.45f
        }.coerceIn(0.40f, 0.65f)
    }

    /**
     * Get threshold for conservative matching (minimize false positives)
     */
    fun getConservativeThreshold(trainingCount: Int): Float {
        return when {
            trainingCount >= 40 -> 0.72f
            trainingCount >= 30 -> 0.68f
            trainingCount >= 20 -> 0.62f
            else -> 0.58f
        }.coerceIn(0.55f, 0.75f)
    }

    /**
     * Estimate image quality from bitmap properties
     */
    fun estimateImageQuality(width: Int, height: Int, fileSize: Long = 0): ImageQuality {
        val megapixels = (width * height) / 1_000_000f

        return when {
            megapixels > 4.0f -> ImageQuality.HIGH
            megapixels > 1.0f -> ImageQuality.MEDIUM
            else -> ImageQuality.LOW
        }
    }

    /**
     * Estimate detection context from face count and face size
     */
    fun estimateDetectionContext(
        faceCount: Int,
        faceAreaRatio: Float = 0f
    ): DetectionContext {
        return when {
            faceCount == 1 && faceAreaRatio > 0.15f -> DetectionContext.SELFIE
            faceCount == 1 && faceAreaRatio < 0.05f -> DetectionContext.DISTANT
            faceCount >= 3 -> DetectionContext.GROUP_PHOTO
            else -> DetectionContext.GENERAL
        }
    }
}

/**
 * Image quality assessment
 */
enum class ImageQuality {
    HIGH,     // > 4MP, good lighting
    MEDIUM,   // 1-4MP
    LOW,      // < 1MP, poor quality
    UNKNOWN   // Cannot determine
}

/**
 * Detection context
 */
enum class DetectionContext {
    GROUP_PHOTO,  // Multiple faces (3+)
    SELFIE,       // Single face, close-up
    PROFILE,      // Side view
    DISTANT,      // Face is small in frame
    GENERAL       // Default
}