package com.atridad.ascently.ui.screens

import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.selection.selectable
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ArrowBack
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.semantics.Role
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.unit.dp
import com.atridad.ascently.data.model.*
import com.atridad.ascently.ui.components.ImagePicker
import com.atridad.ascently.ui.viewmodel.ClimbViewModel
import kotlinx.coroutines.flow.first
import java.time.LocalDateTime

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun AddEditGymScreen(gymId: String?, viewModel: ClimbViewModel, onNavigateBack: () -> Unit) {
    var name by remember { mutableStateOf("") }
    var location by remember { mutableStateOf("") }
    var notes by remember { mutableStateOf("") }
    var selectedClimbTypes by remember { mutableStateOf(setOf<ClimbType>()) }
    var selectedDifficultySystems by remember { mutableStateOf(setOf<DifficultySystem>()) }

    val isEditing = gymId != null

    // Calculate available difficulty systems based on selected climb types
    val availableDifficultySystems =
        if (selectedClimbTypes.isEmpty()) {
            emptyList()
        } else {
            selectedClimbTypes
                .flatMap { climbType -> DifficultySystem.systemsForClimbType(climbType) }
                .distinct()
        }

    // Reset selected difficulty systems when available systems change
    LaunchedEffect(availableDifficultySystems) {
        selectedDifficultySystems =
            selectedDifficultySystems.filter { it in availableDifficultySystems }.toSet()
    }

    // Load existing gym data for editing
    LaunchedEffect(gymId) {
        if (gymId != null) {
            val existingGym = viewModel.getGymById(gymId).first()
            existingGym?.let { gym ->
                name = gym.name
                location = gym.location ?: ""
                notes = gym.notes ?: ""
                selectedClimbTypes = gym.supportedClimbTypes.toSet()
                selectedDifficultySystems = gym.difficultySystems.toSet()
            }
        }
    }

    Scaffold(
        topBar = {
            TopAppBar(
                title = { Text(if (isEditing) "Edit Gym" else "Add Gym") },
                navigationIcon = {
                    IconButton(onClick = onNavigateBack) {
                        Icon(
                            Icons.AutoMirrored.Filled.ArrowBack,
                            contentDescription = "Back",
                        )
                    }
                },
                actions = {
                    TextButton(
                        onClick = {
                            val gym =
                                Gym.create(
                                    name,
                                    location,
                                    selectedClimbTypes.toList(),
                                    selectedDifficultySystems.toList(),
                                    notes = notes,
                                )

                            if (isEditing) {
                                viewModel.updateGym(gym.copy(id = gymId))
                            } else {
                                viewModel.addGym(gym)
                            }
                            onNavigateBack()
                        },
                        enabled =
                        name.isNotBlank() &&
                            selectedClimbTypes.isNotEmpty() &&
                            selectedDifficultySystems.isNotEmpty(),
                    ) { Text("Save") }
                },
            )
        },
    ) { paddingValues ->
        Column(
            modifier = Modifier.fillMaxSize().padding(paddingValues).padding(16.dp),
            verticalArrangement = Arrangement.spacedBy(16.dp),
        ) {
            // Name field
            OutlinedTextField(
                value = name,
                onValueChange = { name = it },
                label = { Text("Gym Name") },
                modifier = Modifier.fillMaxWidth(),
                singleLine = true,
            )

            // Location field
            OutlinedTextField(
                value = location,
                onValueChange = { location = it },
                label = { Text("Location (Optional)") },
                modifier = Modifier.fillMaxWidth(),
                singleLine = true,
            )

            // Climb Types
            Card(modifier = Modifier.fillMaxWidth()) {
                Column(modifier = Modifier.padding(16.dp)) {
                    Text(
                        text = "Supported Climb Types",
                        style = MaterialTheme.typography.titleMedium,
                        fontWeight = FontWeight.Bold,
                    )

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

                    ClimbType.entries.forEach { climbType ->
                        Row(
                            verticalAlignment = Alignment.CenterVertically,
                            modifier =
                            Modifier.fillMaxWidth()
                                .selectable(
                                    selected = climbType in selectedClimbTypes,
                                    onClick = {
                                        selectedClimbTypes =
                                            if (climbType in
                                                selectedClimbTypes
                                            ) {
                                                selectedClimbTypes -
                                                    climbType
                                            } else {
                                                selectedClimbTypes +
                                                    climbType
                                            }
                                    },
                                    role = Role.Checkbox,
                                ),
                        ) {
                            Checkbox(
                                checked = climbType in selectedClimbTypes,
                                onCheckedChange = null,
                            )
                            Spacer(modifier = Modifier.width(8.dp))
                            Text(climbType.displayName)
                        }
                    }
                }
            }

            // Difficulty Systems
            Card(modifier = Modifier.fillMaxWidth()) {
                Column(modifier = Modifier.padding(16.dp)) {
                    Text(
                        text = "Difficulty Systems",
                        style = MaterialTheme.typography.titleMedium,
                        fontWeight = FontWeight.Bold,
                    )

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

                    if (selectedClimbTypes.isEmpty()) {
                        Text(
                            text =
                            "Select climb types first to see available difficulty systems",
                            style = MaterialTheme.typography.bodyMedium,
                            color = MaterialTheme.colorScheme.onSurfaceVariant,
                            modifier = Modifier.padding(vertical = 8.dp),
                        )
                    } else {
                        availableDifficultySystems.forEach { system ->
                            Row(
                                verticalAlignment = Alignment.CenterVertically,
                                modifier =
                                Modifier.fillMaxWidth()
                                    .selectable(
                                        selected =
                                        system in
                                            selectedDifficultySystems,
                                        onClick = {
                                            selectedDifficultySystems =
                                                if (system in
                                                    selectedDifficultySystems
                                                ) {
                                                    selectedDifficultySystems -
                                                        system
                                                } else {
                                                    selectedDifficultySystems +
                                                        system
                                                }
                                        },
                                        role = Role.Checkbox,
                                    ),
                            ) {
                                Checkbox(
                                    checked = system in selectedDifficultySystems,
                                    onCheckedChange = null,
                                )
                                Spacer(modifier = Modifier.width(8.dp))
                                Text(system.displayName)
                            }
                        }
                    }
                }
            }

            // Notes field
            OutlinedTextField(
                value = notes,
                onValueChange = { notes = it },
                label = { Text("Notes (Optional)") },
                modifier = Modifier.fillMaxWidth(),
                minLines = 3,
            )
        }
    }
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun AddEditProblemScreen(
    problemId: String?,
    gymId: String?,
    viewModel: ClimbViewModel,
    onNavigateBack: () -> Unit,
) {
    val isEditing = problemId != null
    val gyms by viewModel.gyms.collectAsState()

    // Problem form state
    var selectedGym by remember {
        mutableStateOf<Gym?>(gymId?.let { id -> gyms.find { it.id == id } })
    }
    var problemName by remember { mutableStateOf("") }
    var description by remember { mutableStateOf("") }
    var selectedClimbType by remember { mutableStateOf(ClimbType.BOULDER) }
    var selectedDifficultySystem by remember { mutableStateOf(DifficultySystem.V_SCALE) }
    var difficultyGrade by remember { mutableStateOf("") }

    var location by remember { mutableStateOf("") }
    var tags by remember { mutableStateOf("") }
    var notes by remember { mutableStateOf("") }
    var isActive by remember { mutableStateOf(true) }
    var imagePaths by remember { mutableStateOf<List<String>>(emptyList()) }

    // Load existing problem data for editing
    LaunchedEffect(problemId) {
        if (problemId != null) {
            val existingProblem = viewModel.getProblemById(problemId).first()
            existingProblem?.let { p ->
                problemName = p.name ?: ""
                description = p.description ?: ""
                selectedClimbType = p.climbType
                selectedDifficultySystem = p.difficulty.system
                difficultyGrade = p.difficulty.grade

                location = p.location ?: ""
                tags = p.tags.joinToString(", ")
                notes = p.notes ?: ""
                isActive = p.isActive
                imagePaths = p.imagePaths
                selectedGym = gyms.find { it.id == p.gymId }
            }
        }
    }

    LaunchedEffect(gymId, gyms) {
        if (gymId != null && selectedGym == null) {
            selectedGym = gyms.find { it.id == gymId }
        }
    }

    val availableClimbTypes = selectedGym?.supportedClimbTypes ?: ClimbType.entries.toList()
    val availableDifficultySystems =
        DifficultySystem.systemsForClimbType(selectedClimbType).filter { system ->
            selectedGym?.difficultySystems?.contains(system) != false
        }

    // Auto-select climb type if there's only one available
    LaunchedEffect(availableClimbTypes) {
        if (availableClimbTypes.size == 1 && selectedClimbType != availableClimbTypes.first()) {
            selectedClimbType = availableClimbTypes.first()
        }
    }

    // Auto-select or reset difficulty system based on climb type
    LaunchedEffect(selectedClimbType, availableDifficultySystems) {
        when {
            // If current system is not compatible, select the first available one
            selectedDifficultySystem !in availableDifficultySystems -> {
                selectedDifficultySystem =
                    availableDifficultySystems.firstOrNull() ?: DifficultySystem.CUSTOM
            }
            // If there's only one available system and nothing is selected, auto-select it
            availableDifficultySystems.size == 1 &&
                selectedDifficultySystem != availableDifficultySystems.first() -> {
                selectedDifficultySystem = availableDifficultySystems.first()
            }
        }
    }

    // Reset grade when difficulty system changes (unless it's a valid grade for the new system)
    LaunchedEffect(selectedDifficultySystem) {
        val availableGrades = selectedDifficultySystem.availableGrades
        if (availableGrades.isNotEmpty() && difficultyGrade !in availableGrades) {
            difficultyGrade = ""
        }
    }

    Scaffold(
        topBar = {
            TopAppBar(
                title = { Text(if (isEditing) "Edit Problem" else "Add Problem") },
                navigationIcon = {
                    IconButton(onClick = onNavigateBack) {
                        Icon(
                            Icons.AutoMirrored.Filled.ArrowBack,
                            contentDescription = "Back",
                        )
                    }
                },
                actions = {
                    TextButton(
                        onClick = {
                            selectedGym?.let { gym ->
                                val difficulty =
                                    DifficultyGrade(
                                        system = selectedDifficultySystem,
                                        grade = difficultyGrade,
                                        numericValue =
                                        when (
                                            selectedDifficultySystem
                                        ) {
                                            DifficultySystem.V_SCALE ->
                                                difficultyGrade
                                                    .removePrefix(
                                                        "V",
                                                    )
                                                    .toIntOrNull()
                                                    ?: 0
                                            else ->
                                                difficultyGrade
                                                    .hashCode() %
                                                    100 // Simple mapping for other systems
                                        },
                                    )

                                val problem =
                                    Problem.create(
                                        gymId = gym.id,
                                        name = problemName.ifBlank { null },
                                        description =
                                        description.ifBlank { null },
                                        climbType = selectedClimbType,
                                        difficulty = difficulty,
                                        tags =
                                        tags.split(",")
                                            .map { it.trim() }
                                            .filter {
                                                it.isNotBlank()
                                            },
                                        location = location.ifBlank { null },
                                        imagePaths = imagePaths,
                                        notes = notes.ifBlank { null },
                                    )

                                if (isEditing) {
                                    problemId.let { id ->
                                        viewModel.updateProblem(problem.copy(id = id))
                                    }
                                } else {
                                    viewModel.addProblem(problem)
                                }
                                onNavigateBack()
                            }
                        },
                        enabled = selectedGym != null && difficultyGrade.isNotBlank(),
                    ) { Text("Save") }
                },
            )
        },
    ) { paddingValues ->
        LazyColumn(
            modifier = Modifier.fillMaxSize().padding(paddingValues).padding(16.dp),
            verticalArrangement = Arrangement.spacedBy(16.dp),
        ) {
            // Gym Selection
            item {
                Card(modifier = Modifier.fillMaxWidth()) {
                    Column(modifier = Modifier.padding(16.dp)) {
                        Text(
                            text = "Select Gym",
                            style = MaterialTheme.typography.titleMedium,
                            fontWeight = FontWeight.Bold,
                        )

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

                        if (gyms.isEmpty()) {
                            Text(
                                text = "No gyms available. Add a gym first.",
                                style = MaterialTheme.typography.bodyMedium,
                                color = MaterialTheme.colorScheme.error,
                            )
                        } else {
                            LazyRow(horizontalArrangement = Arrangement.spacedBy(8.dp)) {
                                items(gyms) { gym ->
                                    FilterChip(
                                        onClick = { selectedGym = gym },
                                        label = { Text(gym.name) },
                                        selected = selectedGym?.id == gym.id,
                                    )
                                }
                            }
                        }
                    }
                }
            }

            // Basic Problem Info
            item {
                Card(modifier = Modifier.fillMaxWidth()) {
                    Column(modifier = Modifier.padding(16.dp)) {
                        Text(
                            text = "Problem Details",
                            style = MaterialTheme.typography.titleMedium,
                            fontWeight = FontWeight.Bold,
                        )

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

                        OutlinedTextField(
                            value = problemName,
                            onValueChange = { problemName = it },
                            label = { Text("Problem Name (Optional)") },
                            modifier = Modifier.fillMaxWidth(),
                            singleLine = true,
                            placeholder = { Text("e.g., 'The Overhang Monster', 'Yellow V4'") },
                        )

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

                        OutlinedTextField(
                            value = description,
                            onValueChange = { description = it },
                            label = { Text("Description (Optional)") },
                            modifier = Modifier.fillMaxWidth(),
                            minLines = 2,
                            placeholder = { Text("Describe the problem, holds, style, etc.") },
                        )

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

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

                        OutlinedTextField(
                            value = location,
                            onValueChange = { location = it },
                            label = { Text("Location (Optional)") },
                            modifier = Modifier.fillMaxWidth(),
                            singleLine = true,
                            placeholder = { Text("e.g., 'Cave area', 'Wall 3', 'Right side'") },
                        )
                    }
                }
            }

            // Climb Type
            if (selectedGym != null) {
                item {
                    Card(modifier = Modifier.fillMaxWidth()) {
                        Column(modifier = Modifier.padding(16.dp)) {
                            Text(
                                text = "Climb Type",
                                style = MaterialTheme.typography.titleMedium,
                                fontWeight = FontWeight.Bold,
                            )

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

                            Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) {
                                availableClimbTypes.forEach { climbType ->
                                    FilterChip(
                                        onClick = { selectedClimbType = climbType },
                                        label = { Text(climbType.displayName) },
                                        selected = selectedClimbType == climbType,
                                    )
                                }
                            }
                        }
                    }
                }
            }

            // Difficulty
            if (selectedGym != null) {
                item {
                    Card(modifier = Modifier.fillMaxWidth()) {
                        Column(modifier = Modifier.padding(16.dp)) {
                            Text(
                                text = "Difficulty",
                                style = MaterialTheme.typography.titleMedium,
                                fontWeight = FontWeight.Bold,
                            )

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

                            Text(
                                text = "Difficulty System",
                                style = MaterialTheme.typography.bodyMedium,
                                fontWeight = FontWeight.Medium,
                            )

                            LazyRow(horizontalArrangement = Arrangement.spacedBy(8.dp)) {
                                items(availableDifficultySystems) { system ->
                                    FilterChip(
                                        onClick = { selectedDifficultySystem = system },
                                        label = { Text(system.displayName) },
                                        selected = selectedDifficultySystem == system,
                                    )
                                }
                            }

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

                            if (selectedDifficultySystem == DifficultySystem.CUSTOM) {
                                OutlinedTextField(
                                    value = difficultyGrade,
                                    onValueChange = { newValue ->
                                        // Only allow integers for custom scales
                                        if (newValue.isEmpty() || newValue.all { it.isDigit() }
                                        ) {
                                            difficultyGrade = newValue
                                        }
                                    },
                                    label = { Text("Grade *") },
                                    modifier = Modifier.fillMaxWidth(),
                                    singleLine = true,
                                    placeholder = {
                                        Text("Enter numeric grade (e.g. 5, 10, 15)")
                                    },
                                    supportingText = {
                                        Text("Custom grades must be whole numbers")
                                    },
                                    keyboardOptions =
                                    KeyboardOptions(keyboardType = KeyboardType.Number),
                                )
                            } else {
                                var expanded by remember { mutableStateOf(false) }
                                val availableGrades = selectedDifficultySystem.availableGrades

                                ExposedDropdownMenuBox(
                                    expanded = expanded,
                                    onExpandedChange = { expanded = !expanded },
                                    modifier = Modifier.fillMaxWidth(),
                                ) {
                                    OutlinedTextField(
                                        value = difficultyGrade,
                                        onValueChange = {},
                                        readOnly = true,
                                        label = { Text("Grade *") },
                                        trailingIcon = {
                                            ExposedDropdownMenuDefaults.TrailingIcon(
                                                expanded = expanded,
                                            )
                                        },
                                        colors =
                                        ExposedDropdownMenuDefaults
                                            .outlinedTextFieldColors(),
                                        modifier =
                                        Modifier.menuAnchor(
                                            ExposedDropdownMenuAnchorType
                                                .PrimaryNotEditable,
                                            enabled = true,
                                        )
                                            .fillMaxWidth(),
                                    )
                                    ExposedDropdownMenu(
                                        expanded = expanded,
                                        onDismissRequest = { expanded = false },
                                    ) {
                                        availableGrades.forEach { grade ->
                                            DropdownMenuItem(
                                                text = { Text(grade) },
                                                onClick = {
                                                    difficultyGrade = grade
                                                    expanded = false
                                                },
                                            )
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            // Images Section
            item {
                Card(modifier = Modifier.fillMaxWidth()) {
                    Column(modifier = Modifier.padding(16.dp)) {
                        Text(
                            text = "Photos",
                            style = MaterialTheme.typography.titleMedium,
                            fontWeight = FontWeight.Bold,
                        )

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

                        ImagePicker(
                            imageUris = imagePaths,
                            onImagesChanged = { imagePaths = it },
                            maxImages = 5,
                        )
                    }
                }
            }

            item {
                Card(modifier = Modifier.fillMaxWidth()) {
                    Column(modifier = Modifier.padding(16.dp)) {
                        Text(
                            text = "Additional Info",
                            style = MaterialTheme.typography.titleMedium,
                            fontWeight = FontWeight.Bold,
                        )

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

                        OutlinedTextField(
                            value = tags,
                            onValueChange = { tags = it },
                            label = { Text("Tags (Optional)") },
                            modifier = Modifier.fillMaxWidth(),
                            singleLine = true,
                            placeholder = { Text("e.g., crimpy, dynamic (comma-separated)") },
                        )

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

                        OutlinedTextField(
                            value = notes,
                            onValueChange = { notes = it },
                            label = { Text("Notes (Optional)") },
                            modifier = Modifier.fillMaxWidth(),
                            minLines = 3,
                            placeholder = { Text("Any additional notes about this problem") },
                        )

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

                        Row(
                            verticalAlignment = Alignment.CenterVertically,
                            modifier =
                            Modifier.fillMaxWidth()
                                .selectable(
                                    selected = isActive,
                                    onClick = { isActive = !isActive },
                                    role = Role.Checkbox,
                                ),
                        ) {
                            Checkbox(checked = isActive, onCheckedChange = null)
                            Spacer(modifier = Modifier.width(8.dp))
                            Text(
                                text = "Problem is currently active",
                                style = MaterialTheme.typography.bodyMedium,
                            )
                        }
                    }
                }
            }
        }
    }
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun AddEditSessionScreen(
    sessionId: String?,
    gymId: String?,
    viewModel: ClimbViewModel,
    onNavigateBack: () -> Unit,
) {
    val isEditing = sessionId != null
    val gyms by viewModel.gyms.collectAsState()
    val context = LocalContext.current

    // Session form state
    var selectedGym by remember {
        mutableStateOf<Gym?>(gymId?.let { id -> gyms.find { it.id == id } })
    }
    var sessionDate by remember { mutableStateOf(LocalDateTime.now().toLocalDate().toString()) }
    var duration by remember { mutableStateOf("") }
    var sessionNotes by remember { mutableStateOf("") }

    // Load existing session data for editing
    LaunchedEffect(sessionId) {
        if (sessionId != null) {
            val existingSession = viewModel.getSessionById(sessionId).first()
            existingSession?.let { session ->
                selectedGym = gyms.find { it.id == session.gymId }
                sessionDate = session.date.split("T")[0] // Extract date part
                duration = session.duration?.toString() ?: ""
                sessionNotes = session.notes ?: ""
            }
        }
    }

    LaunchedEffect(gymId, gyms) {
        if (gymId != null && selectedGym == null) {
            selectedGym = gyms.find { it.id == gymId }
        } else if (gymId == null && selectedGym == null && gyms.size == 1) {
            // Auto-select the gym if there's only one available
            selectedGym = gyms.first()
        }
    }

    Scaffold(
        topBar = {
            TopAppBar(
                title = { Text(if (isEditing) "Edit Session" else "Add Session") },
                navigationIcon = {
                    IconButton(onClick = onNavigateBack) {
                        Icon(
                            Icons.AutoMirrored.Filled.ArrowBack,
                            contentDescription = "Back",
                        )
                    }
                },
                actions = {
                    TextButton(
                        onClick = {
                            selectedGym?.let { gym ->
                                if (isEditing) {
                                    val session =
                                        ClimbSession.create(
                                            gymId = gym.id,
                                            notes =
                                            sessionNotes.ifBlank {
                                                null
                                            },
                                        )
                                    sessionId.let { id ->
                                        viewModel.updateSession(session.copy(id = id))
                                    }
                                } else {
                                    viewModel.startSession(
                                        context,
                                        gym.id,
                                        sessionNotes.ifBlank { null },
                                    )
                                }
                                onNavigateBack()
                            }
                        },
                        enabled = selectedGym != null,
                    ) { Text("Save") }
                },
            )
        },
    ) { paddingValues ->
        LazyColumn(
            modifier = Modifier.fillMaxSize().padding(paddingValues).padding(16.dp),
            verticalArrangement = Arrangement.spacedBy(16.dp),
        ) {
            // Gym Selection
            item {
                Card(modifier = Modifier.fillMaxWidth()) {
                    Column(modifier = Modifier.padding(16.dp)) {
                        Text(
                            text = "Select Gym",
                            style = MaterialTheme.typography.titleMedium,
                            fontWeight = FontWeight.Bold,
                        )

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

                        if (gyms.isEmpty()) {
                            Text(
                                text = "No gyms available. Add a gym first.",
                                style = MaterialTheme.typography.bodyMedium,
                                color = MaterialTheme.colorScheme.error,
                            )
                        } else {
                            LazyRow(horizontalArrangement = Arrangement.spacedBy(8.dp)) {
                                items(gyms) { gym ->
                                    FilterChip(
                                        onClick = { selectedGym = gym },
                                        label = { Text(gym.name) },
                                        selected = selectedGym?.id == gym.id,
                                    )
                                }
                            }
                        }
                    }
                }
            }

            // Session Details
            item {
                Card(modifier = Modifier.fillMaxWidth()) {
                    Column(modifier = Modifier.padding(16.dp)) {
                        Text(
                            text = "Session Details",
                            style = MaterialTheme.typography.titleMedium,
                            fontWeight = FontWeight.Bold,
                        )

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

                        OutlinedTextField(
                            value = sessionDate,
                            onValueChange = { sessionDate = it },
                            label = { Text("Date (YYYY-MM-DD)") },
                            modifier = Modifier.fillMaxWidth(),
                            singleLine = true,
                        )

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

                        OutlinedTextField(
                            value = duration,
                            onValueChange = { duration = it },
                            label = { Text("Duration (minutes)") },
                            modifier = Modifier.fillMaxWidth(),
                            singleLine = true,
                            keyboardOptions =
                            KeyboardOptions(keyboardType = KeyboardType.Number),
                        )

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

                        OutlinedTextField(
                            value = sessionNotes,
                            onValueChange = { sessionNotes = it },
                            label = { Text("Session Notes (Optional)") },
                            modifier = Modifier.fillMaxWidth(),
                            minLines = 3,
                        )
                    }
                }
            }
        }
    }
}
