import Foundation

/// Zentrale Auflösung der system-binaries die der Binder anstößt.
enum Paths {
    /// libimobiledevice — meist via Homebrew installiert.
    static let ideviceinfoCandidates = [
        "/opt/homebrew/bin/ideviceinfo",
        "/usr/local/bin/ideviceinfo",
    ]

    /// Apple Configurator (heißt seit 2026 ohne „2") liefert cfgutil mit.
    static let cfgutilCandidates = [
        "/Applications/Apple Configurator.app/Contents/MacOS/cfgutil",
        "/Applications/Apple Configurator 2.app/Contents/MacOS/cfgutil",
    ]

    static var cfgutilPath: String? {
        firstExecutable(in: cfgutilCandidates)
    }

    /// supervise-magic Go-binary aus dem Monorepo. Pfad relativ zur App-Location:
    /// app läuft typischerweise aus DerivedData/Build/Products oder ähnlich, daher
    /// suchen wir mehrere plausible Locations relativ + absolut.
    static let superviseMagicCandidates: [String] = {
        let env = ProcessInfo.processInfo.environment["REBREAK_SUPERVISE_MAGIC_BIN"]
        var candidates: [String] = []
        if let env, !env.isEmpty { candidates.append(env) }
        // Repo-Layout: rebreak-monorepo/apps/rebreak-binder-mac/...
        // supervise-magic liegt in rebreak-monorepo/ops/mdm/supervise-magic/bin/
        let home = FileManager.default.homeDirectoryForCurrentUser.path
        candidates.append("\(home)/mono/rebreak-monorepo/ops/mdm/supervise-magic/bin/rebreak-supervise-magic")
        candidates.append("/usr/local/bin/rebreak-supervise-magic")
        candidates.append("/opt/homebrew/bin/rebreak-supervise-magic")
        return candidates
    }()

    static func firstExecutable(in candidates: [String]) -> String? {
        candidates.first(where: { FileManager.default.isExecutableFile(atPath: $0) })
    }
}
