import SwiftUI

/// Sheet for picking a chat to forward a message to.
/// Shows all existing dialogs sorted by last message time.
struct ForwardChatPickerView: View {
    let onSelect: (ChatRoute) -> Void
    @Environment(\.dismiss) private var dismiss

    /// Android parity: system accounts (Updates, Safe) excluded from forward picker.
    /// Saved Messages allowed (forward to self).
    private var dialogs: [Dialog] {
        DialogRepository.shared.sortedDialogs.filter {
            ($0.iHaveSent || $0.isSavedMessages) && !SystemAccounts.isSystemAccount($0.opponentKey)
        }
    }

    var body: some View {
        let _ = print("[ForwardPicker] body — dialogs count: \(dialogs.count)")
        NavigationStack {
            List(dialogs) { dialog in
                Button {
                    onSelect(ChatRoute(dialog: dialog))
                } label: {
                    HStack(spacing: 12) {
                        AvatarView(
                            initials: dialog.initials,
                            colorIndex: dialog.avatarColorIndex,
                            size: 42,
                            isSavedMessages: dialog.isSavedMessages
                        )

                        VStack(alignment: .leading, spacing: 2) {
                            HStack(spacing: 4) {
                                Text(dialog.isSavedMessages ? "Saved Messages" : dialog.opponentTitle)
                                    .font(.system(size: 16, weight: .medium))
                                    .foregroundStyle(RosettaColors.Adaptive.text)
                                    .lineLimit(1)

                                if dialog.effectiveVerified > 0 && !dialog.isSavedMessages {
                                    VerifiedBadge(verified: dialog.effectiveVerified, size: 14)
                                }
                            }

                            if !dialog.opponentUsername.isEmpty && !dialog.isSavedMessages {
                                Text("@\(dialog.opponentUsername)")
                                    .font(.system(size: 13, weight: .regular))
                                    .foregroundStyle(RosettaColors.Adaptive.textSecondary)
                                    .lineLimit(1)
                            }
                        }

                        Spacer()
                    }
                    .contentShape(Rectangle())
                }
                .listRowBackground(RosettaColors.Dark.surface)
            }
            .listStyle(.plain)
            .scrollContentBackground(.hidden)
            .background(RosettaColors.Dark.background)
            .navigationTitle("Forward to...")
            .navigationBarTitleDisplayMode(.inline)
            .toolbar {
                ToolbarItem(placement: .cancellationAction) {
                    Button("Cancel") { dismiss() }
                        .foregroundStyle(RosettaColors.Adaptive.text)
                }
            }
        }
        .preferredColorScheme(.dark)
    }
}
