import { afterEach, expect, test, vi } from "vitest";
import { setAccessToken } from "@/shared/api/request";
import { listRegistrationRewardClaims } from "./api";

afterEach(() => {
    setAccessToken("");
    vi.unstubAllGlobals();
});

test("listRegistrationRewardClaims sends receive time filters and normalizes today count", async () => {
    vi.stubGlobal(
        "fetch",
        vi.fn(
            async () =>
                new Response(
                    JSON.stringify({
                        code: 0,
                        data: {
                            items: [
                                {
                                    claim_id: "rclaim_1",
                                    coin_amount: 1000,
                                    user: {
                                        display_user_id: "165121",
                                        username: "jayCiE",
                                    },
                                    user_id: 3201,
                                },
                            ],
                            page: 1,
                            pageSize: 50,
                            today_claimed_count: 7,
                            total: 1,
                        },
                    }),
                ),
        ),
    );

    const result = await listRegistrationRewardClaims({
        claimed_end_ms: 2000,
        claimed_start_ms: 1000,
        page: 1,
        page_size: 50,
        status: "granted",
    });
    const [url, init] = vi.mocked(fetch).mock.calls[0];

    expect(String(url)).toContain("/api/v1/admin/activity/registration-reward/claims?");
    expect(String(url)).toContain("claimed_start_ms=1000");
    expect(String(url)).toContain("claimed_end_ms=2000");
    expect(String(url)).toContain("status=granted");
    expect(init?.method).toBe("GET");
    expect(result.todayClaimedCount).toBe(7);
    expect(result.items[0]).toMatchObject({
        claimId: "rclaim_1",
        coinAmount: 1000,
        user: { displayUserId: "165121", username: "jayCiE" },
    });
});
