//
//  ActiveInstance.swift
//  Tusker
//
//  Created by Shadowfacts on 5/28/23.
//  Copyright © 2023 Shadowfacts. All rights reserved.
//

import Foundation
import CoreData
import Pachyderm
import InstanceFeatures

@objc(ActiveInstance)
public final class ActiveInstance: NSManagedObject {
    
    @nonobjc class public func fetchRequest() -> NSFetchRequest<ActiveInstance> {
        return NSFetchRequest(entityName: "ActiveInstance")
    }
    
    @NSManaged public var version: String
    @NSManaged public var maxStatusCharacters: Int
    @NSManaged private var configurationData: Data?
    @NSManaged private var pollsConfigurationData: Data?
    
    @LazilyDecoding(from: \ActiveInstance.configurationData, fallback: nil)
    public var configuration: Instance.Configuration?
    
    @LazilyDecoding(from: \ActiveInstance.pollsConfigurationData, fallback: nil)
    public var pollsConfiguration: Instance.PollsConfiguration?
    
    func update(from instance: Instance) {
        self.version = instance.version
        self.maxStatusCharacters = instance.maxStatusCharacters ?? 500
        self.configuration = instance.configuration
        self.pollsConfiguration = instance.pollsConfiguration
    }
}

extension InstanceInfo {
    init(activeInstance: ActiveInstance) {
        self.init(
            version: activeInstance.version,
            maxStatusCharacters: activeInstance.maxStatusCharacters,
            configuration: activeInstance.configuration,
            pollsConfiguration: activeInstance.pollsConfiguration
        )
    }
}
