package com.seven.ikar.kotlin.feature.stories

import android.net.Uri
import androidx.activity.compose.BackHandler
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.border
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.WindowInsetsSides
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.only
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.safeDrawing
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.windowInsetsPadding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.Add
import androidx.compose.material.icons.outlined.Close
import androidx.compose.material.icons.outlined.Delete
import androidx.compose.material.icons.outlined.Schedule
import androidx.compose.material3.Button
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.LinearProgressIndicator
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedButton
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import androidx.compose.ui.window.DialogProperties
import coil.compose.AsyncImage
import com.seven.ikar.kotlin.core.model.AttachmentDto
import com.seven.ikar.kotlin.core.model.StoryDto
import com.seven.ikar.kotlin.core.model.StoryVisibility
import com.seven.ikar.kotlin.core.network.ServerConfig
import com.seven.ikar.kotlin.ui.AuthenticatedAsyncImage
import com.seven.ikar.kotlin.ui.AvatarView
import com.seven.ikar.kotlin.ui.IkarHeaderIconBubble
import com.seven.ikar.kotlin.ui.IkarRowDivider
import com.seven.ikar.kotlin.ui.IkarScreenSurface
import com.seven.ikar.kotlin.ui.IkarSectionCard
import com.seven.ikar.kotlin.ui.IkarSectionTitle
import com.seven.ikar.kotlin.ui.IkarSimpleHeader
import com.seven.ikar.kotlin.ui.IkarToggleRow
import com.seven.ikar.kotlin.ui.SecureWindowEffect
import com.seven.ikar.kotlin.ui.theme.TelegramBlue
import com.seven.ikar.kotlin.ui.theme.TelegramInkMuted
import java.io.File

@Composable
fun StoriesScreen(
    state: StoriesUiState,
    onBackClick: () -> Unit,
    onComposeClick: () -> Unit,
    onRefreshClick: () -> Unit,
    onStoryClick: (StoryDto) -> Unit,
    onDeleteStory: (StoryDto) -> Unit,
    onCloseStoryPreview: () -> Unit,
    onNextStory: () -> Unit,
    onPreviousStory: () -> Unit,
    onRetryTransfer: (String) -> Unit,
    onCancelTransfer: (String) -> Unit
) {
    SecureWindowEffect(enabled = state.selectedStory?.hasProtectedContent == true)

    state.selectedStory?.let { story ->
        StoryViewerDialog(
            story = story,
            stories = state.stories,
            accessToken = state.accessToken,
            isOwnStory = story.author.id == state.currentUserId,
            onClose = onCloseStoryPreview,
            onNextStory = onNextStory,
            onPreviousStory = onPreviousStory,
            onDeleteStory = { onDeleteStory(story) }
        )
    }

    IkarScreenSurface(modifier = Modifier.fillMaxSize()) {
        Column(
            modifier = Modifier
                .fillMaxSize()
                .windowInsetsPadding(WindowInsets.safeDrawing.only(WindowInsetsSides.Vertical))
                .verticalScroll(rememberScrollState())
                .padding(horizontal = 18.dp, vertical = 16.dp),
            verticalArrangement = Arrangement.spacedBy(16.dp)
        ) {
            IkarSimpleHeader(
                title = "Истории",
                onBackClick = onBackClick,
                rightContent = {
                    IkarHeaderIconBubble(onClick = onComposeClick) {
                        Icon(Icons.Outlined.Add, contentDescription = null, tint = TelegramBlue)
                    }
                }
            )

            if (state.activeTransfers.isNotEmpty()) {
                StoryTransfersCard(
                    transfers = state.activeTransfers,
                    onRetryTransfer = onRetryTransfer,
                    onCancelTransfer = onCancelTransfer
                )
            }

            state.errorMessage?.let { message ->
                IkarSectionCard {
                    Text("Ошибка", style = MaterialTheme.typography.titleMedium, color = MaterialTheme.colorScheme.error)
                    Text(message, modifier = Modifier.padding(top = 6.dp), style = MaterialTheme.typography.bodySmall, color = TelegramInkMuted)
                }
            }

            IkarSectionCard {
                Row(
                    modifier = Modifier.fillMaxWidth(),
                    horizontalArrangement = Arrangement.SpaceBetween,
                    verticalAlignment = Alignment.CenterVertically
                ) {
                    IkarSectionTitle("Сегодня")
                    TextButton(onClick = onRefreshClick) {
                        Text(if (state.isLoading) "Загрузка..." else "Обновить")
                    }
                }

                if (state.stories.isEmpty()) {
                    EmptyStoriesState(onComposeClick = onComposeClick)
                } else {
                    state.stories.forEachIndexed { index, story ->
                        StoryPreviewRow(story = story, accessToken = state.accessToken, onClick = { onStoryClick(story) })
                        if (index != state.stories.lastIndex) {
                            IkarRowDivider()
                        }
                    }
                }
            }

            IkarSectionCard {
                Row(
                    modifier = Modifier.fillMaxWidth(),
                    horizontalArrangement = Arrangement.spacedBy(12.dp),
                    verticalAlignment = Alignment.CenterVertically
                ) {
                    Icon(Icons.Outlined.Schedule, contentDescription = null, tint = TelegramBlue)
                    Column(verticalArrangement = Arrangement.spacedBy(4.dp)) {
                        Text("Медиаистории подключены", style = MaterialTheme.typography.titleLarge)
                        Text(
                            "Текстовые истории публикуются сразу, а медиаистории уходят через очередь фоновой отправки с прогрессом, повтором и отменой.",
                            style = MaterialTheme.typography.bodySmall,
                            color = TelegramInkMuted
                        )
                    }
                }
            }
        }
    }
}

