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

import Foundation

struct OpenRockyRealtimeProviderInstance: Codable, Identifiable, Equatable {
    let id: String
    var name: String
    var kind: OpenRockyRealtimeProviderKind
    var modelID: String
    var openaiVoice: String?
    var customHost: String?
    var isBuiltIn: Bool
    /// Optional advanced tuning. Nil means "use defaults" — older persisted instances
    /// from before this field existed will decode with nil and behave identically.
    var advancedSettings: OpenRockyRealtimeAdvancedSettings?

    var credentialKeychainKey: String {
        "rocky.realtime-instance.\(id).credential"
    }

    /// Legacy accessor. Older persisted JSON may have a `glmVoice` field; decoder ignores it silently.
    enum CodingKeys: String, CodingKey {
        case id, name, kind, modelID, openaiVoice, customHost, isBuiltIn, advancedSettings
    }

    /// Effective settings, falling back to defaults when the user hasn't touched advanced.
    var effectiveAdvancedSettings: OpenRockyRealtimeAdvancedSettings {
        advancedSettings ?? .default
    }

    func toConfiguration(credential: String?) -> OpenRockyRealtimeProviderConfiguration {
        OpenRockyRealtimeProviderConfiguration(
            provider: kind,
            modelID: effectiveAdvancedSettings.realtimeModel,
            credential: credential,
            openaiVoice: openaiVoice,
            customHost: customHost,
            advancedSettings: advancedSettings
        )
    }
}
