import SwiftUI
import Models

struct MoveToSheet: View {
	let mailboxes: [MailboxInfo]
	let onSelect: (MailboxInfo) -> Void

	@Environment(\.dismiss) private var dismiss
	@State private var searchText = ""

	private var filteredMailboxes: [MailboxInfo] {
		if searchText.isEmpty {
			return mailboxes
		}
		return mailboxes.filter { $0.name.localizedCaseInsensitiveContains(searchText) }
	}

	var body: some View {
		NavigationStack {
			List(filteredMailboxes) { mailbox in
				Button {
					onSelect(mailbox)
					dismiss()
				} label: {
					Label(mailbox.name, systemImage: mailbox.systemImage)
				}
			}
			.searchable(text: $searchText, prompt: "Search mailboxes")
			.navigationTitle("Move to…")
			#if os(iOS)
			.navigationBarTitleDisplayMode(.inline)
			#endif
			.toolbar {
				ToolbarItem(placement: .cancellationAction) {
					Button("Cancel") {
						dismiss()
					}
				}
			}
		}
		#if os(macOS)
		.frame(minWidth: 300, minHeight: 400)
		#endif
	}
}
