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

import Foundation
import CoreData
import UserAccounts

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

extension SavedInstance {
    convenience init(url: URL, account: UserAccountInfo, context: NSManagedObjectContext) {
        self.init(context: context)
        self.accountID = account.id
        self.url = url
    }
}
