package com.tt.honeyDue.platform

import androidx.biometric.BiometricManager
import androidx.biometric.BiometricPrompt
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.platform.LocalContext
import androidx.core.content.ContextCompat
import androidx.fragment.app.FragmentActivity

/**
 * Android implementation of biometric authentication using AndroidX BiometricPrompt.
 */
class AndroidBiometricAuthPerformer(
    private val activity: FragmentActivity
) : BiometricAuthPerformer {

    override fun isBiometricAvailable(): Boolean {
        val biometricManager = BiometricManager.from(activity)
        return biometricManager.canAuthenticate(
            BiometricManager.Authenticators.BIOMETRIC_STRONG or
                BiometricManager.Authenticators.BIOMETRIC_WEAK
        ) == BiometricManager.BIOMETRIC_SUCCESS
    }

    override fun authenticate(
        title: String,
        subtitle: String,
        onResult: (BiometricResult) -> Unit
    ) {
        if (!isBiometricAvailable()) {
            onResult(BiometricResult.NotAvailable)
            return
        }

        val executor = ContextCompat.getMainExecutor(activity)

        val callback = object : BiometricPrompt.AuthenticationCallback() {
            override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
                super.onAuthenticationSucceeded(result)
                onResult(BiometricResult.Success)
            }

            override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
                super.onAuthenticationError(errorCode, errString)
                onResult(BiometricResult.Failed(errString.toString()))
            }

            override fun onAuthenticationFailed() {
                super.onAuthenticationFailed()
                // Don't call onResult here - the system will keep showing the prompt
                // and will eventually call onAuthenticationError if the user cancels
            }
        }

        val biometricPrompt = BiometricPrompt(activity, executor, callback)

        val promptInfo = BiometricPrompt.PromptInfo.Builder()
            .setTitle(title)
            .setSubtitle(subtitle)
            .setNegativeButtonText("Cancel")
            .setAllowedAuthenticators(
                BiometricManager.Authenticators.BIOMETRIC_STRONG or
                    BiometricManager.Authenticators.BIOMETRIC_WEAK
            )
            .build()

        biometricPrompt.authenticate(promptInfo)
    }
}

@Composable
actual fun rememberBiometricAuth(): BiometricAuthPerformer {
    val context = LocalContext.current
    return remember {
        val activity = context as? FragmentActivity
        if (activity != null) {
            AndroidBiometricAuthPerformer(activity)
        } else {
            // Fallback: biometric not available outside FragmentActivity
            object : BiometricAuthPerformer {
                override fun isBiometricAvailable(): Boolean = false
                override fun authenticate(
                    title: String,
                    subtitle: String,
                    onResult: (BiometricResult) -> Unit
                ) {
                    onResult(BiometricResult.NotAvailable)
                }
            }
        }
    }
}