@Composable
fun StoryComposerScreen(
    state: StoriesUiState,
    onBackClick: () -> Unit,
    onTextChanged: (String) -> Unit,
    onVisibilityChanged: (StoryVisibility) -> Unit,
    onProtectedChanged: (Boolean) -> Unit,
    onTtlChanged: (Int) -> Unit,
    onMediaSelected: (List<Uri>) -> Unit,
    onRemoveMedia: (String) -> Unit,
    onClearMedia: () -> Unit,
    onCreateClick: () -> Unit,
    onRetryTransfer: (String) -> Unit,
    onCancelTransfer: (String) -> Unit
) {
    val mediaPicker = rememberLauncherForActivityResult(ActivityResultContracts.OpenMultipleDocuments()) { uris ->
        onMediaSelected(uris)
    }

    IkarScreenSurface(modifier = Modifier.fillMaxSize()) {
        Column(
            modifier = Modifier
                .fillMaxSize()
                .windowInsetsPadding(WindowInsets.safeDrawing.only(WindowInsetsSides.Vertical))
                .verticalScroll(rememberScrollState())
                .padding(horizontal = 18.dp, vertical = 16.dp),
            verticalArrangement = Arrangement.spacedBy(16.dp)
        ) {
            IkarSimpleHeader(title = "Новая история", onBackClick = onBackClick)

            if (state.activeTransfers.isNotEmpty()) {
                StoryTransfersCard(
                    transfers = state.activeTransfers,
                    onRetryTransfer = onRetryTransfer,
                    onCancelTransfer = onCancelTransfer
                )
            }

            IkarSectionCard {
                Row(
                    modifier = Modifier.fillMaxWidth(),
                    horizontalArrangement = Arrangement.SpaceBetween,
                    verticalAlignment = Alignment.CenterVertically
                ) {
                    IkarSectionTitle("Медиа")
                    Text("${state.pendingMedia.size}/10", style = MaterialTheme.typography.bodySmall, color = TelegramInkMuted)
                }
                OutlinedButton(
                    onClick = { mediaPicker.launch(arrayOf("image/*", "video/*")) },
                    enabled = !state.isImportingMedia && state.pendingMedia.size < 10,
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(top = 10.dp)
                ) {
                    Text(if (state.isImportingMedia) "Импортируем..." else "Добавить фото или видео")
                }
                if (state.pendingMedia.isNotEmpty()) {
                    MediaPreviewGrid(
                        media = state.pendingMedia,
                        onRemoveMedia = onRemoveMedia,
                        modifier = Modifier.padding(top = 12.dp)
                    )
                    TextButton(onClick = onClearMedia, modifier = Modifier.fillMaxWidth()) {
                        Text("Очистить медиа")
                    }
                }
            }

            IkarSectionCard {
                Text("Текст", style = MaterialTheme.typography.titleLarge)
                BasicTextField(
                    value = state.draftText,
                    onValueChange = onTextChanged,
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(top = 12.dp)
                        .background(Color(0xFFF2F6FA), RoundedCornerShape(18.dp))
                        .padding(14.dp),
                    textStyle = MaterialTheme.typography.bodyLarge.copy(color = MaterialTheme.colorScheme.onSurface),
                    decorationBox = { inner ->
                        if (state.draftText.isBlank()) {
                            Text("Что происходит?", color = TelegramInkMuted, style = MaterialTheme.typography.bodyLarge)
                        }
                        inner()
                    }
                )
                Text("${state.draftText.length}/500", modifier = Modifier.padding(top = 8.dp), style = MaterialTheme.typography.bodySmall, color = TelegramInkMuted)
            }

            IkarSectionCard {
                IkarSectionTitle("Кто увидит", modifier = Modifier.padding(bottom = 10.dp))
                StoryVisibility.entries.forEachIndexed { index, visibility ->
                    StoryOptionRow(
                        title = visibility.label(),
                        subtitle = visibility.subtitle(),
                        selected = state.visibility == visibility,
                        onClick = { onVisibilityChanged(visibility) }
                    )
                    if (index != StoryVisibility.entries.lastIndex) {
                        IkarRowDivider(startIndent = 0.dp)
                    }
                }
            }

            IkarSectionCard {
                IkarSectionTitle("Срок жизни", modifier = Modifier.padding(bottom = 10.dp))
                Row(horizontalArrangement = Arrangement.spacedBy(8.dp), modifier = Modifier.fillMaxWidth()) {
                    listOf(3_600, 43_200, 86_400, 604_800).forEach { ttl ->
                        Surface(
                            modifier = Modifier
                                .weight(1f)
                                .clickable { onTtlChanged(ttl) },
                            color = if (state.ttlSeconds == ttl) Color(0xFFEAF4FF) else Color(0xFFF2F6FA),
                            shape = RoundedCornerShape(16.dp)
                        ) {
                            Text(
                                ttl.labelTtl(),
                                modifier = Modifier.padding(vertical = 12.dp),
                                style = MaterialTheme.typography.bodyMedium,
                                color = if (state.ttlSeconds == ttl) TelegramBlue else MaterialTheme.colorScheme.onSurface,
                                fontWeight = if (state.ttlSeconds == ttl) FontWeight.SemiBold else FontWeight.Normal
                            )
                        }
                    }
                }
            }

            IkarSectionCard {
                IkarToggleRow(
                    "Защитить контент",
                    "Флаг передаётся серверу; клиент должен блокировать пересылку, сохранение и скриншоты там, где это возможно.",
                    state.hasProtectedContent,
                    onProtectedChanged
                )
            }

            state.errorMessage?.let { message ->
                Text(message, color = MaterialTheme.colorScheme.error, style = MaterialTheme.typography.bodySmall)
            }

            Button(
                onClick = onCreateClick,
                enabled = (state.draftText.isNotBlank() || state.pendingMedia.isNotEmpty()) && !state.isSaving,
                modifier = Modifier.fillMaxWidth()
            ) {
                val hasMedia = state.pendingMedia.isNotEmpty()
                Text(
                    when {
                        state.isSaving -> "Публикуем..."
                        hasMedia -> "Поставить в очередь"
                        else -> "Опубликовать"
                    }
                )
            }
        }
    }
}

