package payment

import (
	"errors"
	"testing"
)

// TestServiceDependencyChecks 测试 Service 依赖检查
func TestServiceStartWithNilRepo(t *testing.T) {
	svc := &Service{repo: nil}
	_, err := svc.Start(t.Context(), 1, 100, StartPaymentRequest{}, "127.0.0.1")
	if !errors.Is(err, ErrDependencyUnavailable) {
		t.Fatalf("Start() error = %v, want ErrDependencyUnavailable", err)
	}
}

func TestServiceStartWithInvalidParams(t *testing.T) {
	svc := &Service{repo: &Repository{}}

	// 测试 userID 为 0
	_, err := svc.Start(t.Context(), 0, 100, StartPaymentRequest{}, "127.0.0.1")
	if !errors.Is(err, ErrPaymentCannotStart) {
		t.Fatalf("Start() error = %v, want ErrPaymentCannotStart", err)
	}

	// 测试 orderID 为 0
	_, err = svc.Start(t.Context(), 1, 0, StartPaymentRequest{}, "127.0.0.1")
	if !errors.Is(err, ErrPaymentCannotStart) {
		t.Fatalf("Start() error = %v, want ErrPaymentCannotStart", err)
	}
}

func TestServiceQueryWithNilRepo(t *testing.T) {
	svc := &Service{repo: nil}
	_, err := svc.Query(t.Context(), 1, 100)
	if !errors.Is(err, ErrDependencyUnavailable) {
		t.Fatalf("Query() error = %v, want ErrDependencyUnavailable", err)
	}
}

func TestServiceQueryWithInvalidParams(t *testing.T) {
	svc := &Service{repo: &Repository{}}

	_, err := svc.Query(t.Context(), 0, 100)
	if !errors.Is(err, ErrPaymentNotFound) {
		t.Fatalf("Query() error = %v, want ErrPaymentNotFound", err)
	}
}

func TestServiceStartRefundWithNilRepo(t *testing.T) {
	svc := &Service{repo: nil}
	_, err := svc.StartRefund(t.Context(), 100, 1000, "cancel_refund", "test")
	if !errors.Is(err, ErrDependencyUnavailable) {
		t.Fatalf("StartRefund() error = %v, want ErrDependencyUnavailable", err)
	}
}

func TestServiceStartRefundWithInvalidParams(t *testing.T) {
	svc := &Service{repo: &Repository{}}

	// 测试 orderID 为 0
	_, err := svc.StartRefund(t.Context(), 0, 1000, "cancel_refund", "test")
	if !errors.Is(err, ErrRefundCannotStart) {
		t.Fatalf("StartRefund() error = %v, want ErrRefundCannotStart", err)
	}

	// 测试金额为 0
	_, err = svc.StartRefund(t.Context(), 100, 0, "cancel_refund", "test")
	if !errors.Is(err, ErrRefundCannotStart) {
		t.Fatalf("StartRefund() error = %v, want ErrRefundCannotStart", err)
	}
}

// TestRefundBizTypeConstants 测试退款业务类型常量
func TestRefundBizTypesContainsExpectedValues(t *testing.T) {
	expected := []string{
		"cancel_refund",
		"admin_close_refund",
		"admin_refund",
		"checkout_refund",
		"deposit_refund",
		"rent_refund",
		"arbitration_refund",
	}

	for _, bizType := range expected {
		found := false
		for _, refundType := range refundBizTypes {
			if refundType == bizType {
				found = true
				break
			}
		}
		if !found {
			t.Fatalf("refundBizTypes missing expected type: %s", bizType)
		}
	}
}

// TestRuntimePaymentConfigMockMode 测试 mock 模式判断
func TestRuntimePaymentConfigIsMockMode(t *testing.T) {
	config := runtimePaymentConfig{Provider: "mock"}
	if !config.isMockMode() {
		t.Fatal("isMockMode() = false, want true for mock provider")
	}

	config.Provider = "lakala"
	if config.isMockMode() {
		t.Fatal("isMockMode() = true, want false for lakala provider")
	}
}

// TestChannelSourceConstants 测试渠道来源常量
func TestChannelSourceConstantsAreDefined(t *testing.T) {
	sources := []string{
		channelSourceCreate,
		channelSourceQuery,
		channelSourceNotify,
		channelSourceMock,
	}

	expected := []string{"create", "query", "notify", "mock"}

	for i, source := range sources {
		if source != expected[i] {
			t.Fatalf("channelSource[%d] = %s, want %s", i, source, expected[i])
		}
	}
}
