package userapi

import (
	"testing"

	userv1 "hyapp.local/api/proto/user/v1"
)

func TestCPRelationshipFromProtoIncludesLevelProgress(t *testing.T) {
	item := &userv1.CPRelationship{
		RelationshipId:        "cp_rel:1:2",
		RelationType:          "brother",
		Status:                "active",
		IntimacyValue:         180,
		Level:                 2,
		CurrentLevelThreshold: 100,
		NextLevelThreshold:    300,
		NeededForNextLevel:    120,
		LevelProgressPercent:  40,
		MaxLevel:              false,
		BreakupCostCoins:      88,
		FormedAtMs:            1700000000000,
		EndedAtMs:             1700000002000,
		UpdatedAtMs:           1700000001000,
	}

	got := cpRelationshipFromProto(item)
	if got.CurrentLevelThreshold != 100 ||
		got.NextLevelThreshold != 300 ||
		got.NeededForNextLevel != 120 ||
		got.LevelProgressPercent != 40 ||
		got.MaxLevel ||
		got.BreakupCostCoins != 88 ||
		got.EndedAtMS != 1700000002000 {
		t.Fatalf("relationship progress fields not mapped: %+v", got)
	}
}

func TestCPIntimacyLeaderboardItemFromProtoIncludesBothUsersAndAvatarFrames(t *testing.T) {
	item := &userv1.CPIntimacyLeaderboardItem{
		Rank:           3,
		RelationshipId: "cp_rel:7101:7102",
		RelationType:   "cp",
		IntimacyValue:  9527,
		Level:          6,
		UserA: &userv1.CPIntimacyLeaderboardUser{
			UserId:        7101,
			DisplayUserId: "1007101",
			Username:      "Alice",
			Avatar:        "alice.png",
			AvatarFrame: &userv1.CPAvatarFrameSnapshot{
				ResourceId:   9101,
				ResourceCode: "frame_gold",
				Name:         "Gold",
				AssetUrl:     "gold.webp",
			},
		},
		UserB: &userv1.CPIntimacyLeaderboardUser{
			UserId:        7102,
			DisplayUserId: "1007102",
			Username:      "Bob",
			Avatar:        "bob.png",
			AvatarFrame: &userv1.CPAvatarFrameSnapshot{
				ResourceId:   9102,
				ResourceCode: "frame_blue",
				Name:         "Blue",
				PreviewUrl:   "blue-preview.webp",
			},
		},
		FormedAtMs:  1700000000000,
		UpdatedAtMs: 1700000005000,
	}

	got := cpIntimacyLeaderboardItemFromProto(item)
	if got.Rank != 3 ||
		got.RelationshipID != "cp_rel:7101:7102" ||
		got.IntimacyValue != 9527 ||
		got.Level != 6 ||
		got.UserA == nil ||
		got.UserA.UserID != "7101" ||
		got.UserA.DisplayUserID != "1007101" ||
		got.UserA.AvatarFrame == nil ||
		got.UserA.AvatarFrame.ResourceCode != "frame_gold" ||
		got.UserB == nil ||
		got.UserB.UserID != "7102" ||
		got.UserB.AvatarFrame == nil ||
		got.UserB.AvatarFrame.PreviewURL != "blue-preview.webp" ||
		got.FormedAtMS != 1700000000000 ||
		got.UpdatedAtMS != 1700000005000 {
		t.Fatalf("leaderboard item fields not mapped: %+v", got)
	}
}