@Composable
private fun StoryViewerDialog(
    story: StoryDto,
    stories: List<StoryDto>,
    accessToken: String?,
    isOwnStory: Boolean,
    onClose: () -> Unit,
    onNextStory: () -> Unit,
    onPreviousStory: () -> Unit,
    onDeleteStory: () -> Unit
) {
    Dialog(
        onDismissRequest = onClose,
        properties = DialogProperties(usePlatformDefaultWidth = false)
    ) {
        BackHandler(onBack = onClose)

        Box(
            modifier = Modifier
                .fillMaxSize()
                .background(Color.Black)
        ) {
            StoryViewerContent(story = story, accessToken = accessToken)

            Row(modifier = Modifier.fillMaxSize()) {
                Box(
                    modifier = Modifier
                        .weight(1f)
                        .fillMaxSize()
                        .clickable(onClick = onPreviousStory)
                )
                Box(
                    modifier = Modifier
                        .weight(1f)
                        .fillMaxSize()
                        .clickable(onClick = onNextStory)
                )
            }

            StoryViewerTopBar(
                story = story,
                stories = stories,
                accessToken = accessToken,
                isOwnStory = isOwnStory,
                onClose = onClose,
                onDeleteStory = onDeleteStory
            )

            StoryViewerCaption(
                story = story,
                modifier = Modifier.align(Alignment.BottomCenter)
            )
        }
    }
}

