/*
 * Copyright (c) 2026 QPQ IaaS AG <info@qpq.swiss>. All rights reserved.
 * Project: Gajumaru Core Java Libraries <gajumaru.io>
 *
 * This program is dual-licensed:
 * 1) Under the GNU Affero General Public License as published by the Free
 *    Software Foundation, either version 3 of the License, or (at your option)
 *    any later version (AGPL-3.0-or-later).
 *
 * 2) Under a commercial/proprietary license available directly from QPQ IaaS AG.
 *    If you wish to use this software outside the strict constraints of the
 *    AGPLv3 (e.g. within a closed-source or proprietary product), you must
 *    purchase a commercial license from QPQ IaaS AG.
 *
 * Authors:
 *   - Craig Everett <craigeverett@qpq.swiss>
 *
 * SPDX-License-Identifier: AGPL-3.0-or-later OR LicenseRef-QPQ-Commercial
 */

package swiss.qpq.gajumobile.ui.screens

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.safeDrawingPadding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import swiss.qpq.gajumobile.R
import swiss.qpq.gajumobile.ui.components.GajuButton
import swiss.qpq.gajumobile.ui.components.GajuHeader
import swiss.qpq.gajumobile.ui.components.SecurityDataSection

@Composable
fun EnvironmentInfoScreen(
    onBack: () -> Unit
) {
    val context = androidx.compose.ui.platform.LocalContext.current
    val hasStrongBox = remember {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.P) {
            context.packageManager.hasSystemFeature(android.content.pm.PackageManager.FEATURE_STRONGBOX_KEYSTORE)
        } else false
    }
    
    val appVersion = remember {
        try {
            val pInfo = context.packageManager.getPackageInfo(context.packageName, 0)
            "${pInfo.versionName} (${androidx.core.content.pm.PackageInfoCompat.getLongVersionCode(pInfo)})"
        } catch (_: Exception) {
            "Unknown"
        }
    }

    Column(
        modifier = Modifier
            .fillMaxSize()
            .padding(24.dp)
            .safeDrawingPadding()
            .verticalScroll(rememberScrollState()),
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        GajuHeader()
        Text(
            stringResource(R.string.menu_environment_info),
            style = MaterialTheme.typography.headlineMedium,
            color = MaterialTheme.colorScheme.onBackground
        )
        
        Spacer(modifier = Modifier.height(24.dp))

        SecurityDataSection(
            title = stringResource(R.string.security_hardware_title),
            data = listOf(
                stringResource(R.string.keystore_label) to if (hasStrongBox) stringResource(R.string.strongbox_label) else stringResource(R.string.hardware_tee_label),
                stringResource(R.string.auth_enforcement_label) to stringResource(R.string.os_lock_required_label),
                stringResource(R.string.encryption_label) to stringResource(R.string.hw_generated_ivs_label)
            )
        )

        SecurityDataSection(
            title = stringResource(R.string.device_identity_title),
            data = listOf(
                stringResource(R.string.manufacturer_label) to android.os.Build.MANUFACTURER,
                stringResource(R.string.model_label) to android.os.Build.MODEL,
                stringResource(R.string.board_label) to android.os.Build.BOARD,
                stringResource(R.string.hardware_label) to android.os.Build.HARDWARE
            )
        )

        SecurityDataSection(
            title = stringResource(R.string.system_build_title),
            data = listOf(
                stringResource(R.string.android_version_label) to "${android.os.Build.VERSION.RELEASE} (API ${android.os.Build.VERSION.SDK_INT})",
                stringResource(R.string.security_patch_label) to android.os.Build.VERSION.SECURITY_PATCH,
                stringResource(R.string.fingerprint_label) to android.os.Build.FINGERPRINT
            )
        )

        SecurityDataSection(
            title = "GajuMobile",
            data = listOf(
                stringResource(R.string.app_version_label) to appVersion,
                stringResource(R.string.theme_label) to stringResource(R.string.theme_name)
            )
        )

        Spacer(modifier = Modifier.weight(1f))
        
        GajuButton(text = stringResource(R.string.done_button), onClick = onBack)
    }
}
