package asset

import (
	"testing"

	"github.com/break/junhong_cmp_fiber/internal/model"
)

func TestComputeUsageSummary_EmptyList(t *testing.T) {
	totalUsed, totalRemaining := computeUsageSummary(nil)
	if totalUsed != 0.0 {
		t.Errorf("期望 totalUsed=0.0，实际=%v", totalUsed)
	}
	if totalRemaining != 0.0 {
		t.Errorf("期望 totalRemaining=0.0，实际=%v", totalRemaining)
	}
}

func TestComputeUsageSummary_SingleVirtualPackage(t *testing.T) {
	// 启用虚流量：真实总量=100MB，虚拟总量=200MB，倍率=0.5
	// 真实已用=40MB → 虚拟已用=min(40*0.5, 100)=20MB
	// 虚拟剩余=200-20=180MB
	usage := &model.PackageUsage{
		DataLimitMB:               100,
		DataUsageMB:               40,
		VirtualTotalMBSnapshot:    200,
		DisplayGainRatioSnapshot:  0.5,
		EnableVirtualDataSnapshot: true,
	}
	totalUsed, totalRemaining := computeUsageSummary([]*model.PackageUsage{usage})
	if totalUsed != 20.0 {
		t.Errorf("期望 totalUsed=20.0，实际=%v", totalUsed)
	}
	if totalRemaining != 180.0 {
		t.Errorf("期望 totalRemaining=180.0，实际=%v", totalRemaining)
	}
}

func TestComputeUsageSummary_SingleNonVirtualPackage(t *testing.T) {
	// 未启用虚流量：退化为真实值
	// 真实总量=100MB，真实已用=40MB → 虚拟已用=40MB，虚拟剩余=60MB
	usage := &model.PackageUsage{
		DataLimitMB:               100,
		DataUsageMB:               40,
		EnableVirtualDataSnapshot: false,
	}
	totalUsed, totalRemaining := computeUsageSummary([]*model.PackageUsage{usage})
	if totalUsed != 40.0 {
		t.Errorf("期望 totalUsed=40.0，实际=%v", totalUsed)
	}
	if totalRemaining != 60.0 {
		t.Errorf("期望 totalRemaining=60.0，实际=%v", totalRemaining)
	}
}

func TestComputeUsageSummary_MultiplePackages(t *testing.T) {
	// 套餐1（启用虚流量）：VirtualUsed=20, VirtualTotal=200
	// 套餐2（未启用虚流量）：VirtualUsed=10, VirtualTotal=50
	// 汇总：totalUsed=30, totalRemaining=(200+50)-30=220
	usages := []*model.PackageUsage{
		{
			DataLimitMB:               100,
			DataUsageMB:               40,
			VirtualTotalMBSnapshot:    200,
			DisplayGainRatioSnapshot:  0.5,
			EnableVirtualDataSnapshot: true,
		},
		{
			DataLimitMB:               50,
			DataUsageMB:               10,
			EnableVirtualDataSnapshot: false,
		},
	}
	totalUsed, totalRemaining := computeUsageSummary(usages)
	if totalUsed != 30.0 {
		t.Errorf("期望 totalUsed=30.0，实际=%v", totalUsed)
	}
	if totalRemaining != 220.0 {
		t.Errorf("期望 totalRemaining=220.0，实际=%v", totalRemaining)
	}
}
