import Foundation
import CoreData

@objc(Tag)
public class Tag: NSManagedObject {

}

extension Tag {

    @nonobjc public class func fetchRequest() -> NSFetchRequest<Tag> {
        return NSFetchRequest<Tag>(entityName: "Tag")
    }

    @NSManaged public var name: String
    @NSManaged public var slug: String
    @NSManaged public var tagDescription: String?
    @NSManaged public var createdAt: Date
    @NSManaged public var links: NSSet?

}

// MARK: Generated accessors for links
extension Tag {

    @objc(addLinksObject:)
    @NSManaged public func addToLinks(_ value: Link)

    @objc(removeLinksObject:)
    @NSManaged public func removeFromLinks(_ value: Link)

    @objc(addLinks:)
    @NSManaged public func addToLinks(_ values: NSSet)

    @objc(removeLinks:)
    @NSManaged public func removeFromLinks(_ values: NSSet)

}

extension Tag : Identifiable {

    public var id: NSManagedObjectID {
        return self.objectID
    }

    var linkCount: Int {
        return links?.count ?? 0
    }

    var sortedLinks: [Link] {
        let linkSet = links as? Set<Link> ?? []
        return linkSet.sorted { $0.alias < $1.alias }
    }
}
