package main

import (
	"strings"
	"testing"

	"git.d-ma.be/mathias/tapir/internal/config"
)

// TestChatModelsReuseTheChainLocalFirst: the switcher offers the ADR-022 chain in
// order, deduped — primary, local fallback, cloud.
func TestChatModelsReuseTheChainLocalFirst(t *testing.T) {
	got := chatModels(config.Config{
		SummarizerModel:    "koala/phi4-mini",
		FallbackModel:      "iguana/gemma4-26b",
		CloudFallbackModel: "berget/mistral-small",
	})
	want := []string{"koala/phi4-mini", "iguana/gemma4-26b", "berget/mistral-small"}
	if strings.Join(got, ",") != strings.Join(want, ",") {
		t.Fatalf("chatModels = %v, want %v", got, want)
	}
}

// TestChatCloudModelAbsentWhenDisabled: the local-first / NDA lever — with the
// cloud fallback empty (TAPIR_CLOUD_FALLBACK_MODEL=""), no external model is
// offered in the switcher, so chat content never leaves the local stack (ADR-027,
// honouring ADR-022's "content stays local" guarantee).
func TestChatCloudModelAbsentWhenDisabled(t *testing.T) {
	got := chatModels(config.Config{
		SummarizerModel:    "koala/phi4-mini",
		FallbackModel:      "iguana/gemma4-26b",
		CloudFallbackModel: "",
	})
	for _, m := range got {
		if strings.HasPrefix(m, "berget/") || strings.Contains(m, "mistral") {
			t.Fatalf("cloud model %q offered though the cloud fallback is disabled", m)
		}
	}
	want := []string{"koala/phi4-mini", "iguana/gemma4-26b"}
	if strings.Join(got, ",") != strings.Join(want, ",") {
		t.Fatalf("chatModels = %v, want %v", got, want)
	}
}

// TestChatModelsDedup: a config that reuses one alias across slots collapses to a
// single switcher entry (no duplicate options).
func TestChatModelsDedup(t *testing.T) {
	got := chatModels(config.Config{
		SummarizerModel:    "koala/phi4-mini",
		FallbackModel:      "koala/phi4-mini",
		CloudFallbackModel: "",
	})
	if len(got) != 1 || got[0] != "koala/phi4-mini" {
		t.Fatalf("chatModels = %v, want a single deduped entry", got)
	}
}

// TestBuildChatNilWithoutGateway: no gateway → no chat backend (routes unmounted,
// read path unaffected).
func TestBuildChatNilWithoutGateway(t *testing.T) {
	if c := buildChat(config.Config{GatewayURL: ""}); c != nil {
		t.Fatal("buildChat must return nil without a gateway URL")
	}
}
