package unit

import (
	"context"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"

	"github.com/break/junhong_cmp_fiber/internal/model"
	"github.com/break/junhong_cmp_fiber/internal/model/dto"
	"github.com/break/junhong_cmp_fiber/internal/service/shop_commission"
	"github.com/break/junhong_cmp_fiber/internal/store/postgres"
	"github.com/break/junhong_cmp_fiber/pkg/constants"
	"github.com/break/junhong_cmp_fiber/tests/testutils"
)

func createCommissionTestContext(userID uint) context.Context {
	ctx := context.Background()
	ctx = context.WithValue(ctx, constants.ContextKeyUserID, userID)
	ctx = context.WithValue(ctx, constants.ContextKeyUserType, constants.UserTypePlatform)
	return ctx
}

func TestShopCommissionService_ListShopCommissionSummary(t *testing.T) {
	tx := testutils.NewTestTransaction(t)
	rdb := testutils.GetTestRedis(t)
	testutils.CleanTestRedisKeys(t, rdb)

	shopStore := postgres.NewShopStore(tx, rdb)
	accountStore := postgres.NewAccountStore(tx, rdb)
	walletStore := postgres.NewWalletStore(tx, rdb)
	commissionWithdrawalRequestStore := postgres.NewCommissionWithdrawalRequestStore(tx, rdb)
	commissionRecordStore := postgres.NewCommissionRecordStore(tx, rdb)

	service := shop_commission.New(shopStore, accountStore, walletStore, commissionWithdrawalRequestStore, commissionRecordStore)

	t.Run("查询店铺佣金汇总列表", func(t *testing.T) {
		ctx := createCommissionTestContext(1)

		shop := &model.Shop{
			ShopName:     "测试店铺",
			ShopCode:     "COMMISSION_TEST_001",
			Level:        1,
			ContactName:  "测试联系人",
			ContactPhone: "13800000001",
			Status:       constants.StatusEnabled,
			BaseModel: model.BaseModel{
				Creator: 1,
				Updater: 1,
			},
		}
		err := shopStore.Create(ctx, shop)
		require.NoError(t, err)

		req := &dto.ShopCommissionSummaryListReq{
			Page:     1,
			PageSize: 20,
		}

		result, err := service.ListShopCommissionSummary(ctx, req)
		require.NoError(t, err)
		assert.NotNil(t, result)
		assert.GreaterOrEqual(t, result.Total, int64(0))
	})

	t.Run("按店铺名称筛选", func(t *testing.T) {
		ctx := createCommissionTestContext(1)

		shop := &model.Shop{
			ShopName:     "筛选测试店铺",
			ShopCode:     "FILTER_TEST_001",
			Level:        1,
			ContactName:  "测试联系人",
			ContactPhone: "13800000002",
			Status:       constants.StatusEnabled,
			BaseModel: model.BaseModel{
				Creator: 1,
				Updater: 1,
			},
		}
		err := shopStore.Create(ctx, shop)
		require.NoError(t, err)

		req := &dto.ShopCommissionSummaryListReq{
			Page:     1,
			PageSize: 20,
			ShopName: "筛选测试",
		}

		result, err := service.ListShopCommissionSummary(ctx, req)
		require.NoError(t, err)
		assert.NotNil(t, result)
	})
}

