/* @vitest-environment jsdom */

import { render, screen, within } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { describe, expect, test, vi } from 'vitest';

import { PlatformProfileTaskCenterModal } from './PlatformProfileTaskCenterModal';

describe('PlatformProfileTaskCenterModal', () => {
  test('renders claimable tasks and forwards claim action', async () => {
    const user = userEvent.setup();
    const onClaim = vi.fn();

    render(
      <PlatformProfileTaskCenterModal
        center={{
          dayKey: 20260610,
          walletBalance: 66,
          updatedAt: '2026-06-10T08:00:00.000Z',
          tasks: [
            {
              taskId: 'task-1',
              title: '每日登录',
              description: '登录一次',
              eventKey: 'daily_login',
              cycle: 'daily',
              rewardPoints: 10,
              status: 'claimable',
              progressCount: 1,
              threshold: 1,
              dayKey: 20260610,
              claimedAt: null,
              updatedAt: '2026-06-10T08:00:00.000Z',
            },
          ],
        }}
        isLoading={false}
        error={null}
        success={null}
        claimingTaskId={null}
        fallbackBalance={12}
        onClose={vi.fn()}
        onRetry={vi.fn()}
        onClaim={onClaim}
      />,
    );

    const dialog = screen.getByRole('dialog', { name: '每日任务' });

    expect(within(dialog).getByText('66泥点')).toBeTruthy();
    expect(within(dialog).getByText('每日登录')).toBeTruthy();
    expect(within(dialog).getByText('1/1')).toBeTruthy();
    expect(within(dialog).getByText('可领取')).toBeTruthy();

    await user.click(within(dialog).getByRole('button', { name: '领取' }));
    expect(onClaim).toHaveBeenCalledWith('task-1');
  });

  test('keeps incomplete tasks disabled', () => {
    render(
      <PlatformProfileTaskCenterModal
        center={{
          dayKey: 20260610,
          walletBalance: 20,
          updatedAt: '2026-06-10T08:00:00.000Z',
          tasks: [
            {
              taskId: 'task-2',
              title: '分享一次',
              description: '完成一次分享',
              eventKey: 'daily_share',
              cycle: 'daily',
              rewardPoints: 8,
              status: 'incomplete',
              progressCount: 0,
              threshold: 1,
              dayKey: 20260610,
              claimedAt: null,
              updatedAt: '2026-06-10T08:00:00.000Z',
            },
          ],
        }}
        isLoading={false}
        error={null}
        success={null}
        claimingTaskId={null}
        fallbackBalance={12}
        onClose={vi.fn()}
        onRetry={vi.fn()}
        onClaim={vi.fn()}
      />,
    );

    expect(
      screen.getByRole('button', { name: '未完成' }).hasAttribute('disabled'),
    ).toBe(true);
  });
});
