import APIClient
import Foundation
import SwiftData

// class InAppNotificationDatabaseSync {
//
//    typealias DatabaseItem = InAppNotificationItem.StorageDataObject
//
//    private static let container: ModelContainer = {
//        let schema = Schema([
//            InAppNotificationItem.StorageDataObject.self,
//        ])
//        let modelConfiguration = ModelConfiguration(schema: schema, isStoredInMemoryOnly: false)
//
//        do {
//            return try ModelContainer(for: schema, configurations: [modelConfiguration])
//        } catch {
//            fatalError("Could not create ModelContainer: \(error)")
//        }
//    }()
//
//    func updateNotifications(_ items: [InAppNotificationItem]) throws {
//        let context = ModelContext(Self.container)
//        for item in items {
//            let existingItem = try fetchNotificationById(id: item.id)
//            if let existingItem = existingItem {
//                existingItem.updateFromNewValue(item)
//            } else {
//                let newItem = item.asSourceData
//                context.insert(newItem)
//            }
//        }
//        try context.save()
//    }
//
//    func fetchNotificationById(id: String) throws -> DatabaseItem? {
//        let context = ModelContext(Self.container)
//        let predicate = #Predicate<DatabaseItem> { $0.id == id }
//        let descriptor = FetchDescriptor<DatabaseItem>(predicate: predicate)
//        return try context.fetch(descriptor).first
//    }
//
//    func fetchNotifications() throws -> [DatabaseItem] {
//        do {
//            let context = ModelContext(Self.container)
//            let descriptor = FetchDescriptor<DatabaseItem>(sortBy: [.init(\.createdAt, order: .reverse)])
//            return try context.fetch(descriptor)
//        } catch {
//            return []
//        }
//    }
//
//    func insertNotification(_ item: DatabaseItem) throws {
//        let context = ModelContext(Self.container)
//        context.insert(item)
//        try context.save()
//    }
//
//    func deleteNotificationById(id: String) throws {
//        let context = ModelContext(Self.container)
//        let predicate = #Predicate<DatabaseItem> { $0.id == id }
//        try context.delete(model: DatabaseItem.self, where: predicate)
//    }
//
//    func deleteAllNotifications() throws {
//        let context = ModelContext(Self.container)
//        try context.delete(model: DatabaseItem.self, includeSubclasses: true)
//    }
//
//    func save() throws {
//        let context = ModelContext(Self.container)
//        try context.save()
//    }
// }
