package cl.homelogic.platform.designsystem.components.molecules

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import cl.homelogic.platform.designsystem.components.atoms.BottomSafeZone
import cl.homelogic.platform.designsystem.foundations.Padding
import cl.homelogic.platform.designsystem.foundations.Sizes

data class SectionedListItem(
    val title: String,
    val composables: List<@Composable () -> Unit>,
)

@Composable
fun LazySectionedList(
    modifier: Modifier = Modifier,
    items: List<SectionedListItem>,
    addSeparator: Boolean,
) {
    LazyColumn(
        modifier = modifier,
    ) {
        items(items) { sizeGroup ->
            Section(sizeGroup.title) {
                Column(modifier = Modifier.fillMaxWidth()) {
                    sizeGroup.composables.forEachIndexed { index, item ->
                        Row(
                            modifier = Modifier
                                .fillMaxWidth()
                                .wrapContentHeight()
                                .padding(horizontal = Padding.small),
                            horizontalArrangement = Arrangement.SpaceBetween,
                            verticalAlignment = Alignment.CenterVertically,
                        ) {
                            item()
                        }
                        if (index < sizeGroup.composables.size - 1 && addSeparator) {
                            HorizontalDivider(
                                thickness = Sizes.Stroke.Regular,
                                color = MaterialTheme.colorScheme.outline,
                            )
                        }
                    }
                }
            }
        }
        item {
            BottomSafeZone()
        }
    }
}
