import Foundation
import SwiftData

@MainActor
public final class ConnectionStore {
    private let context: ModelContext

    public init(context: ModelContext) {
        self.context = context
    }

    public func all() throws -> [SavedConnection] {
        let descriptor = FetchDescriptor<SavedConnection>(
            sortBy: [SortDescriptor(\.displayName, order: .forward)]
        )
        return try context.fetch(descriptor)
    }

    public func insert(_ connection: SavedConnection) {
        context.insert(connection)
    }

    public func delete(_ connection: SavedConnection) {
        context.delete(connection)
    }

    public func save() throws {
        try context.save()
    }
}
