package legal

import (
	"errors"
	"strings"
	"testing"

	"github.com/niels/banking-app/backend/internal/domain"
)

func TestNormalizeControlParamsRequiresProviderContractReferenceForSignedContracts(t *testing.T) {
	params := ControlParams{
		ControlKey:       " Provider-Contract-Card ",
		ControlType:      TypeProviderContract,
		Jurisdiction:     "global",
		ProviderCategory: "card_issuer_processor",
		ProviderName:     "Processor Sandbox",
		Status:           StatusSigned,
		DecisionSummary:  "Contract approved by operations.",
	}

	err := normalizeControlParams(&params)
	if err == nil {
		t.Fatal("expected signed provider contract without contract reference to fail")
	}
	if !errors.Is(err, domain.ErrValidation) || !strings.Contains(err.Error(), "contract_reference") {
		t.Fatalf("unexpected error: %v", err)
	}
}

func TestNormalizeControlParamsRequiresDisclosuresForRestrictedTerms(t *testing.T) {
	params := ControlParams{
		ControlKey:      "product-term-savings",
		ControlType:     TypeProductTermApproval,
		Jurisdiction:    "NL",
		ProductScope:    "savings",
		Status:          StatusRequiresDisclaimer,
		DecisionSummary: "Savings wording requires a disclaimer.",
	}

	err := normalizeControlParams(&params)
	if err == nil {
		t.Fatal("expected restricted term without disclosures to fail")
	}
	if !errors.Is(err, domain.ErrValidation) || !strings.Contains(err.Error(), "required_disclosures") {
		t.Fatalf("unexpected error: %v", err)
	}
}

func TestNormalizeControlParamsRequiresEvidenceForPublishedPolicies(t *testing.T) {
	params := ControlParams{
		ControlKey:      "policy-terms",
		ControlType:     TypePolicyDocument,
		Jurisdiction:    "GLOBAL",
		ProductScope:    "terms_of_service",
		Status:          StatusPublished,
		PolicyVersion:   "v1.0",
		DecisionSummary: "Terms are approved.",
	}

	err := normalizeControlParams(&params)
	if err == nil {
		t.Fatal("expected published policy without evidence to fail")
	}
	if !errors.Is(err, domain.ErrValidation) || !strings.Contains(err.Error(), "evidence_reference") {
		t.Fatalf("unexpected error: %v", err)
	}
}

func TestNormalizeControlParamsDefaultsAndNormalizesLegalControls(t *testing.T) {
	params := ControlParams{
		ControlKey:          " Product-Term-Bank ",
		ControlType:         TypeProductTermApproval,
		Jurisdiction:        "nl",
		ProductScope:        " Bank ",
		Status:              StatusRequiresDisclaimer,
		DecisionSummary:     "Bank wording requires legal approval.",
		RequiredDisclosures: []string{"Do not imply licensed bank status."},
	}

	if err := normalizeControlParams(&params); err != nil {
		t.Fatalf("expected valid product term approval: %v", err)
	}
	if params.ControlKey != "product-term-bank" {
		t.Fatalf("control key not normalized: %q", params.ControlKey)
	}
	if params.Jurisdiction != "NL" {
		t.Fatalf("jurisdiction not normalized: %q", params.Jurisdiction)
	}
	if params.ProductScope != "bank" {
		t.Fatalf("product scope not normalized: %q", params.ProductScope)
	}
	if params.RiskLevel != "high" || params.OwnerTeam != "legal" {
		t.Fatalf("defaults not applied: risk=%q owner=%q", params.RiskLevel, params.OwnerTeam)
	}
}
