//
// OpenRocky — Voice-first AI Agent
// https://github.com/openrocky
//
// Developed by everettjf with the assistance of Claude Code and Codex.
// Date: 2026-04-14
// Copyright (c) 2026 everettjf. All rights reserved.
//

import Foundation

/// Finds reusable API credentials across all provider stores for a given provider family.
/// For example, if the user has an OpenAI Chat key configured, adding OpenAI STT should
/// offer to reuse that same key instead of requiring manual re-entry.
@MainActor
struct OpenRockyCredentialReuse {
    struct ReusableCredential {
        let credential: String
        let sourceName: String   // e.g. "OpenAI (Chat)"
        let maskedCredential: String
    }

    private static func mask(_ credential: String) -> String {
        guard credential.count > 8 else { return "****" }
        let prefix = credential.prefix(4)
        let suffix = credential.suffix(4)
        return "\(prefix)****\(suffix)"
    }
}

// MARK: - Credential Family Mapping

/// Providers that share the same API platform and can reuse each other's credentials.
/// Only families with more than one kind mapped to them are useful — the rest fall
/// through to `.other`, which disables cross-section reuse.
enum OpenRockyCredentialFamily: String {
    case openAI
    case zhipuAI
    case other
}

extension OpenRockyProviderKind {
    var credentialFamily: OpenRockyCredentialFamily {
        switch self {
        case .openAI, .aiProxy: .openAI
        case .zhipuAI: .zhipuAI
        case .groq, .azureOpenAI, .gemini, .volcengine,
             .deepSeek, .anthropic, .xAI, .openRouter, .bailian, .appleFoundationModels:
            .other
        }
    }
}

extension OpenRockyRealtimeProviderKind {
    var credentialFamily: OpenRockyCredentialFamily {
        switch self {
        case .openAI: .openAI
        }
    }
}
