import SwiftData
import SwiftUI

/// "Add to Playlist" sheet — pick which playlist to add a track to.
struct AddToPlaylistSheet: View {
    let track: Track
    @Environment(PlaylistViewModel.self) private var playlistVM
    @EnvironmentObject private var theme: AppTheme
    @Environment(\.modelContext) private var modelContext
    @Environment(\.dismiss) private var dismiss

    @Query(sort: \Playlist.dateModified, order: .reverse) private var playlists: [Playlist]
    @State private var showNewPlaylist = false
    @State private var newPlaylistName = ""

    var body: some View {
        NavigationStack {
            List {
                Section {
                    Button {
                        showNewPlaylist = true
                    } label: {
                        Label("New Playlist", systemImage: "plus.circle")
                            .foregroundStyle(theme.accent)
                    }
                }

                Section("Existing Playlists") {
                    ForEach(playlists) { playlist in
                        let isDuplicate = playlistVM.isDuplicate(track: track, in: playlist, context: modelContext)

                        Button {
                            playlistVM.addTrack(track, to: playlist, context: modelContext)
                            dismiss()
                        } label: {
                            HStack {
                                Circle()
                                    .fill(Color(hex: playlist.color) ?? theme.accent)
                                    .frame(width: 10, height: 10)
                                Text(playlist.name)
                                    .foregroundStyle(theme.primaryText)
                                Spacer()
                                if isDuplicate {
                                    Text("Already added")
                                        .font(.caption)
                                        .foregroundStyle(theme.tertiaryText)
                                }
                                Text("\(playlist.trackCount)")
                                    .font(.caption)
                                    .foregroundStyle(theme.tertiaryText)
                            }
                        }
                        .disabled(isDuplicate)
                        .opacity(isDuplicate ? 0.5 : 1)
                    }
                }
            }
            .navigationTitle("Add to Playlist")
            .navigationBarTitleDisplayMode(.inline)
            .toolbar {
                ToolbarItem(placement: .cancellationAction) {
                    Button("Cancel") { dismiss() }
                }
            }
            .alert("New Playlist", isPresented: $showNewPlaylist) {
                TextField("Playlist name", text: $newPlaylistName)
                Button("Cancel", role: .cancel) { newPlaylistName = "" }
                Button("Create & Add") {
                    guard !newPlaylistName.isEmpty else { return }
                    let pl = playlistVM.createPlaylist(name: newPlaylistName, context: modelContext)
                    playlistVM.addTrack(track, to: pl, context: modelContext)
                    dismiss()
                }
            }
        }
    }
}
