import SwiftData
import SwiftUI

/// Sheet for adding multiple tracks (e.g. an album, artist, folder) to a playlist.
struct AddGroupToPlaylistSheet: View {
    let tracks: [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 {
                    Text("Adding \(tracks.count) tracks")
                        .foregroundStyle(theme.secondaryText)
                }

                Section {
                    Button {
                        showNewPlaylist = true
                    } label: {
                        Label("New Playlist", systemImage: "plus.circle")
                            .foregroundStyle(theme.accent)
                    }
                }

                Section("Existing Playlists") {
                    ForEach(playlists) { playlist in
                        Button {
                            playlistVM.addTracks(tracks, to: playlist, context: modelContext)
                            playlistVM.showStatus("Added \(tracks.count) tracks to \(playlist.name)")
                            dismiss()
                        } label: {
                            HStack {
                                Circle()
                                    .fill(Color(hex: playlist.color) ?? theme.accent)
                                    .frame(width: 10, height: 10)
                                Text(playlist.name)
                                    .foregroundStyle(theme.primaryText)
                                Spacer()
                                Text("\(playlist.trackCount)")
                                    .font(.caption)
                                    .foregroundStyle(theme.tertiaryText)
                            }
                        }
                    }
                }
            }
            .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.addTracks(tracks, to: pl, context: modelContext)
                    playlistVM.showStatus("Created \(newPlaylistName) with \(tracks.count) tracks")
                    dismiss()
                }
            }
        }
    }
}
