package user

import (
	"reflect"
	"testing"

	userdomain "hyapp/services/user-service/internal/domain/user"
)

func TestMergeActiveBanUsesPermanentFactAndPreservesCount(t *testing.T) {
	finite := userdomain.ActiveBanSummary{
		Active: true, Source: userdomain.BanSourceManager, BanID: "manager-1",
		ExpiresAtMs: 2_000, CreatedAtMs: 100, ActiveBanCount: 1,
	}
	permanent := userdomain.ActiveBanSummary{
		Active: true, Source: userdomain.BanSourceAdmin, BanID: "admin-1",
		Permanent: true, CreatedAtMs: 200, ActiveBanCount: 1,
	}

	merged := mergeActiveBan(finite, permanent)
	if !merged.Permanent || merged.BanID != "admin-1" || merged.ActiveBanCount != 2 {
		t.Fatalf("permanent ban must govern aggregate: %+v", merged)
	}
}

func TestMergeActiveBanUsesLatestFiniteExpiry(t *testing.T) {
	first := userdomain.ActiveBanSummary{Active: true, BanID: "first", ExpiresAtMs: 3_000, CreatedAtMs: 100, ActiveBanCount: 1}
	second := userdomain.ActiveBanSummary{Active: true, BanID: "second", ExpiresAtMs: 5_000, CreatedAtMs: 50, ActiveBanCount: 1}

	merged := mergeActiveBan(first, second)
	if merged.BanID != "second" || merged.ExpiresAtMs != 5_000 || merged.ActiveBanCount != 2 {
		t.Fatalf("latest finite expiry must govern aggregate: %+v", merged)
	}
}

func TestAppendAdminRoleKeepsStableDisplayOrder(t *testing.T) {
	roles := make([]string, 0, 6)
	roles = appendAdminRole(roles, true, userdomain.AdminRoleHost)
	roles = appendAdminRole(roles, true, userdomain.AdminRoleAgency)
	roles = appendAdminRole(roles, false, userdomain.AdminRoleBD)
	roles = appendAdminRole(roles, true, userdomain.AdminRoleManager)

	want := []string{userdomain.AdminRoleHost, userdomain.AdminRoleAgency, userdomain.AdminRoleManager}
	if !reflect.DeepEqual(roles, want) {
		t.Fatalf("role order mismatch: got=%v want=%v", roles, want)
	}
}
