package usecase

import (
	"encoding/json"
	"os"
	"strings"
	"testing"
)

type accuracyCase struct {
	ID                      string   `json:"id"`
	Text                    string   `json:"text"`
	ProductTerms            []string `json:"product_terms"`
	ExpectedDemand          bool     `json:"expected_demand"`
	ExpectedProductEvidence bool     `json:"expected_product_evidence"`
	ExpectedExclude         bool     `json:"expected_exclude"`
	ExpectedStale           bool     `json:"expected_stale"`
	ExpectedUnknownTime     bool     `json:"expected_unknown_time"`
}

func loadAccuracyCases(t *testing.T) []accuracyCase {
	t.Helper()
	raw, err := os.ReadFile("testdata/opportunity_inbox_accuracy.json")
	if err != nil {
		t.Fatal(err)
	}
	var cases []accuracyCase
	if err := json.Unmarshal(raw, &cases); err != nil {
		t.Fatal(err)
	}
	return cases
}

func TestAccuracyFixtureSchema(t *testing.T) {
	cases := loadAccuracyCases(t)
	if len(cases) < 10 {
		t.Fatalf("fixture needs at least 10 cases, got %d", len(cases))
	}
	seen := map[string]bool{}
	for _, item := range cases {
		if item.ID == "" || seen[item.ID] || strings.TrimSpace(item.Text) == "" {
			t.Fatalf("invalid or duplicate case: %+v", item)
		}
		seen[item.ID] = true
		if item.ExpectedDemand && len(item.ProductTerms) == 0 {
			t.Fatalf("demand case lacks product evidence: %s", item.ID)
		}
		if item.ExpectedExclude && item.ExpectedDemand {
			t.Fatalf("exclude case cannot be demand: %s", item.ID)
		}
		if strings.Contains(strings.ToLower(item.Text), "bearer ") || strings.Contains(item.Text, "sk-") {
			t.Fatalf("fixture contains token-like text: %s", item.ID)
		}
	}
}
