import SwiftUI
import VelodyDomain

struct MacLibraryView: View {
    @State private var viewModel = MacLibraryViewModel()

    var body: some View {
        NavigationSplitView {
            List(viewModel.tracks) { track in
                VStack(alignment: .leading, spacing: 4) {
                    Text(track.title)
                        .font(.headline)
                    Text(track.artist)
                        .foregroundStyle(.secondary)
                    if let album = track.album {
                        Text(album)
                            .font(.caption)
                            .foregroundStyle(.secondary)
                    }
                }
                .padding(.vertical, 4)
            }
            .navigationTitle("Velody")
        } detail: {
            VStack(alignment: .leading, spacing: 16) {
                Text("Private Library Foundation")
                    .font(.largeTitle)
                Text("Selected folder")
                    .font(.headline)
                Text(viewModel.selectedFolderPath)
                    .textSelection(.enabled)
                Text("Sync status")
                    .font(.headline)
                Text(viewModel.syncStatus)
                    .foregroundStyle(.secondary)

                HStack {
                    Button("Choose Watched Folder") {
                        viewModel.chooseFolder()
                    }
                    Button("Refresh Placeholder Sync") {
                        Task {
                            await viewModel.refreshSyncStatus()
                        }
                    }
                }

                Spacer()
            }
            .padding(24)
        }
        .task {
            await viewModel.loadIfNeeded()
        }
    }
}
