import SwiftUI

struct iPhoneLibraryView: View {
    @State private var viewModel = iPhoneLibraryViewModel()

    var body: some View {
        NavigationStack {
            List(viewModel.tracks) { track in
                VStack(alignment: .leading, spacing: 4) {
                    Text(track.title)
                        .font(.headline)
                    Text(track.artist)
                        .foregroundStyle(.secondary)
                }
            }
            .overlay {
                if viewModel.tracks.isEmpty {
                    ContentUnavailableView(
                        "No Local Tracks Yet",
                        systemImage: "music.note.list",
                        description: Text("This iPhone target currently exposes the offline catalog shell only.")
                    )
                }
            }
            .navigationTitle("Velody")
            .toolbar {
                ToolbarItem(placement: .topBarTrailing) {
                    Button("Sync") {
                        Task {
                            await viewModel.refreshSync()
                        }
                    }
                }
            }
            .safeAreaInset(edge: .bottom) {
                Text(viewModel.syncStatus)
                    .font(.footnote)
                    .foregroundStyle(.secondary)
                    .padding()
                    .frame(maxWidth: .infinity, alignment: .leading)
                    .background(.ultraThinMaterial)
            }
        }
        .task {
            await viewModel.loadIfNeeded()
        }
    }
}
