import SwiftUI

/// Telegram-style in-app notification banner with glass background.
/// Shows sender avatar, name, and message preview. Supports swipe-up
/// to dismiss and tap to navigate to the chat.
struct InAppNotificationBanner: View {

    let notification: InAppNotificationManager.InAppNotification
    let onTap: () -> Void
    let onDismiss: () -> Void

    @State private var dragOffset: CGFloat = 0

    var body: some View {
        HStack(spacing: 12) {
            AvatarView(
                initials: notification.initials,
                colorIndex: notification.avatarColorIndex,
                size: 40,
                image: notification.avatar
            )

            VStack(alignment: .leading, spacing: 2) {
                Text(notification.senderName)
                    .font(.system(size: 15, weight: .semibold))
                    .foregroundStyle(Color(RosettaColors.Adaptive.text))
                    .lineLimit(1)

                Text(notification.messagePreview)
                    .font(.system(size: 14))
                    .foregroundStyle(Color(RosettaColors.Adaptive.textSecondary))
                    .lineLimit(2)
            }

            Spacer(minLength: 0)
        }
        .padding(.horizontal, 14)
        .padding(.vertical, 10)
        .frame(maxWidth: .infinity)
        .background { TelegramGlassRoundedRect(cornerRadius: 16) }
        .padding(.horizontal, 8)
        .offset(y: min(dragOffset, 0))
        .gesture(
            DragGesture(minimumDistance: 8)
                .onChanged { value in
                    // Only allow dragging upward (negative Y).
                    dragOffset = min(value.translation.height, 0)
                }
                .onEnded { value in
                    if value.translation.height < -30 {
                        onDismiss()
                    } else {
                        withAnimation(.spring(response: 0.3, dampingFraction: 0.8)) {
                            dragOffset = 0
                        }
                    }
                }
        )
        .contentShape(Rectangle())
        .onTapGesture { onTap() }
    }
}
