# PreferencesExtensions.kt

    /*
     * The MIT License
     *
     * Original work sponsored and donated by The Danish Health Data Authority (http://www.sundhedsdatastyrelsen.dk)
     *
     * Copyright (C) 2025 The Danish Health Data Authority (http://www.sundhedsdatastyrelsen.dk)
     *
     *
     * Permission is hereby granted, free of charge, to any person obtaining a copy of
     * this software and associated documentation files (the "Software"), to deal in
     * the Software without restriction, including without limitation the rights to
     * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
     * of the Software, and to permit persons to whom the Software is furnished to do
     * so, subject to the following conditions:
     *
     *
     * The above copyright notice and this permission notice shall be included in all
     * copies or substantial portions of the Software.
     *
     *
     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
     * SOFTWARE.
     */
    package dk.nsp.mingraviditet.core.data.util

    import android.app.KeyguardManager
    import dk.nsp.mingraviditet.core.data.domain.settings.AppPreferences
    import dk.nsp.mingraviditet.core.data.repository.UserPreferencesRepository
    import dk.nsp.mingraviditet.core.model.misc.SettingSubSectionUrl
    import kotlinx.coroutines.CoroutineScope
    import kotlinx.coroutines.launch

    fun AppPreferences.userHasNotGivenConsent(): Boolean {
        return this.userAcceptedAnonymousAgreement == null && userAcceptedDataAgreement == null
    }

    fun AppPreferences.consentPath(): String = if (this.userAcceptedAnonymousAgreement != null) {
        SettingSubSectionUrl.AnonymousConsent.path
    } else {
        SettingSubSectionUrl.DataConsent.path
    }

    fun AppPreferences.userIsAnonymousWithoutLoginEnabled(keyguardManager: KeyguardManager, userPreferences: UserPreferencesRepository, coroutineScope: CoroutineScope): Boolean {
        if (!this.userAcceptedAnonymousAgreement()) {
            return false
        }
        if (!keyguardManager.isDeviceSecure) {
            coroutineScope.launch {
                userPreferences.setUseBiometrics(false)
            }
            return true
        }
        return !this.useBiometricsForAnonymousApp
    }

    fun AppPreferences.userAcceptedAnonymousAgreement(): Boolean {
        return this.userAcceptedAnonymousAgreement != null
    }

    fun AppPreferences.userHasAcceptedConsent(): Boolean {
        return this.userAcceptedAnonymousAgreement != null || this.userAcceptedDataAgreement != null
    }

    fun AppPreferences.userMustLoginWithMidId(keyguardManager: KeyguardManager): Boolean {
        return (this.userAcceptedDataAgreement() && !this.savedRefreshToken) || userHasDeletedScreenLock(keyguardManager)
    }

    private fun AppPreferences.userHasDeletedScreenLock(keyguardManager: KeyguardManager): Boolean =
        (this.userAcceptedDataAgreement() && this.savedRefreshToken && !keyguardManager.isDeviceSecure)

    fun AppPreferences.userMustAuthenticate(): Boolean {
        return (this.userAcceptedDataAgreement() && this.savedRefreshToken) ||
            (this.userAcceptedAnonymousAgreement() && this.useBiometricsForAnonymousApp)
    }
    fun AppPreferences.userAcceptedDataAgreement(): Boolean {
        return this.userAcceptedDataAgreement != null
    }