import SwiftUI
import VoiceCatCore

struct MoveUserView: View {
    let user: User
    @Bindable var session: SessionState
    @Environment(\.dismiss) private var dismiss

    @State private var selectedChannelId: UInt32 = 0

    var body: some View {
        NavigationStack {
            List(session.channels) { channel in
                HStack {
                    Text(channel.name)
                    Spacer()
                    if channel.id == selectedChannelId {
                        Image(systemName: "checkmark")
                            .foregroundStyle(.blue)
                            .accessibilityHidden(true)
                    }
                }
                .contentShape(Rectangle())
                .onTapGesture { selectedChannelId = channel.id }
                .accessibilityElement(children: .combine)
                .accessibilityLabel("\(channel.name)\(channel.id == selectedChannelId ? ", selected" : "")")
                .accessibilityAddTraits(channel.id == selectedChannelId ? .isSelected : [])
            }
            .navigationTitle("Move \(user.nickname)")
            .navigationBarTitleDisplayMode(.inline)
            .toolbar {
                ToolbarItem(placement: .cancellationAction) {
                    Button("Cancel") { dismiss() }
                }
                ToolbarItem(placement: .confirmationAction) {
                    Button("Move") {
                        session.moveUser(user.id, toChannel: selectedChannelId)
                        dismiss()
                    }
                    .disabled(selectedChannelId == 0)
                }
            }
        }
        .onAppear {
            selectedChannelId = user.channelId
        }
    }
}
