import SwiftUI

struct ThreadDetailView: View {
	let thread: ThreadSummary?
	let messages: [EmailMessage]

	var body: some View {
		Group {
			if let thread {
				ScrollView {
					VStack(alignment: .leading, spacing: 0) {
						Text(thread.subject)
							.font(.title2)
							.fontWeight(.semibold)
							.padding()

						Divider()

						ForEach(messages) { message in
							MessageView(message: message)
							Divider()
						}
					}
				}
			} else {
				ContentUnavailableView(
					"No Thread Selected",
					systemImage: "envelope",
					description: Text("Select a thread to read")
				)
			}
		}
	}
}

struct MessageView: View {
	let message: EmailMessage

	var body: some View {
		VStack(alignment: .leading, spacing: 8) {
			HStack {
				Text(message.senderName)
					.fontWeight(.semibold)
				Spacer()
				Text(message.date)
					.font(.caption)
					.foregroundStyle(.secondary)
			}

			Text("To: \(message.toHeader)")
				.font(.caption)
				.foregroundStyle(.secondary)

			Text(message.body)
				.font(.body)
				.textSelection(.enabled)
		}
		.padding()
	}
}
