import Foundation
import SwiftData

public enum SharedStore {
    public static let appGroupID = "group.com.conjuga.app"

    /// Resolves the local SwiftData store URL inside the shared app group container
    /// at the canonical `Library/Application Support/local.store` path.
    /// Returns nil if the app group isn't accessible (entitlement / profile issue).
    public static func localStoreURL() -> URL? {
        guard let groupURL = FileManager.default.containerURL(
            forSecurityApplicationGroupIdentifier: appGroupID
        ) else {
            return nil
        }
        let dir = groupURL.appendingPathComponent("Library/Application Support")
        try? FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true)
        return dir.appendingPathComponent("local.store")
    }

    /// Global reference to the main app's local reference-data container.
    /// Set by `ConjugaApp.init()` so any view can bypass `@Environment(\.modelContext)`
    /// and hit the exact container used for seeding.
    @MainActor
    public static var localContainer: ModelContainer?
}
