@file:Suppress("HardCodedStringLiteral")

package eu.gaudian.translator.view.vocabulary.widgets

import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.RowScope
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import eu.gaudian.translator.ui.theme.ThemePreviews
import eu.gaudian.translator.view.composable.AppIcons

@Composable
fun WidgetButtonOutline(
    onClick: () -> Unit,
    modifier: Modifier = Modifier,
    showIcon: Boolean = false,
    content: @Composable RowScope.() -> Unit // RowScope is fine for this version
) {
    Surface(
        onClick = onClick,
        modifier = modifier.fillMaxWidth(), // The clickable area is still full-width
        shape = MaterialTheme.shapes.medium,
        border = BorderStroke(1.dp, MaterialTheme.colorScheme.outline.copy(alpha = 0.5f)),
        color = Color.Transparent,
        tonalElevation = 2.dp
    ) {
        // This Row now only takes up as much space as its children need.
        // It is placed at the start of the Surface by default.
        Row(
            modifier = Modifier.padding(horizontal = 16.dp, vertical = 12.dp),
            verticalAlignment = Alignment.CenterVertically,
            horizontalArrangement = Arrangement.Start // Explicitly align to the start
        ) {
            // Your content is placed first
            content()

            if (showIcon) {
                // A small spacer for breathing room
                Spacer(modifier = Modifier.width(8.dp))

                // The Icon is placed immediately after the spacer
                Icon(
                    AppIcons.ArrowForward,
                    contentDescription = "Arrow Icon",
                    tint = MaterialTheme.colorScheme.onSurface
                )
            }
        }
    }
}

@ThemePreviews
@Composable
fun WidgetButtonOutlinePreview() {
    WidgetButtonOutline(onClick = {}) {
        Text("Button Text")
    }
}

@ThemePreviews
@Composable
fun WidgetButtonOutlinePreviewIcon() {
    WidgetButtonOutline(onClick = {}, showIcon = true) {
        Text("Button Text")
    }
}