package config_test

import (
	"os"
	"path/filepath"
	"strings"
	"testing"

	"iop/packages/go/config"
)

func TestLegacyProviderCLIRejected(t *testing.T) {
	tests := map[string]string{
		"adapter block": "    adapters:\n      cli:\n        enabled: true\n",
		"provider type": "    providers:\n      - id: old\n        type: cli\n        category: api\n        models: [m]\n",
		"process field": "    providers:\n      - id: old\n        type: ollama\n        category: local_inference\n        models: [m]\n        command: tool\n",
		"process args":  "    providers:\n      - id: old\n        type: ollama\n        category: local_inference\n        models: [m]\n        args: [--api-key=redacted-fixture]\n",
		"process env":   "    providers:\n      - id: old\n        type: ollama\n        category: local_inference\n        models: [m]\n        env: [PROVIDER_API_KEY=redacted-fixture]\n",
	}
	for name, legacy := range tests {
		t.Run(name, func(t *testing.T) {
			dir := t.TempDir()
			path := filepath.Join(dir, "edge.yaml")
			yaml := "long_context_threshold_tokens: 1\nnodes:\n  - id: n1\n    alias: n1\n    token: token\n" + legacy
			if err := os.WriteFile(path, []byte(yaml), 0o600); err != nil {
				t.Fatal(err)
			}
			_, err := config.LoadEdge(path)
			if err == nil || !strings.Contains(err.Error(), "legacy provider configuration") {
				t.Fatalf("LoadEdge() error = %v, want explicit legacy rejection", err)
			}
		})
	}
}

func TestLegacyConsoleAgentRejected(t *testing.T) {
	dir := t.TempDir()
	path := filepath.Join(dir, "edge.yaml")
	yaml := "long_context_threshold_tokens: 1\nconsole:\n  agent: removed-target\n"
	if err := os.WriteFile(path, []byte(yaml), 0o600); err != nil {
		t.Fatal(err)
	}

	_, err := config.LoadEdge(path)
	if err == nil {
		t.Fatal("LoadEdge() error = nil, want legacy console.agent rejection")
	}
	if !strings.Contains(err.Error(), "legacy provider configuration") || !strings.Contains(err.Error(), "console.agent") {
		t.Fatalf("LoadEdge() error = %q, want legacy provider configuration and console.agent", err)
	}
}
