//
// 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 SwiftUI

/// Single-instance Realtime voice configuration.
///
/// OpenAI Realtime is the sole voice entry-point, so the Settings surface is
/// a single config card rather than a multi-instance list with an Add button.
/// The underlying store still supports multiple instances for future-proofing,
/// but the UI is intentionally collapsed to one.
struct OpenRockyRealtimeProviderInstanceListView: View {
    @ObservedObject var realtimeProviderStore: OpenRockyRealtimeProviderStore

    var body: some View {
        OpenRockyRealtimeProviderInstanceEditorView(
            realtimeProviderStore: realtimeProviderStore
        )
        .onAppear { ensureActiveInstance() }
    }

    /// Make sure the store has exactly one active instance with sensible OpenAI defaults.
    /// Called on every appear so users dropping into Settings without a configured
    /// provider see an editable card immediately rather than an empty form bound to nothing.
    private func ensureActiveInstance() {
        if realtimeProviderStore.activeInstance != nil { return }
        let instance = OpenRockyRealtimeProviderInstance(
            id: UUID().uuidString,
            name: OpenRockyRealtimeProviderKind.openAI.displayName,
            kind: .openAI,
            modelID: OpenRockyRealtimeProviderKind.openAI.defaultModel,
            isBuiltIn: false
        )
        realtimeProviderStore.add(instance, credential: nil)
        realtimeProviderStore.setActive(id: instance.id)
    }
}
