package com.garfiec.librechat.feature.agents.components

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Switch
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import org.jetbrains.compose.resources.stringResource
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.unit.dp
import librechat_mobile.feature.agents.generated.resources.Res
import librechat_mobile.feature.agents.generated.resources.*

data class AgentCapabilities(
    val artifacts: Boolean = false,
    val endAfterTools: Boolean = false,
    val hideSequentialOutputs: Boolean = false,
    val recursionLimit: Int = 25,
)

@Composable
fun AgentCapabilitiesSection(
    capabilities: AgentCapabilities,
    onCapabilitiesChanged: (AgentCapabilities) -> Unit,
    modifier: Modifier = Modifier,
) {
    Column(modifier = modifier.fillMaxWidth()) {
        Text(
            text = stringResource(Res.string.label_capabilities),
            style = MaterialTheme.typography.titleSmall,
        )
        Spacer(modifier = Modifier.height(8.dp))

        CapabilityToggle(
            label = stringResource(Res.string.label_artifacts),
            description = stringResource(Res.string.artifacts_description),
            checked = capabilities.artifacts,
            onCheckedChange = {
                onCapabilitiesChanged(capabilities.copy(artifacts = it))
            },
        )

        CapabilityToggle(
            label = stringResource(Res.string.label_end_after_tools),
            description = stringResource(Res.string.end_after_tools_description),
            checked = capabilities.endAfterTools,
            onCheckedChange = {
                onCapabilitiesChanged(capabilities.copy(endAfterTools = it))
            },
        )

        CapabilityToggle(
            label = stringResource(Res.string.label_hide_sequential_outputs),
            description = stringResource(Res.string.hide_sequential_description),
            checked = capabilities.hideSequentialOutputs,
            onCheckedChange = {
                onCapabilitiesChanged(capabilities.copy(hideSequentialOutputs = it))
            },
        )

        Spacer(modifier = Modifier.height(8.dp))

        Column(modifier = Modifier.fillMaxWidth()) {
            Text(
                text = stringResource(Res.string.label_recursion_limit),
                style = MaterialTheme.typography.bodyLarge,
            )
            Text(
                text = stringResource(Res.string.recursion_limit_description),
                style = MaterialTheme.typography.bodySmall,
                color = MaterialTheme.colorScheme.onSurfaceVariant,
            )
            Spacer(modifier = Modifier.height(4.dp))
            OutlinedTextField(
                value = capabilities.recursionLimit.toString(),
                onValueChange = { newValue ->
                    val filtered = newValue.filter { it.isDigit() }
                    val intVal = filtered.toIntOrNull() ?: 0
                    onCapabilitiesChanged(
                        capabilities.copy(recursionLimit = intVal.coerceIn(1, 100)),
                    )
                },
                keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
                singleLine = true,
                modifier = Modifier.fillMaxWidth(),
            )
        }
    }
}

@Composable
private fun CapabilityToggle(
    label: String,
    description: String,
    checked: Boolean,
    onCheckedChange: (Boolean) -> Unit,
    modifier: Modifier = Modifier,
) {
    Row(
        modifier = modifier
            .fillMaxWidth()
            .padding(vertical = 4.dp),
        horizontalArrangement = Arrangement.SpaceBetween,
        verticalAlignment = Alignment.CenterVertically,
    ) {
        Column(modifier = Modifier.weight(1f)) {
            Text(
                text = label,
                style = MaterialTheme.typography.bodyLarge,
            )
            Text(
                text = description,
                style = MaterialTheme.typography.bodySmall,
                color = MaterialTheme.colorScheme.onSurfaceVariant,
            )
        }
        Switch(
            checked = checked,
            onCheckedChange = onCheckedChange,
        )
    }
}
