import { fireEvent, render, screen } from "@testing-library/react";
import { expect, test, vi } from "vitest";
import { ActivityTemplateTaskClaimsPanel } from "./ActivityTemplateTaskClaimsPanel.jsx";

test("renders immutable claim attribution and retries only failed claims", () => {
    const onRetryClaim = vi.fn();
    render(
        <ActivityTemplateTaskClaimsPanel
            abilities={{ canRetry: true }}
            data={{
                items: [
                    {
                        attributionAtMs: Date.parse("2026-07-14T03:00:00Z"),
                        attemptCount: 2,
                        claimId: "claim-101",
                        createdAtMs: Date.parse("2026-07-14T03:01:00Z"),
                        earningRegionId: 7,
                        failureReason: "wallet unavailable",
                        grantedAtMs: 0,
                        rewardItems: [{ name: "Avatar frame" }],
                        rewardName: "Daily task reward",
                        rewardResourceGroupId: 81,
                        rewardSnapshotHash: "snapshot-hash",
                        rewardSnapshotId: "rgs-81-v3",
                        status: "failed",
                        taskDay: "2026-07-14",
                        taskKey: "send_gift",
                        updatedAtMs: Date.parse("2026-07-14T03:02:00Z"),
                        user: { displayUserId: "HY503", username: "User 503" },
                        userId: 503,
                        versionNo: 3,
                        walletGrantId: "",
                    },
                ],
                pageSize: 20,
                total: 1,
            }}
            error=""
            loading={false}
            loadingAction=""
            page={1}
            periodOptions={["2026-07-14"]}
            status=""
            taskDay=""
            taskKey=""
            taskOptions={[{ taskKey: "send_gift" }]}
            userId=""
            onDayChange={vi.fn()}
            onPageChange={vi.fn()}
            onRetry={vi.fn()}
            onRetryClaim={onRetryClaim}
            onStatusChange={vi.fn()}
            onTaskKeyChange={vi.fn()}
            onUserIdChange={vi.fn()}
        />,
    );

    expect(screen.getByText("claim-101")).toBeInTheDocument();
    expect(screen.getByText("Daily task reward")).toBeInTheDocument();
    expect(screen.getByText(/区域 #7/)).toBeInTheDocument();
    expect(screen.getByText("wallet unavailable")).toBeInTheDocument();

    fireEvent.click(screen.getByRole("button", { name: "重试发放" }));
    expect(onRetryClaim).toHaveBeenCalledWith(expect.objectContaining({ claimId: "claim-101" }));
});
