import SwiftUI
import Models

struct ThreadListView: View {
	@Bindable var viewModel: MailViewModel
	@State var showMoveSheet = false

	var body: some View {
		List(viewModel.threads, selection: Binding(
			get: { viewModel.selectedThread?.id },
			set: { newId in
				if let thread = viewModel.threads.first(where: { $0.id == newId }) {
					viewModel.selectThread(thread)
				}
			}
		)) { thread in
			ThreadRow(thread: thread)
				.tag(thread.id)
				.swipeActions(edge: .leading) {
					Button {
						viewModel.selectThread(thread)
						viewModel.archiveSelectedThread()
					} label: {
						Label("Archive", systemImage: "archivebox")
					}
					.tint(.green)
				}
				.swipeActions(edge: .trailing) {
					Button(role: .destructive) {
						viewModel.selectThread(thread)
						viewModel.deleteSelectedThread()
					} label: {
						Label("Delete", systemImage: "trash")
					}
				}
		}
		.listStyle(.inset)
		.navigationTitle(viewModel.selectedMailbox?.name ?? "Mail")
		.overlay {
			if viewModel.threads.isEmpty {
				ContentUnavailableView(
					"No Messages",
					systemImage: "tray",
					description: Text("No threads in this mailbox")
				)
			}
		}
		.toolbar {
			ToolbarItemGroup(placement: .automatic) {
				if viewModel.selectedThread != nil {
					Button {
						viewModel.archiveSelectedThread()
					} label: {
						Label("Archive", systemImage: "archivebox")
					}
					.keyboardShortcut("e", modifiers: [])
					.help("Archive (e)")

					Button {
						viewModel.deleteSelectedThread()
					} label: {
						Label("Delete", systemImage: "trash")
					}
					.keyboardShortcut(.delete, modifiers: [])
					.help("Delete (⌫)")

					Button {
						viewModel.toggleFlagSelectedThread()
					} label: {
						Label("Flag", systemImage: "flag")
					}
					.keyboardShortcut("s", modifiers: [])
					.help("Toggle Flag (s)")

					Button {
						viewModel.toggleReadSelectedThread()
					} label: {
						Label("Read/Unread", systemImage: "envelope.badge")
					}
					.keyboardShortcut("u", modifiers: [.shift, .command])
					.help("Toggle Read/Unread (⇧⌘U)")

					Button {
						showMoveSheet = true
					} label: {
						Label("Move", systemImage: "folder")
					}
					.keyboardShortcut("m", modifiers: [.shift, .command])
					.help("Move to… (⇧⌘M)")
				}
			}
		}
		.sheet(isPresented: $showMoveSheet) {
			MoveToSheet(mailboxes: viewModel.mailboxes) { mailbox in
				viewModel.moveSelectedThread(to: mailbox)
			}
		}
	}
}

struct ThreadRow: View {
	let thread: ThreadSummary

	var body: some View {
		VStack(alignment: .leading, spacing: 4) {
			HStack {
				Text(thread.senders)
					.fontWeight(thread.unreadCount > 0 ? .bold : .regular)
					.lineLimit(1)
				Spacer()
				if thread.unreadCount > 0 {
					Circle()
						.fill(.blue)
						.frame(width: 8, height: 8)
				}
				Text(thread.lastDate, style: .relative)
					.font(.caption)
					.foregroundStyle(.secondary)
			}
			Text(thread.subject ?? "(No Subject)")
				.font(.subheadline)
				.lineLimit(1)
			if let snippet = thread.snippet {
				Text(snippet)
					.font(.caption)
					.foregroundStyle(.tertiary)
					.lineLimit(1)
			}
			if thread.messageCount > 1 {
				Text("\(thread.messageCount)")
					.font(.caption2)
					.foregroundStyle(.secondary)
					.padding(.horizontal, 6)
					.padding(.vertical, 1)
					.background(.quaternary, in: Capsule())
			}
		}
		.padding(.vertical, 2)
	}
}