@Composable
private fun StoryViewerContent(story: StoryDto, accessToken: String?) {
    val imageAttachment = story.attachments.firstOrNull { it.contentType.startsWith("image/") }
    if (imageAttachment != null) {
        AuthenticatedAsyncImage(
            imageUrl = ServerConfig.normalizeMediaUrl(imageAttachment.downloadPath),
            accessToken = accessToken,
            contentDescription = imageAttachment.fileName,
            modifier = Modifier.fillMaxSize(),
            contentScale = ContentScale.Crop
        )
        return
    }

    Box(
        modifier = Modifier
            .fillMaxSize()
            .padding(28.dp),
        contentAlignment = Alignment.Center
    ) {
        if (story.attachments.isNotEmpty()) {
            val first = story.attachments.first()
            Column(
                horizontalAlignment = Alignment.CenterHorizontally,
                verticalArrangement = Arrangement.spacedBy(10.dp)
            ) {
                Text("Видео", style = MaterialTheme.typography.headlineMedium, color = Color.White)
                Text(
                    first.fileName,
                    style = MaterialTheme.typography.bodyMedium,
                    color = Color.White.copy(alpha = 0.72f),
                    textAlign = TextAlign.Center,
                    maxLines = 2,
                    overflow = TextOverflow.Ellipsis
                )
            }
        } else {
            Text(
                story.text.ifBlank { "История без подписи" },
                style = MaterialTheme.typography.headlineMedium,
                color = Color.White,
                textAlign = TextAlign.Center
            )
        }
    }
}

@Composable
private fun StoryViewerTopBar(
    story: StoryDto,
    stories: List<StoryDto>,
    accessToken: String?,
    isOwnStory: Boolean,
    onClose: () -> Unit,
    onDeleteStory: () -> Unit
) {
    Column(
        modifier = Modifier
            .fillMaxWidth()
            .background(Color.Black.copy(alpha = 0.42f))
            .padding(horizontal = 14.dp, vertical = 12.dp),
        verticalArrangement = Arrangement.spacedBy(12.dp)
    ) {
        StoryProgressSegments(story = story, stories = stories)
        Row(
            modifier = Modifier.fillMaxWidth(),
            verticalAlignment = Alignment.CenterVertically
        ) {
            AvatarView(
                imageUrl = ServerConfig.normalizeMediaUrl(story.author.avatarPath),
                title = story.author.displayName,
                accessToken = accessToken,
                size = 42.dp
            )
            Spacer(modifier = Modifier.width(10.dp))
            Column(modifier = Modifier.weight(1f), verticalArrangement = Arrangement.spacedBy(2.dp)) {
                Text(
                    story.author.displayName,
                    style = MaterialTheme.typography.titleMedium,
                    color = Color.White,
                    maxLines = 1,
                    overflow = TextOverflow.Ellipsis
                )
                Text(
                    story.createdAt.shortDateTime(),
                    style = MaterialTheme.typography.bodySmall,
                    color = Color.White.copy(alpha = 0.72f)
                )
            }
            if (isOwnStory) {
                IconButton(onClick = onDeleteStory) {
                    Icon(Icons.Outlined.Delete, contentDescription = "Удалить историю", tint = Color.White)
                }
            }
            IconButton(onClick = onClose) {
                Icon(Icons.Outlined.Close, contentDescription = "Закрыть", tint = Color.White)
            }
        }
    }
}

