package dev.privacyllc.period.feature.common

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Row
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.semantics.clearAndSetSemantics
import androidx.compose.ui.semantics.contentDescription
import androidx.compose.ui.unit.dp
import dev.privacyllc.period.domain.prediction.ConfidenceLabel

/**
 * How sure the app is, in the one form it says it.
 *
 * Shared rather than copied, because two of these drifting apart is a screen
 * telling a user "Medium" in dots and "Low" in words. It lives here rather than
 * in `core/designsystem` for a boundary reason: the design system draws things
 * and knows nothing about a forecast, and giving it `ConfidenceLabel` would put
 * `domain/prediction` on its compile path for one enum.
 *
 * The dots are never the whole message. §43: `clearAndSetSemantics` replaces the
 * glyphs with the word, so a screen reader gets "Prediction confidence: High"
 * instead of six bullet characters read aloud one at a time.
 */
@Composable
fun ConfidenceRow(label: ConfidenceLabel, prefix: String = "Prediction confidence") {
    val filled = when (label) {
        ConfidenceLabel.LOW -> 1
        ConfidenceLabel.MEDIUM -> 2
        ConfidenceLabel.HIGH -> 3
    }
    Row(
        verticalAlignment = Alignment.CenterVertically,
        horizontalArrangement = Arrangement.spacedBy(6.dp),
        modifier = Modifier.clearAndSetSemantics {
            contentDescription = "$prefix: ${label.readable()}"
        },
    ) {
        Text("●".repeat(filled) + "○".repeat(3 - filled), color = MaterialTheme.colorScheme.primary)
        Text(label.readable(), style = MaterialTheme.typography.bodyMedium)
    }
}

fun ConfidenceLabel.readable(): String = when (this) {
    ConfidenceLabel.LOW -> "Low"
    ConfidenceLabel.MEDIUM -> "Medium"
    ConfidenceLabel.HIGH -> "High"
}
