package dev.adriankuta.visualizer.components

import android.Manifest
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.height
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.activity.compose.ManagedActivityResultLauncher

/**
 * Shows rationale and a button to request RECORD_AUDIO permission when not granted.
 *
 * @param permissionGranted Whether microphone permission is already granted.
 * @param requestPermissionLauncher Launcher configured for ActivityResultContracts.RequestPermission().
 * @param modifier Compose modifier for layout.
 */
@Composable
fun PermissionSection(
    permissionGranted: Boolean,
    requestPermissionLauncher: ManagedActivityResultLauncher<String, Boolean>,
    modifier: Modifier = Modifier,
) {
    if (permissionGranted) return
    Text("This app needs microphone permission to access the audio output for visualization.", style = MaterialTheme.typography.bodyMedium)
    Spacer(Modifier.height(8.dp))
    Button(onClick = { requestPermissionLauncher.launch(Manifest.permission.RECORD_AUDIO) }) {
        Text("Grant Microphone Permission")
    }
}
