import Foundation
import Testing

@testable import NucleicCore

@Suite struct ExternalAgentIntegrationSettingsTests {
    @Test func modelSelectionsRoundTrip() {
        let selected: Set<String> = ["claude-sonnet-5", "gpt-5.4"]
        let encoded = ExternalAgentIntegrationSettings.encodedModels(selected)
        #expect(ExternalAgentIntegrationSettings.models(from: encoded) == selected)
        #expect(ExternalAgentIntegrationSettings.models(from: "*") == ["*"])
        #expect(ExternalAgentIntegrationSettings.models(from: "not json").isEmpty)
    }

    @Test func defaultsKeepSkillsButRequireOptInForMCPAndPlugins() {
        let defaults = isolatedDefaults()

        #expect(
            ExternalAgentIntegrationSettings.allows(
                .skills, model: "claude-sonnet-5", defaults: defaults))
        #expect(
            ExternalAgentIntegrationSettings.allows(
                .plugins, model: "gpt-5.4", defaults: defaults) == false)
        #expect(
            ExternalAgentIntegrationSettings.allows(
                .mcp, model: "claude-sonnet-5", defaults: defaults) == false)
    }

    @Test func eachIntegrationCanTargetSeveralModels() {
        let defaults = isolatedDefaults()
        let selected: Set<String> = ["claude-sonnet-5", "gpt-5.4"]
        defaults.set(
            ExternalAgentIntegrationSettings.encodedModels(selected),
            forKey: ExternalAgentIntegration.mcp.defaultsKey)

        #expect(ExternalAgentIntegrationSettings.allows(.mcp, model: "gpt-5.4", defaults: defaults))
        #expect(ExternalAgentIntegrationSettings.allows(.mcp, model: "claude-sonnet-5", defaults: defaults))
        #expect(ExternalAgentIntegrationSettings.allows(.mcp, model: "grok-build", defaults: defaults) == false)
    }

    private func isolatedDefaults() -> UserDefaults {
        UserDefaults(suiteName: "ExternalAgentIntegrationSettingsTests.\(UUID().uuidString)")!
    }
}