@Composable
private fun StoryProgressSegments(story: StoryDto, stories: List<StoryDto>) {
    val selectedIndex = stories.indexOfFirst { it.id == story.id }
    val firstIndex = if (selectedIndex < 0) 0 else (selectedIndex - 6).coerceAtLeast(0)
    val segmentStories = stories.drop(firstIndex).take(12).ifEmpty { listOf(story) }
    Row(horizontalArrangement = Arrangement.spacedBy(4.dp), modifier = Modifier.fillMaxWidth()) {
        segmentStories.forEach { item ->
            Box(
                modifier = Modifier
                    .weight(1f)
                    .height(3.dp)
                    .clip(RoundedCornerShape(2.dp))
                    .background(if (item.id == story.id) Color.White else Color.White.copy(alpha = 0.34f))
            )
        }
    }
}

@Composable
private fun StoryViewerCaption(story: StoryDto, modifier: Modifier = Modifier) {
    Column(
        modifier = modifier
            .fillMaxWidth()
            .background(Color.Black.copy(alpha = 0.54f))
            .padding(horizontal = 18.dp, vertical = 16.dp),
        verticalArrangement = Arrangement.spacedBy(6.dp)
    ) {
        if (story.attachments.isNotEmpty() && story.text.isNotBlank()) {
            Text(story.text, style = MaterialTheme.typography.bodyLarge, color = Color.White)
        }
        Row(verticalAlignment = Alignment.CenterVertically) {
            Text(
                "${story.viewCount} просмотров",
                style = MaterialTheme.typography.bodySmall,
                color = Color.White.copy(alpha = 0.72f)
            )
            if (story.hasProtectedContent) {
                Spacer(modifier = Modifier.width(10.dp))
                Text(
                    "защищено",
                    style = MaterialTheme.typography.labelSmall,
                    color = Color.White.copy(alpha = 0.72f)
                )
            }
        }
    }
}

@Composable
private fun StoryTransfersCard(
    transfers: List<StoryTransferUiState>,
    onRetryTransfer: (String) -> Unit,
    onCancelTransfer: (String) -> Unit
) {
    IkarSectionCard {
        IkarSectionTitle("Очередь историй", modifier = Modifier.padding(bottom = 8.dp))
        transfers.forEachIndexed { index, transfer ->
            Column(verticalArrangement = Arrangement.spacedBy(8.dp), modifier = Modifier.padding(vertical = 8.dp)) {
                Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceBetween) {
                    Text(transfer.title, style = MaterialTheme.typography.titleMedium, maxLines = 1, overflow = TextOverflow.Ellipsis)
                    Text("${transfer.progressPercent}%", style = MaterialTheme.typography.bodySmall, color = TelegramInkMuted)
                }
                LinearProgressIndicator(
                    progress = { transfer.progressPercent / 100f },
                    modifier = Modifier.fillMaxWidth(),
                    color = TelegramBlue
                )
                Text(transfer.errorMessage ?: transfer.status, style = MaterialTheme.typography.bodySmall, color = TelegramInkMuted)
                Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) {
                    TextButton(onClick = { onRetryTransfer(transfer.groupId) }) { Text("Повторить") }
                    TextButton(onClick = { onCancelTransfer(transfer.groupId) }) { Text("Отменить") }
                }
            }
            if (index != transfers.lastIndex) {
                IkarRowDivider(startIndent = 0.dp)
            }
        }
    }
}

@Composable
private fun MediaPreviewGrid(
    media: List<StoryMediaPreviewUiState>,
    onRemoveMedia: (String) -> Unit,
    modifier: Modifier = Modifier
) {
    Column(modifier = modifier, verticalArrangement = Arrangement.spacedBy(10.dp)) {
        media.chunked(2).forEach { row ->
            Row(horizontalArrangement = Arrangement.spacedBy(10.dp), modifier = Modifier.fillMaxWidth()) {
                row.forEach { item ->
                    StoryMediaTile(
                        item = item,
                        onRemoveMedia = onRemoveMedia,
                        modifier = Modifier.weight(1f)
                    )
                }
                if (row.size == 1) {
                    Box(modifier = Modifier.weight(1f))
                }
            }
        }
    }
}

