//
//  ObjectBoxService.swift
//  SodaLive
//
//  Created by klaus on 2023/08/11.
//

import Foundation
import ObjectBox

class ObjectBoxService {
    let store: Store!
    let playbackTrackingBox: Box<PlaybackTracking>
    
    init() {
        let databaseName = "sodalive"
        let appSupport = try! FileManager.default.url(for: .applicationSupportDirectory,
                                                      in: .userDomainMask,
                                                      appropriateFor: nil,
                                                      create: true)
            .appendingPathComponent(Bundle.main.bundleIdentifier!)
        let directory = appSupport.appendingPathComponent(databaseName)
        try? FileManager.default.createDirectory(at: directory,
                                                 withIntermediateDirectories: true,
                                                 attributes: nil)
        
        if try! Store.isOpen(directory: directory.path) {
            self.store = try! Store.attachTo(directory: directory.path)
        } else {
            self.store = try! Store(directoryPath: directory.path)
        }
        self.playbackTrackingBox = store.box(for: PlaybackTracking.self)
    }
}
