import { act, cleanup, render, screen } from '@testing-library/react';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';

import {
  AutomationUnauthorizedError,
  fetchSearchManualInterventions,
  type SearchManualInterventionAlert,
} from '@/services/automationService';
import { SearchManualInterventionProvider, useSearchManualIntervention } from '@/contexts/SearchManualInterventionContext';

const authState = vi.hoisted(() => ({
  current: {
    isAuthenticated: false,
    isBootstrapping: true,
    session: null as { token: string } | null,
  },
}));

vi.mock('@/hooks/useAuth', () => ({
  useAuth: () => authState.current,
}));

vi.mock('@/services/automationService', async () => {
  const actual = await vi.importActual<typeof import('@/services/automationService')>('@/services/automationService');
  return {
    ...actual,
    fetchSearchManualInterventions: vi.fn(),
    openSearchManualIntervention: vi.fn(),
  };
});

vi.mock('sonner', () => ({
  toast: {
    error: vi.fn(),
    success: vi.fn(),
  },
}));

function createAlert(overrides: Partial<SearchManualInterventionAlert> = {}): SearchManualInterventionAlert {
  return {
    alertId: overrides.alertId ?? 'alert-1',
    taskId: overrides.taskId ?? 'task-1',
    batchId: overrides.batchId ?? 'CG-TEST-001',
    accountId: overrides.accountId ?? 'account-1',
    accountName: overrides.accountName ?? '测试账号',
    status: overrides.status ?? 'waiting_for_manual',
    headless: overrides.headless ?? true,
    message: overrides.message ?? '检测到验证码，等待人工处理',
    kind: overrides.kind ?? 'captcha',
    materialId: overrides.materialId ?? 'material-1',
    materialName: overrides.materialName ?? '测试物料',
    pageUrl: overrides.pageUrl ?? '',
    detectedAt: overrides.detectedAt ?? '2026-04-24T10:00:00.000Z',
    resolvedAt: overrides.resolvedAt ?? null,
    updatedAt: overrides.updatedAt ?? '2026-04-24T10:00:00.000Z',
    active: overrides.active ?? true,
  };
}

function Probe() {
  const { activeCount } = useSearchManualIntervention();
  return <div data-testid="active-count">{activeCount}</div>;
}

function renderProvider() {
  return render(
    <SearchManualInterventionProvider>
      <Probe />
    </SearchManualInterventionProvider>,
  );
}

async function flushAsyncWork() {
  await act(async () => {
    await Promise.resolve();
    await Promise.resolve();
  });
}

describe('SearchManualInterventionProvider', () => {
  const fetchSearchManualInterventionsMock = vi.mocked(fetchSearchManualInterventions);

  beforeEach(() => {
    vi.useFakeTimers();
    Object.defineProperty(window.HTMLMediaElement.prototype, 'play', {
      configurable: true,
      value: vi.fn(() => Promise.resolve()),
    });
    fetchSearchManualInterventionsMock.mockReset();
    fetchSearchManualInterventionsMock.mockResolvedValue({ alerts: [] });
    authState.current = {
      isAuthenticated: false,
      isBootstrapping: true,
      session: null,
    };
  });

  afterEach(() => {
    cleanup();
    vi.clearAllTimers();
    vi.useRealTimers();
    vi.clearAllMocks();
  });

  it('does not poll while auth state is still bootstrapping', () => {
    renderProvider();

    expect(fetchSearchManualInterventionsMock).not.toHaveBeenCalled();

    act(() => {
      vi.advanceTimersByTime(6000);
    });

    expect(fetchSearchManualInterventionsMock).not.toHaveBeenCalled();
    expect(screen.getByTestId('active-count')).toHaveTextContent('0');
  });

  it('polls only after the user is authenticated with a session token', async () => {
    authState.current = {
      isAuthenticated: true,
      isBootstrapping: false,
      session: { token: 'token-xyz' },
    };

    renderProvider();

    await flushAsyncWork();
    expect(fetchSearchManualInterventionsMock).toHaveBeenCalledTimes(1);

    await act(async () => {
      vi.advanceTimersByTime(2000);
      await Promise.resolve();
    });

    expect(fetchSearchManualInterventionsMock).toHaveBeenCalledTimes(2);
  });

  it('clears alerts and pauses polling after a manual intervention 401', async () => {
    authState.current = {
      isAuthenticated: true,
      isBootstrapping: false,
      session: { token: 'expired-token' },
    };
    fetchSearchManualInterventionsMock
      .mockResolvedValueOnce({ alerts: [createAlert()] })
      .mockRejectedValueOnce(new AutomationUnauthorizedError('未登录或登录态已失效'));

    renderProvider();

    await flushAsyncWork();
    expect(screen.getByTestId('active-count')).toHaveTextContent('1');

    await act(async () => {
      vi.advanceTimersByTime(2000);
      await Promise.resolve();
    });

    expect(screen.getByTestId('active-count')).toHaveTextContent('0');

    await act(async () => {
      vi.advanceTimersByTime(6000);
      await Promise.resolve();
    });

    expect(fetchSearchManualInterventionsMock).toHaveBeenCalledTimes(2);
  });
});