@Composable
private fun StoryMediaTile(
    item: StoryMediaPreviewUiState,
    onRemoveMedia: (String) -> Unit,
    modifier: Modifier = Modifier
) {
    Surface(modifier = modifier, color = Color(0xFFF2F6FA), shape = RoundedCornerShape(18.dp)) {
        Column(verticalArrangement = Arrangement.spacedBy(8.dp), modifier = Modifier.padding(10.dp)) {
            Box(
                modifier = Modifier
                    .fillMaxWidth()
                    .aspectRatio(1f)
                    .clip(RoundedCornerShape(14.dp))
                    .background(Color(0xFFEAF4FF)),
                contentAlignment = Alignment.Center
            ) {
                if (item.contentType.startsWith("image/")) {
                    AsyncImage(
                        model = File(item.localPath),
                        contentDescription = null,
                        modifier = Modifier.fillMaxSize(),
                        contentScale = ContentScale.Crop
                    )
                } else {
                    Text("ВИДЕО", color = TelegramBlue, fontWeight = FontWeight.SemiBold)
                }
            }
            Text(item.fileName, maxLines = 1, overflow = TextOverflow.Ellipsis, style = MaterialTheme.typography.bodySmall)
            Text(item.fileSizeBytes.formatBytes(), style = MaterialTheme.typography.bodySmall, color = TelegramInkMuted)
            TextButton(onClick = { onRemoveMedia(item.localPath) }, modifier = Modifier.fillMaxWidth()) {
                Text("Удалить")
            }
        }
    }
}

@Composable
private fun EmptyStoriesState(onComposeClick: () -> Unit) {
    Column(
        modifier = Modifier
            .fillMaxWidth()
            .padding(vertical = 18.dp),
        horizontalAlignment = Alignment.CenterHorizontally,
        verticalArrangement = Arrangement.spacedBy(10.dp)
    ) {
        Box(
            modifier = Modifier
                .size(62.dp)
                .clip(CircleShape)
                .background(Color(0xFFEAF4FF)),
            contentAlignment = Alignment.Center
        ) {
            Icon(Icons.Outlined.Add, contentDescription = null, tint = TelegramBlue)
        }
        Text("Пока нет активных историй", style = MaterialTheme.typography.titleMedium)
        Text("Опубликуйте первую историю со сроком жизни, приватностью и медиа.", style = MaterialTheme.typography.bodySmall, color = TelegramInkMuted)
        TextButton(onClick = onComposeClick) { Text("Создать историю") }
    }
}

@Composable
private fun StoryPreviewRow(
    story: StoryDto,
    accessToken: String?,
    onClick: () -> Unit
) {
    Row(
        modifier = Modifier
            .fillMaxWidth()
            .clickable(onClick = onClick)
            .padding(vertical = 12.dp),
        horizontalArrangement = Arrangement.spacedBy(14.dp),
        verticalAlignment = Alignment.CenterVertically
    ) {
        Surface(
            modifier = Modifier.size(48.dp),
            color = if (story.viewedByMe) Color(0xFFF1F3F5) else Color(0xFFEAF4FF),
            shape = CircleShape
        ) {
            Box(contentAlignment = Alignment.Center) {
                val preview = story.attachments.firstOrNull { it.contentType.startsWith("image/") }
                if (preview != null) {
                    AuthenticatedAsyncImage(
                        imageUrl = ServerConfig.normalizeMediaUrl(preview.downloadPath),
                        accessToken = accessToken,
                        contentDescription = preview.fileName,
                        modifier = Modifier.fillMaxSize(),
                        contentScale = ContentScale.Crop
                    )
                } else {
                    Text(
                        text = story.author.displayName.firstOrNull()?.uppercaseChar()?.toString()
                            ?: story.author.username.firstOrNull()?.uppercaseChar()?.toString().orEmpty(),
                        style = MaterialTheme.typography.titleLarge,
                        color = TelegramBlue,
                        fontWeight = FontWeight.SemiBold
                    )
                }
            }
        }
        Column(modifier = Modifier.weight(1f), verticalArrangement = Arrangement.spacedBy(3.dp)) {
            Text(story.author.displayName, style = MaterialTheme.typography.titleLarge)
            Text(story.text.ifBlank { "${story.attachments.size} медиа" }, style = MaterialTheme.typography.bodySmall, color = TelegramInkMuted, maxLines = 1, overflow = TextOverflow.Ellipsis)
            Text("до ${story.expiresAt.shortDateTime()} · ${story.viewCount} просмотров", style = MaterialTheme.typography.bodySmall, color = TelegramInkMuted)
        }
        if (story.hasProtectedContent) {
            Text("защищено", style = MaterialTheme.typography.labelSmall, color = TelegramBlue)
        }
    }
}

