import CodexBarCore
import Foundation

struct LLMProxyProviderImplementation: ProviderImplementation {
    let id: UsageProvider = .llmproxy

    @MainActor
    func presentation(context _: ProviderPresentationContext) -> ProviderPresentation {
        ProviderPresentation { _ in "api" }
    }

    @MainActor
    func observeSettings(_ settings: SettingsStore) {
        _ = settings[providerConfig: .llmproxy, field: .apiKey]
        _ = settings[providerConfig: .llmproxy, field: .endpoint]
    }

    @MainActor
    func isAvailable(context: ProviderAvailabilityContext) -> Bool {
        ProviderTokenResolver.token(for: .llmproxy, environment: context.environment) != nil &&
            LLMProxySettingsReader.hasBaseURLOverride(environment: context.environment)
    }

    @MainActor
    func settingsFields(context: ProviderSettingsContext) -> [ProviderSettingsFieldDescriptor] {
        [
            ProviderSettingsFieldDescriptor(
                id: "llmproxy-api-key",
                title: "API key",
                subtitle: "Stored in ~/.codexbar/config.json. Used for /v1/quota-stats.",
                kind: .secure,
                placeholder: "proxy key…",
                binding: context.providerConfigBinding(.apiKey),
                actions: [],
                isVisible: nil,
                onActivate: nil),
            ProviderSettingsFieldDescriptor(
                id: "llmproxy-base-url",
                title: "Base URL",
                subtitle: "Base URL for the LLM-API-Key-Proxy instance.",
                kind: .plain,
                placeholder: "https://proxy.example.com",
                binding: context.providerConfigBinding(.endpoint),
                actions: [],
                isVisible: nil,
                onActivate: nil),
        ]
    }
}
