//
//  SplitView.swift
//  kordophone2
//
//  Created by James Magahern on 8/29/25.
//

import SwiftUI

struct SplitView: View
{
    @State var conversationListModel = ConversationListView.ViewModel()
    @State var transcriptViewModel = TranscriptView.ViewModel()
    @State var entryViewModel = MessageEntryView.ViewModel()
    
    private let xpcClient = XPCClient()
    private var selectedConversation: Display.Conversation? {
        guard let id = conversationListModel.selectedConversation else { return nil }
        return conversationListModel.conversations.first { $0.id == id }
    }

    var body: some View {
        NavigationSplitView {
            ConversationListView(model: $conversationListModel)
                .frame(minWidth: 330.0)
                .xpcClient(xpcClient)
        } detail: {
            ConversationView(transcriptModel: $transcriptViewModel, entryModel: $entryViewModel)
                .xpcClient(xpcClient)
                .selectedConversation(selectedConversation)
                .navigationTitle(selectedConversation?.displayName ?? "Kordophone")
                .navigationSubtitle(selectedConversation?.participants.joined(separator: ", ") ?? "")
                .onChange(of: conversationListModel.selectedConversation) { oldValue, newValue in
                    transcriptViewModel.displayedConversation = conversationListModel.conversations.first { $0.id == newValue }
                }
        }
    }
}
