//
//  SavedHashtag.swift
//  Tusker
//
//  Created by Shadowfacts on 5/8/22.
//  Copyright © 2022 Shadowfacts. All rights reserved.
//

import Foundation
import CoreData
import Pachyderm
import WebURLFoundationExtras
import UserAccounts

@objc(SavedHashtag)
public final class SavedHashtag: NSManagedObject {
    
    @nonobjc class func fetchRequestWithoutAccountForMigrating() -> NSFetchRequest<SavedHashtag> {
        return NSFetchRequest<SavedHashtag>(entityName: "SavedHashtag")
    }
    
    @nonobjc class func fetchRequest(account: UserAccountInfo) -> NSFetchRequest<SavedHashtag> {
        let req = NSFetchRequest<SavedHashtag>(entityName: "SavedHashtag")
        req.predicate = NSPredicate(format: "accountID = %@", account.id)
        return req
    }
    
    @nonobjc class func fetchRequest(name: String, account: UserAccountInfo) -> NSFetchRequest<SavedHashtag> {
        let req = NSFetchRequest<SavedHashtag>(entityName: "SavedHashtag")
        req.predicate = NSPredicate(format: "name LIKE[cd] %@ AND accountID = %@", name, account.id)
        return req
    }
    
    @NSManaged public var accountID: String
    @NSManaged public var name: String
    @NSManaged public var url: URL
    
}

extension SavedHashtag {
    convenience init(hashtag: Hashtag, account: UserAccountInfo, context: NSManagedObjectContext) {
        self.init(context: context)
        self.accountID = account.id
        self.name = hashtag.name
        self.url = URL(hashtag.url)!
    }
}
