package userapi

import (
	"net/http/httptest"
	"testing"

	walletv1 "hyapp.local/api/proto/wallet/v1"
)

func TestHostCenterPolicyFromProtoMapsPointDiamondConfig(t *testing.T) {
	policy := hostCenterPolicyFromProto(&walletv1.HostSalaryPolicy{
		PolicyId:                9002,
		PolicyType:              "POINT_DIAMOND",
		PointDiamondsPerUsd:     300000,
		CoinsPerUsd:             80000,
		AgencyPointShareBps:     2000,
		MinimumWithdrawUsdMinor: 500,
		WithdrawFeeBps:          250,
	})
	if policy == nil || policy.PolicyID != "9002" || policy.PolicyType != "POINT_DIAMOND" || policy.PointDiamondsPerUSD != 300000 || policy.CoinsPerUSD != 80000 || policy.AgencyPointShareBPS != 2000 || policy.MinimumWithdrawUSDMinor != 500 || policy.WithdrawFeeBPS != 250 {
		t.Fatalf("POINT_DIAMOND policy mapping mismatch: %+v", policy)
	}
}

func TestParseHostCenterStatsRangeUsesInclusiveNaturalDays(t *testing.T) {
	request := httptest.NewRequest("GET", "/api/v1/host-center/stats?start_date=2026-07-06&end_date=2026-07-12", nil)
	startDate, endDate, startAtMS, endAtMS, ok := parseHostCenterStatsRange(request)
	if !ok || startDate != "2026-07-06" || endDate != "2026-07-12" {
		t.Fatalf("range parse mismatch: ok=%t start=%s end=%s", ok, startDate, endDate)
	}
	if got := endAtMS - startAtMS; got != 7*24*60*60*1000 {
		t.Fatalf("end must be exclusive after seven inclusive days, got %d", got)
	}
}

func TestBDCenterAgencyHostIncomeIncludesBothPolicyProjections(t *testing.T) {
	stats := &walletv1.AgencyHostGiftStats{
		GiftIncome:             8800,
		PointDiamondHostIncome: 1200,
		// Agency 分成不能计入 BD 表格里的直属 Host 总收入。
		PointDiamondAgencyShareIncome: 300,
	}
	if got := bdCenterAgencyHostIncome(stats); got != 10000 {
		t.Fatalf("bd agency host income = %d, want 10000", got)
	}
	if got := bdCenterAgencyHostIncome(nil); got != 0 {
		t.Fatalf("nil bd agency stats income = %d, want 0", got)
	}
}

func TestParseHostCenterStatsRangeRejectsInvalidOrOversizedRanges(t *testing.T) {
	for _, rawURL := range []string{
		"/api/v1/host-center/stats?start_date=2026-07-12&end_date=2026-07-01",
		"/api/v1/host-center/stats?start_date=2026-07-12",
		"/api/v1/host-center/stats?start_date=2025-01-01&end_date=2026-07-12",
	} {
		request := httptest.NewRequest("GET", rawURL, nil)
		if _, _, _, _, ok := parseHostCenterStatsRange(request); ok {
			t.Fatalf("expected invalid range for %s", rawURL)
		}
	}
}

func TestAgencyCenterSummaryKeepsHostAndAgencyPointDiamondComponentsSeparate(t *testing.T) {
	stats := &walletv1.AgencyHostGiftStats{GiftIncome: 999, PointDiamondHostIncome: 100, PointDiamondAgencyShareIncome: 20, GiftedHostCount: 1}
	summary := agencyCenterSummaryFromGiftStats(stats, 1, "POINT_DIAMOND")
	if summary.GiftIncome != "100" || summary.ShareIncome != "20" || summary.TotalEarnings != "120" || summary.SalaryHostIncome != "999" || summary.PointDiamondHostIncome != "100" || summary.PointDiamondAgencyShareIncome != "20" || summary.GiftedHostCount != 1 || summary.TotalHosts != 1 {
		t.Fatalf("self Host/Agency summary split mismatch: %+v", summary)
	}
}

func TestHostCenterRevenueMetricsSelectsCurrentPolicyWithoutMixingAssets(t *testing.T) {
	stats := &walletv1.HostRevenueStats{DiamondEarnings: 900, DiamondExchanged: 80, PointDiamondEarnings: 120, PointDiamondExchanged: 40}
	pointMetrics := hostCenterRevenueMetrics(stats, "POINT_DIAMOND")
	if pointMetrics.DiamondEarnings != "120" || pointMetrics.DiamondExchanged != "40" || pointMetrics.SalaryDiamondEarnings != "900" || pointMetrics.LegacyPointExchanged != "80" {
		t.Fatalf("POINT_DIAMOND metrics mixed assets: %+v", pointMetrics)
	}
	salaryMetrics := hostCenterRevenueMetrics(stats, "SALARY_DIAMOND")
	if salaryMetrics.DiamondEarnings != "900" || salaryMetrics.DiamondExchanged != "80" || salaryMetrics.PointDiamondEarnings != "120" || salaryMetrics.PointDiamondExchanged != "40" {
		t.Fatalf("SALARY_DIAMOND metrics changed legacy fields: %+v", salaryMetrics)
	}
}
