import Foundation
import SwiftData

/// A folder that groups playlists together.
@Model
final class PlaylistFolder {
    var id: UUID
    var name: String
    var dateCreated: Date
    var isExpanded: Bool

    @Relationship(deleteRule: .nullify, inverse: \Playlist.folder)
    var playlists: [Playlist]

    var sortedPlaylists: [Playlist] {
        playlists.sorted { $0.dateModified > $1.dateModified }
    }

    var totalTrackCount: Int {
        playlists.reduce(0) { $0 + $1.trackCount }
    }

    init(name: String) {
        self.id = UUID()
        self.name = name
        self.dateCreated = Date()
        self.isExpanded = true
        self.playlists = []
    }
}
