package task

import (
	"context"
	"testing"

	"github.com/break/junhong_cmp_fiber/internal/model"
	"github.com/break/junhong_cmp_fiber/internal/store/postgres"
	"github.com/break/junhong_cmp_fiber/pkg/constants"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	"go.uber.org/zap"
)

func TestIotCardImportHandler_ProcessImport(t *testing.T) {
	tx := newTaskTestTransaction(t)
	rdb := getTaskTestRedis(t)
	cleanTaskTestRedisKeys(t, rdb)

	logger := zap.NewNop()
	importTaskStore := postgres.NewIotCardImportTaskStore(tx, rdb)
	iotCardStore := postgres.NewIotCardStore(tx, rdb)

	handler := NewIotCardImportHandler(tx, rdb, importTaskStore, iotCardStore, nil, logger)
	ctx := context.Background()

	t.Run("成功导入新ICCID", func(t *testing.T) {
		task := &model.IotCardImportTask{
			CarrierID:   1,
			CarrierType: constants.CarrierCodeCMCC,
			BatchNo:     "TEST_BATCH_001",
			CardList: model.CardListJSON{
				{ICCID: "89860012345678905001", MSISDN: "13800000001"},
				{ICCID: "89860012345678905002", MSISDN: "13800000002"},
				{ICCID: "89860012345678905003", MSISDN: "13800000003"},
			},
			TotalCount: 3,
		}
		task.Creator = 1

		result := handler.processImport(ctx, task)

		assert.Equal(t, 3, result.successCount)
		assert.Equal(t, 0, result.skipCount)
		assert.Equal(t, 0, result.failCount)

		exists, _ := iotCardStore.ExistsByICCID(ctx, "89860012345678905001")
		assert.True(t, exists)

		card, _ := iotCardStore.GetByICCID(ctx, "89860012345678905001")
		assert.Equal(t, "13800000001", card.MSISDN)
	})

	t.Run("跳过已存在的ICCID", func(t *testing.T) {
		existingCard := &model.IotCard{
			ICCID:     "89860012345678906001",
			CardType:  "data_card",
			CarrierID: 1,
			Status:    1,
		}
		require.NoError(t, iotCardStore.Create(ctx, existingCard))

		task := &model.IotCardImportTask{
			CarrierID:   1,
			CarrierType: constants.CarrierCodeCMCC,
			BatchNo:     "TEST_BATCH_002",
			CardList: model.CardListJSON{
				{ICCID: "89860012345678906001", MSISDN: "13800000011"},
				{ICCID: "89860012345678906002", MSISDN: "13800000012"},
			},
			TotalCount: 2,
		}
		task.Creator = 1

		result := handler.processImport(ctx, task)

		assert.Equal(t, 1, result.successCount)
		assert.Equal(t, 1, result.skipCount)
		assert.Equal(t, 0, result.failCount)
		assert.Len(t, result.skippedItems, 1)
		assert.Equal(t, "89860012345678906001", result.skippedItems[0].ICCID)
		assert.Equal(t, "13800000011", result.skippedItems[0].MSISDN)
		assert.Equal(t, "ICCID 已存在", result.skippedItems[0].Reason)
	})

	t.Run("ICCID格式校验失败", func(t *testing.T) {
		task := &model.IotCardImportTask{
			CarrierID:   1,
			CarrierType: constants.CarrierCodeCTCC,
			BatchNo:     "TEST_BATCH_003",
			CardList: model.CardListJSON{
				{ICCID: "89860312345678907001", MSISDN: "13900000001"},
				{ICCID: "898603123456789070", MSISDN: "13900000002"},
			},
			TotalCount: 2,
		}
		task.Creator = 1

		result := handler.processImport(ctx, task)

		assert.Equal(t, 0, result.successCount)
		assert.Equal(t, 0, result.skipCount)
		assert.Equal(t, 2, result.failCount)
		assert.Len(t, result.failedItems, 2)
		assert.Equal(t, "13900000001", result.failedItems[0].MSISDN)
	})

	t.Run("混合场景-成功跳过和失败", func(t *testing.T) {
		existingCard := &model.IotCard{
			ICCID:     "89860012345678908001",
			CardType:  "data_card",
			CarrierID: 1,
			Status:    1,
		}
		require.NoError(t, iotCardStore.Create(ctx, existingCard))

		task := &model.IotCardImportTask{
			CarrierID:   1,
			CarrierType: constants.CarrierCodeCMCC,
			BatchNo:     "TEST_BATCH_004",
			CardList: model.CardListJSON{
				{ICCID: "89860012345678908001", MSISDN: "13800000021"},
				{ICCID: "89860012345678908002", MSISDN: "13800000022"},
				{ICCID: "invalid!iccid", MSISDN: "13800000023"},
			},
			TotalCount: 3,
		}
		task.Creator = 1

		result := handler.processImport(ctx, task)

		assert.Equal(t, 1, result.successCount)
		assert.Equal(t, 1, result.skipCount)
		assert.Equal(t, 1, result.failCount)
	})

	t.Run("空卡列表", func(t *testing.T) {
		task := &model.IotCardImportTask{
			CarrierID:   1,
			CarrierType: constants.CarrierCodeCMCC,
			BatchNo:     "TEST_BATCH_005",
			CardList:    model.CardListJSON{},
			TotalCount:  0,
		}

		result := handler.processImport(ctx, task)

		assert.Equal(t, 0, result.successCount)
		assert.Equal(t, 0, result.skipCount)
		assert.Equal(t, 0, result.failCount)
	})
}

func TestIotCardImportHandler_ProcessBatch(t *testing.T) {
	tx := newTaskTestTransaction(t)
	rdb := getTaskTestRedis(t)
	cleanTaskTestRedisKeys(t, rdb)

	logger := zap.NewNop()
	importTaskStore := postgres.NewIotCardImportTaskStore(tx, rdb)
	iotCardStore := postgres.NewIotCardStore(tx, rdb)

	handler := NewIotCardImportHandler(tx, rdb, importTaskStore, iotCardStore, nil, logger)
	ctx := context.Background()

	t.Run("验证行号和MSISDN正确记录", func(t *testing.T) {
		existingCard := &model.IotCard{
			ICCID:     "89860012345678909002",
			CardType:  "data_card",
			CarrierID: 1,
			Status:    1,
		}
		require.NoError(t, iotCardStore.Create(ctx, existingCard))

		task := &model.IotCardImportTask{
			CarrierID:   1,
			CarrierType: constants.CarrierCodeCMCC,
			BatchNo:     "TEST_BATCH_LINE",
		}
		task.Creator = 1

		batch := []model.CardItem{
			{ICCID: "89860012345678909001", MSISDN: "13800000031"},
			{ICCID: "89860012345678909002", MSISDN: "13800000032"},
			{ICCID: "invalid", MSISDN: "13800000033"},
		}
		result := &importResult{
			skippedItems: make(model.ImportResultItems, 0),
			failedItems:  make(model.ImportResultItems, 0),
		}

		handler.processBatch(ctx, task, batch, 100, result)

		assert.Equal(t, 1, result.successCount)
		assert.Equal(t, 1, result.skipCount)
		assert.Equal(t, 1, result.failCount)

		assert.Equal(t, 101, result.skippedItems[0].Line)
		assert.Equal(t, "13800000032", result.skippedItems[0].MSISDN)
		assert.Equal(t, 102, result.failedItems[0].Line)
		assert.Equal(t, "13800000033", result.failedItems[0].MSISDN)
	})
}