@Composable
private fun StoryAttachmentStrip(story: StoryDto, accessToken: String?) {
    if (story.attachments.isEmpty()) {
        return
    }

    Column(verticalArrangement = Arrangement.spacedBy(8.dp)) {
        story.attachments.take(4).forEach { attachment ->
            StoryAttachmentPreview(attachment = attachment, accessToken = accessToken)
        }
        if (story.attachments.size > 4) {
            Text("+${story.attachments.size - 4} медиа", style = MaterialTheme.typography.bodySmall, color = TelegramInkMuted)
        }
    }
}

@Composable
private fun StoryAttachmentPreview(attachment: AttachmentDto, accessToken: String?) {
    if (attachment.contentType.startsWith("image/")) {
        AuthenticatedAsyncImage(
            imageUrl = ServerConfig.normalizeMediaUrl(attachment.downloadPath),
            accessToken = accessToken,
            contentDescription = attachment.fileName,
            modifier = Modifier
                .fillMaxWidth()
                .aspectRatio(16f / 10f)
                .clip(RoundedCornerShape(18.dp)),
            contentScale = ContentScale.Crop
        )
    } else {
        Surface(color = Color(0xFFEAF4FF), shape = RoundedCornerShape(18.dp), modifier = Modifier.fillMaxWidth()) {
            Column(modifier = Modifier.padding(14.dp), verticalArrangement = Arrangement.spacedBy(4.dp)) {
                Text("Видео", color = TelegramBlue, fontWeight = FontWeight.SemiBold)
                Text(attachment.fileName, maxLines = 1, overflow = TextOverflow.Ellipsis, style = MaterialTheme.typography.bodySmall, color = TelegramInkMuted)
            }
        }
    }
}

@Composable
private fun StoryOptionRow(
    title: String,
    subtitle: String,
    selected: Boolean,
    onClick: () -> Unit
) {
    Row(
        modifier = Modifier
            .fillMaxWidth()
            .clickable(onClick = onClick)
            .padding(vertical = 12.dp),
        horizontalArrangement = Arrangement.spacedBy(12.dp),
        verticalAlignment = Alignment.CenterVertically
    ) {
        Box(
            modifier = Modifier
                .size(20.dp)
                .clip(CircleShape)
                .background(if (selected) TelegramBlue else Color(0xFFDDE7F0))
        )
        Column(modifier = Modifier.weight(1f), verticalArrangement = Arrangement.spacedBy(2.dp)) {
            Text(title, style = MaterialTheme.typography.titleMedium)
            Text(subtitle, style = MaterialTheme.typography.bodySmall, color = TelegramInkMuted)
        }
    }
}

private fun StoryVisibility.label(): String = when (this) {
    StoryVisibility.Everyone -> "Все"
    StoryVisibility.Contacts -> "Контакты"
    StoryVisibility.CloseFriends -> "Близкие друзья"
    StoryVisibility.Private -> "Только я"
}

private fun StoryVisibility.subtitle(): String = when (this) {
    StoryVisibility.Everyone -> "История доступна всем пользователям."
    StoryVisibility.Contacts -> "Показывать только контактам."
    StoryVisibility.CloseFriends -> "Использовать приватный круг близких."
    StoryVisibility.Private -> "Скрыть от всех кроме автора."
}

private fun Int.labelTtl(): String = when (this) {
    3_600 -> "1 ч"
    43_200 -> "12 ч"
    86_400 -> "24 ч"
    604_800 -> "7 д"
    else -> "${this / 3_600} ч"
}

private fun Long.formatBytes(): String = when {
    this >= 1024L * 1024L -> "${this / (1024L * 1024L)} MB"
    this >= 1024L -> "${this / 1024L} KB"
    else -> "$this B"
}

private fun String.shortDateTime(): String =
    take(16).replace('T', ' ')
