package commerce

import (
	"strings"
	"testing"
)

func TestValidateCartCreate(t *testing.T) {
	err := validateCartCreate(CartItemCreateRequest{
		ProductID: 1,
		Cycle:     "monthly",
		Quantity:  1,
		UnitPrice: 9.9,
	})
	if err != nil {
		t.Fatalf("expected valid cart create request: %v", err)
	}
}

func TestValidateCartCreateRejectsInvalidQuantity(t *testing.T) {
	err := validateCartCreate(CartItemCreateRequest{
		ProductID: 1,
		Cycle:     "monthly",
		Quantity:  0,
		UnitPrice: 9.9,
	})
	if err == nil {
		t.Fatal("expected invalid quantity error")
	}
}

func TestEnsureProductCanBeAddedTrackedStock(t *testing.T) {
	service := &Service{}
	product := Product{Name: "small-vps", Status: "active", StockPolicy: "tracked", Stock: 2}
	err := service.ensureProductCanBeAdded(product, 2, 1)
	if err == nil {
		t.Fatal("expected stock conflict")
	}
	if !strings.Contains(err.Error(), "库存不足") {
		t.Fatalf("unexpected error: %v", err)
	}
}

func TestEnsureProductCanBeAddedUnlimitedStock(t *testing.T) {
	service := &Service{}
	product := Product{Name: "small-vps", Status: "active", StockPolicy: "unlimited"}
	if err := service.ensureProductCanBeAdded(product, 999, 0); err != nil {
		t.Fatalf("expected unlimited stock to pass: %v", err)
	}
}
