import Foundation
import SaplingGit
import SaplingLogging
import SaplingStorage
import SaplingWorkspace

struct AppDependencies {
    let gitProvider: any GitProvider
    let configurationStore: any ConfigurationStore
    let metadataStore: any WorkspaceMetadataStore
    let workspaceManager: any WorkspaceManaging
    let logger: SaplingLogger

    static func live() -> AppDependencies {
        let gitProvider = MockGitProvider()
        let metadataStore = InMemoryWorkspaceMetadataStore()
        let configurationStore = JSONConfigurationStore(
            fileURL: supportDirectory().appendingPathComponent("Configuration.json")
        )
        let workspaceManager = LocalWorkspaceManager(
            gitProvider: gitProvider,
            metadataStore: metadataStore
        )

        return AppDependencies(
            gitProvider: gitProvider,
            configurationStore: configurationStore,
            metadataStore: metadataStore,
            workspaceManager: workspaceManager,
            logger: SaplingLogger()
        )
    }

    static func preview() -> AppDependencies {
        let gitProvider = MockGitProvider()
        let metadataStore = InMemoryWorkspaceMetadataStore()
        let configurationStore = InMemoryConfigurationStore()
        let workspaceManager = LocalWorkspaceManager(
            gitProvider: gitProvider,
            metadataStore: metadataStore
        )

        return AppDependencies(
            gitProvider: gitProvider,
            configurationStore: configurationStore,
            metadataStore: metadataStore,
            workspaceManager: workspaceManager,
            logger: SaplingLogger(subsystem: "app.sapling.Sapling.preview")
        )
    }

    private static func supportDirectory() -> URL {
        let baseURL = FileManager.default.urls(
            for: .applicationSupportDirectory,
            in: .userDomainMask
        ).first ?? FileManager.default.temporaryDirectory

        return baseURL.appendingPathComponent("Sapling", isDirectory: true)
    }
}
