import SwiftUI /// Upload button for playlist headers — visible only when playlist has local tracks and Chad Music is configured. /// Shows uploaded/total count (e.g., "↑ 2/9"). struct PlaylistUploadButton: View { let playlist: Playlist @State private var uploadService = UploadService.shared private var localTracks: [Track] { playlist.sortedEntries.compactMap(\.track).filter { !$0.isCloud && !$0.filePath.isEmpty } } private var uploadedCount: Int { localTracks.filter { $0.uploadState == .uploaded }.count } private var uploadingCount: Int { localTracks.filter { $0.uploadState == .uploading }.count } var body: some View { let local = localTracks if !local.isEmpty && ChadMusicAPIClient.shared.isConfigured { Button { handleTap(local) } label: { Label(buttonLabel(local), systemImage: "arrow.up.circle") } .help(helpText(local)) } } private func buttonLabel(_ local: [Track]) -> String { "↑ \(uploadedCount)/\(local.count)" } private func helpText(_ local: [Track]) -> String { let total = local.count if uploadedCount == total { return "All local tracks uploaded to cloud" } if uploadingCount > 0 { return "Uploading... tap to cancel" } return "Upload \(total - uploadedCount) local tracks to cloud" } private func handleTap(_ local: [Track]) { if uploadingCount > 0 { uploadService.cancel() } else if uploadedCount < local.count { let eligible = local.filter { $0.uploadState != .uploaded } uploadService.uploadBatch(tracks: eligible, apiClient: ChadMusicAPIClient.shared) } } }