func TestShopCommissionService_ListShopWithdrawalRequests(t *testing.T) {
	tx := testutils.NewTestTransaction(t)
	rdb := testutils.GetTestRedis(t)
	testutils.CleanTestRedisKeys(t, rdb)

	shopStore := postgres.NewShopStore(tx, rdb)
	accountStore := postgres.NewAccountStore(tx, rdb)
	walletStore := postgres.NewWalletStore(tx, rdb)
	commissionWithdrawalRequestStore := postgres.NewCommissionWithdrawalRequestStore(tx, rdb)
	commissionRecordStore := postgres.NewCommissionRecordStore(tx, rdb)

	service := shop_commission.New(shopStore, accountStore, walletStore, commissionWithdrawalRequestStore, commissionRecordStore)

	t.Run("查询店铺提现记录", func(t *testing.T) {
		ctx := createCommissionTestContext(1)

		shop := &model.Shop{
			ShopName:     "提现测试店铺",
			ShopCode:     "WITHDRAWAL_TEST_001",
			Level:        1,
			ContactName:  "测试联系人",
			ContactPhone: "13800000003",
			Status:       constants.StatusEnabled,
			BaseModel: model.BaseModel{
				Creator: 1,
				Updater: 1,
			},
		}
		err := shopStore.Create(ctx, shop)
		require.NoError(t, err)

		req := &dto.ShopWithdrawalRequestListReq{
			Page:     1,
			PageSize: 20,
		}

		result, err := service.ListShopWithdrawalRequests(ctx, shop.ID, req)
		require.NoError(t, err)
		assert.NotNil(t, result)
		assert.GreaterOrEqual(t, result.Total, int64(0))
	})

	t.Run("查询不存在的店铺提现记录应失败", func(t *testing.T) {
		ctx := createCommissionTestContext(1)

		req := &dto.ShopWithdrawalRequestListReq{
			Page:     1,
			PageSize: 20,
		}

		_, err := service.ListShopWithdrawalRequests(ctx, 99999, req)
		assert.Error(t, err)
	})
}

func TestShopCommissionService_ListShopCommissionRecords(t *testing.T) {
	tx := testutils.NewTestTransaction(t)
	rdb := testutils.GetTestRedis(t)
	testutils.CleanTestRedisKeys(t, rdb)

	shopStore := postgres.NewShopStore(tx, rdb)
	accountStore := postgres.NewAccountStore(tx, rdb)
	walletStore := postgres.NewWalletStore(tx, rdb)
	commissionWithdrawalRequestStore := postgres.NewCommissionWithdrawalRequestStore(tx, rdb)
	commissionRecordStore := postgres.NewCommissionRecordStore(tx, rdb)

	service := shop_commission.New(shopStore, accountStore, walletStore, commissionWithdrawalRequestStore, commissionRecordStore)

	t.Run("查询店铺佣金明细", func(t *testing.T) {
		ctx := createCommissionTestContext(1)

		shop := &model.Shop{
			ShopName:     "佣金明细测试店铺",
			ShopCode:     "RECORD_TEST_001",
			Level:        1,
			ContactName:  "测试联系人",
			ContactPhone: "13800000004",
			Status:       constants.StatusEnabled,
			BaseModel: model.BaseModel{
				Creator: 1,
				Updater: 1,
			},
		}
		err := shopStore.Create(ctx, shop)
		require.NoError(t, err)

		req := &dto.ShopCommissionRecordListReq{
			Page:     1,
			PageSize: 20,
		}

		result, err := service.ListShopCommissionRecords(ctx, shop.ID, req)
		require.NoError(t, err)
		assert.NotNil(t, result)
		assert.GreaterOrEqual(t, result.Total, int64(0))
	})

	t.Run("查询不存在的店铺佣金明细应失败", func(t *testing.T) {
		ctx := createCommissionTestContext(1)

		req := &dto.ShopCommissionRecordListReq{
			Page:     1,
			PageSize: 20,
		}

		_, err := service.ListShopCommissionRecords(ctx, 99999, req)
		assert.Error(t, err)
	})
}

func TestBuildShopHierarchyPath(t *testing.T) {
	t.Run("一级店铺路径", func(t *testing.T) {
		shop := &model.Shop{
			ShopName: "一级店铺",
			Level:    1,
			ParentID: nil,
		}
		path := buildTestHierarchyPath(shop, nil)
		assert.Equal(t, "一级店铺", path)
	})

	t.Run("多级店铺路径", func(t *testing.T) {
		parentID := uint(1)
		shop := &model.Shop{
			ShopName: "二级店铺",
			Level:    2,
			ParentID: &parentID,
		}
		parent := &model.Shop{
			ShopName: "一级店铺",
			Level:    1,
			ParentID: nil,
		}
		path := buildTestHierarchyPath(shop, parent)
		assert.Equal(t, "一级店铺 > 二级店铺", path)
	})
}

func buildTestHierarchyPath(shop *model.Shop, parent *model.Shop) string {
	if parent == nil {
		return shop.ShopName
	}
	return parent.ShopName + " > " + shop.ShopName
}
